-2
I’m trying to create a simple event to change page by clicking the button, but whenever I run the application it crashes when it opens.
Mainactivity:
package com.app.agrandesacada;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;
public class MainActivity extends Activity {
private Button btnAlunos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAlunos=(Button)findViewById(R.id.btnaluno);
btnAlunos.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent("com.app.agrandesacada.AlunoActivity");
startActivity(i);
finish();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Manifestxml:
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.app.agrandesacada.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="com.app.agrandesacada.AlunoActivity"
android:label="@string/title_activity_aluno" >
<intent-filter>
<action android:name="com.app.agrandesacada.AlunoActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
Error:
04-29 20:45:51.941: D/androidruntime(511): Shutting down VM 04-29 20:45:51.941: W/dalvikvm(511): threadid=1: thread exiting with uncaught Exception (group=0x40015560) 04-29 20:45:51.961: E/androidruntime(511): FATAL EXCEPTION: main 04-29 20:45:51.961: E/Androidruntime(511): java.lang.Runtimeexception: Unable to start Activity Componentinfo{com.app.agrandesacada/com.app.agrandesacada.Mainactivity}: java.lang.Nullpointerexception 04-29 20:45:51.961: E/Androidruntime(511): at android.app.Activitythread.performLaunchActivity(Activitythread.java:1647) 04-29 20:45:51.961: E/Androidruntime(511): at android.app.Activitythread.handleLaunchActivity(Activitythread.java:1663) 04-29 20:45:51.961: E/Androidruntime(511): at android.app.Activitythread.access$1500(Activitythread.java:117) 04-29 20:45:51.961: E/Androidruntime(511): at android.app.Activitythread$H.handleMessage(Activitythread.java:931) 04-29 20:45:51.961: E/Androidruntime(511): at android.os.Handler.dispatchMessage(Handler.java:99) 04-29 20:45:51.961: E/Androidruntime(511): at android.os.Looper.loop(Looper.java:123) 04-29 20:45:51.961: E/Androidruntime(511): at android.app.Activitythread.main(Activitythread.java:3683) 04-29 20:45:51.961: E/Androidruntime(511): at java.lang.reflect.Method.invokeNative(Native Method) 04-29 20:45:51.961: E/Androidruntime(511): at java.lang.reflect.Method.invoke(Method.java:507) 04-29 20:45:51.961: E/Androidruntime(511): at com.android.Internal.os.Zygoteinit$Methodandargscaller.run(Zygoteinit.java:839) 04-29 20:45:51.961: E/Androidruntime(511): at com.android.Internal.os.Zygoteinit.main(Zygoteinit.java:597) 04-29 20:45:51.961: E/Androidruntime(511): at Dalvik.system.Nativestart.main(Native Method) 04-29 20:45:51.961: E/Androidruntime(511): Caused by: java.lang.Nullpointerexception 04-29 20:45:51.961: E/Androidruntime(511): at com.app.agrandesacada.MainActivity.onCreate(Mainactivity.java:24) 04-29 20:45:51.961: E/Androidruntime(511): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 04-29 20:45:51.961: E/Androidruntime(511): at android.app.Activitythread.performLaunchActivity(Activitythread.java:1611) 04-29 20:45:51.961: E/Androidruntime(511): ... 11 more
He gives Nullpointexception but I don’t understand why.
Give a debug and see where the execution really stops, because if the crash before the app runs it is very likely to be in the first lines.
– Furflez
Complementing the @Furflez comment, look to write methods
onCreate
onStart
andonResume
of Activity and put several log on them, to know how far the code can run.– anisanwesley