Evan Evan - 1 month ago 8
Android Question

App not working after new Activity

So I'm new to Android Studio and I've been trying to build an app. It was working fine when running it on a virtual phone / emulator (nexus_5_API_24) but when I created a new Activity it stopped working. Error given says:


"Instant Run performed a clean build and install since the
installation on the device does not match the local build on disk."


What's going on?

CatsMenu Activity

public class CatsMenu extends AppCompatActivity {
Button btn1 = (Button) findViewById(R.id.mainButton1);

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_cats_menu);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});

btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
newActivity(v);
}
});
}

public void newActivity(View v) {
startActivity(new Intent(CatsMenu.this, Disponibles.class));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_cats_menu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}


Disponibles Activity

public class Disponibles extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_disponibles);
}
}


Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cats.evan.catsapp">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".CatsMenu"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:label="@string/disponibles" android:name=".Disponibles"/>
</application>
</manifest>


UPDATE: Now I get this exception:


E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.cats.evan.catsapp, PID: 2441
java.lang.RuntimeException: Unable to instantiate activity
ComponentInfo{com.cats.evan.catsapp/com.cats.evan.catsapp.CatsMenu}:
java.lang.NullPointerException: Attempt to invoke virtual method
'android.view.Window$Callback android.view.Window.getCallback()' on a
null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2548)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback
android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImplBase.(AppCompatDelegateImplBase.java:116)
at android.support.v7.app.AppCompatDelegateImplV9.(AppCompatDelegateImplV9.java:147)
at android.support.v7.app.AppCompatDelegateImplV11.(AppCompatDelegateImplV11.java:27)
at android.support.v7.app.AppCompatDelegateImplV14.(AppCompatDelegateImplV14.java:50)

Answer

Your error isn't instant run, and it isn't the other class.

Unable to instantiate activity ComponentInfo{com.cats.evan.catsapp/com.cats.evan.catsapp.CatsMenu}

You need to move the initialization of the button into onCreate

Button btn1 = (Button) findViewById(R.id.mainButton1);

You can declare the button as member variable, but you are unable to find the reference until the Activity layout has been loaded