NullPointerException OnClickListener

时间:2014-04-30 11:17:42

标签: android nullpointerexception onclicklistener

我在单击按钮时尝试调用另一个活动,但应用程序在打开时崩溃。 主要活动:     包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"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.agrandesacada"
android:versionCode="1"
android:versionName="1.0" >

<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>

</manifest>

错误:

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

2 个答案:

答案 0 :(得分:1)

activity_main.xml布局可能不包含ID为Button的{​​{1}}。这就是它投掷btnaluno的原因。如果确实如此,请在NullPointerException中添加标识为Button的{​​{1}}。

另一件事,您应该初始化btnaluno对象,如下所示

activity_main.xml

而不是

Intent

答案 1 :(得分:1)

首先确保您的布局xml中有一个ID为“btnaluno”的Button,然后更改您的意图,如下所示:

 Intent i = new Intent(MainActivity.this , AlunoActivity.class);
 startActivity(i);