1. Are you ready for the Galaxy S20? Here is everything we know so far!

Android Java - Change View

Discussion in 'Android Development' started by Frederik Frandsen, Jul 2, 2021.

  1. Frederik Frandsen

    Thread Starter

    Hello,

    I made a simple app to change view to another view.

    I trying to change android view to new layout but then i start it its crash on the botton i made in MainActivity, trying to run the code below.

    And my MainActivity
    Code (Text):
    1. package com.example.changeview;
    2.  
    3. import androidx.appcompat.app.AppCompatActivity;
    4.  
    5. import android.content.Intent;
    6. import android.os.Bundle;
    7. import android.util.Log;
    8. import android.view.View;
    9. import android.widget.Button;
    10.  
    11. public class MainActivity extends AppCompatActivity {
    12.  
    13.     Button SecondActivityButton;
    14.  
    15.     @Override
    16.     protected void onCreate(Bundle savedInstanceState) {
    17.         super.onCreate(savedInstanceState);
    18.         setContentView(R.layout.activity_main);
    19.  
    20.         SecondActivityButton = findViewById(R.id.GotoSecondActivity);
    21.         SecondActivityButton.setOnClickListener(new View.OnClickListener() {
    22.             @Override
    23.             public void onClick(View view) {
    24.                 Log.d("myTag", "Change to SecondActivity");
    25.  
    26.  
    27.                 Intent switchActivityIntent = new Intent(MainActivity.this, SecondActivity.class);
    28.                 startActivity(switchActivityIntent);
    29.  
    30.             }
    31.         });
    32.     }
    33.  
    34.  
    35.     public class SecondActivity extends AppCompatActivity {
    36.         Button backButton;
    37.  
    38.         @Override
    39.         protected void onCreate(Bundle savedInstanceState) {
    40.             super.onCreate(savedInstanceState);
    41.             setContentView(R.layout.activity_second);
    42.  
    43.             backButton = findViewById(R.id.activity_second_button);
    44.             backButton.setOnClickListener(new View.OnClickListener() {
    45.                 @Override
    46.                 public void onClick(View view) {
    47.                     Log.d("myTag", "Change to Main");
    48.                 }
    49.             });
    50.         }
    51.     }
    52. }
    And my AndroidManufest.xml
    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    3.     xmlns:tools="http://schemas.android.com/tools"
    4.     package="com.example.changeview">
    5.  
    6.     <application
    7.         android:allowBackup="true"
    8.         android:icon="@mipmap/ic_launcher"
    9.         android:label="@string/app_name"
    10.         android:roundIcon="@mipmap/ic_launcher_round"
    11.         android:supportsRtl="true"
    12.         android:theme="@style/Theme.ChangeView">
    13.         <activity android:name=".MainActivity">
    14.             <intent-filter>
    15.                 <action android:name="android.intent.action.MAIN" />
    16.  
    17.                 <category android:name="android.intent.category.LAUNCHER" />
    18.             </intent-filter>
    19.         </activity>
    20.         <activity android:name=".MainActivity$SecondActivity"
    21.             tools:ignore="Instantiatable">
    22.         </activity>
    23.     </application>
    24.  
    25. </manifest>
    Error Code:
    Code (Text):
    1. --------- beginning of crash
    2. 2021-07-02 09:12:48.787 4667-4667/com.example.changeview E/AndroidRuntime: FATAL EXCEPTION: main
    3.     Process: com.example.changeview, PID: 4667
    4.     java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.changeview/com.example.changeview.MainActivity$SecondActivity}: java.lang.InstantiationException: java.lang.Class<com.example.changeview.MainActivity$SecondActivity> has no zero argument constructor
    5.         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3365)
    6.         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
    7.         at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
    8.         at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    9.         at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    10.         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
    11.         at android.os.Handler.dispatchMessage(Handler.java:106)
    12.         at android.os.Looper.loop(Looper.java:223)
    13.         at android.app.ActivityThread.main(ActivityThread.java:7656)
    14.         at java.lang.reflect.Method.invoke(Native Method)
    15.         at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    16.         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
    17.      Caused by: java.lang.InstantiationException: java.lang.Class<com.example.changeview.MainActivity$SecondActivity> has no zero argument constructor
    18.         at java.lang.Class.newInstance(Native Method)
    19.         at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
    20.         at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
    21.         at android.app.Instrumentation.newActivity(Instrumentation.java:1253)
    22.         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3353)
    23.         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
    24.         at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
    25.         at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    26.         at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    27.         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
    28.         at android.os.Handler.dispatchMessage(Handler.java:106)
    29.         at android.os.Looper.loop(Looper.java:223)
    30.         at android.app.ActivityThread.main(ActivityThread.java:7656)
    31.         at java.lang.reflect.Method.invoke(Native Method)
    32.         at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    33.         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
    34. 2021-07-02 09:12:48.880 4667-4667/com.example.changeview I/Process: Sending signal. PID: 4667 SIG: 9
    Can someone help me out? Thanks for your time
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...
Similar Threads - Android Java Change
  1. Emir Sanchez
    Replies:
    1
    Views:
    495
  2. Zied Orabi
    Replies:
    0
    Views:
    670
  3. DonaldDuck313
    Replies:
    0
    Views:
    528
  4. Francesco10
    Replies:
    0
    Views:
    455
  5. Anand Suthar
    Replies:
    1
    Views:
    1,150
  6. tekir90
    Replies:
    0
    Views:
    1,227
  7. WinchestStoke
    Replies:
    0
    Views:
    835
  8. ninodor
    Replies:
    1
    Views:
    377
  9. andy_111
    Replies:
    0
    Views:
    961
  10. Mohamed13
    Replies:
    0
    Views:
    475

Share This Page

Loading...