I'm trying to create a simple page-changing event when I click the button, but every time 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
It gives NullPointException but I do not understand why.