yazeed yazeed - 1 month ago 7
Android Question

My app crashes when execute Intent statement

I learn android studio programing and I have problem in my programs,
when I want to go to new activity using intent statement my app crashed,
this is my main activity;

public class MainActivity extends AppCompatActivity {

private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;

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

mAuth = FirebaseAuth.getInstance();
}

public void onStart(){
super.onStart();
FirebaseUser CurrentUser = mAuth.getCurrentUser();
if(CurrentUser == null){
Intent StartIntent = new Intent(MainActivity.this,
StartActivity.class);
startActivity(StartIntent);
finish();
}
}

}


and this is manifest :

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

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".StartActivity"
android:label="@string/app_name">

</activity>


there is no error message
but thit is what i have into android monitor :

08-23 21:50:33.507 11264-11264/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ruse.ayse.areyousmartenough, PID: 11264
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ruse.ayse.areyousmartenough/com.ruse.ayse.areyousmartenough.StartActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2378)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5433)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.ruse.ayse.areyousmartenough.StartActivity.onCreate(StartActivity.java:31)
at android.app.Activity.performCreate(Activity.java:5301)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2291)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2378) 
at android.app.ActivityThread.access$800(ActivityThread.java:155) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:136) 
at android.app.ActivityThread.main(ActivityThread.java:5433) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 
at dalvik.system.NativeStart.main(Native Method) 


I solved this problem into another project by adding "intent-filter" into manifest file but it does not work with this project,

this is StartActivity :

public class StartActivity extends AppCompatActivity {

private Button mRegBtn ;
private Button mLoginBtn;

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

mRegBtn = (Button) findViewById(R.id.RegBtn);
mLoginBtn = (Button) findViewById(R.id.LoginBtn);

mRegBtn.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View view) {
Intent createAccount = new Intent(StartActivity.this, RegisterActivity.class);
startActivity(createAccount);
}
});
mLoginBtn.setOnClickListener(new View.OnClickListener(){ /////this is line 31
@Override
public void onClick(View view) {
Intent loginAccount = new Intent(StartActivity.this, LoginActivity.class);
startActivity(loginAccount);
}
});
}}

Answer Source

Your problem is at StartActivity.java:31 you are trying to read from a null reference.

By your comment, you look to miss getting the mLoginBtn from your Layout=

mLoginBtn = (Button) findViewById(R.id.btn_login);