当我调用另一个意图时出错

我无法解决此问题,因此我需要尽快解决它,以完成课程的完成工作。预先感谢您的任何帮助!

06-09 14:41:28.902: E/AndroidRuntime(769): FATAL EXCEPTION: main
06-09 14:41:28.902: E/AndroidRuntime(769): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{br.com.projeto_tcc/br.com.projeto_tcc.view.EscolhaGUI}: java.lang.NullPointerException
06-09 14:41:28.902: E/AndroidRuntime(769):
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
06-09 14:41:28.902: E/AndroidRuntime(769):


我的清单

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="br.com.projeto_tcc"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

        <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
        <uses-permission android:name="android.permission.BLUETOOTH"/>
         <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="br.com.projeto_tcc.view.NewPlayerGUI"
            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="br.com.projeto_tcc.view.PlayerGUI"/>
        <activity android:name="br.com.projeto_tcc.view.OpcaoGUI"/>
        <activity android:name="br.com.projeto_tcc.view.JogoGUI"/>
        <activity android:name="br.com.projeto_tcc.view.EscolhaGUI"/>
        <activity android:name="br.com.projeto_tcc.bluetooth.Actv"/>
        <activity android:name="br.com.projeto_tcc.bluetooth.MainActivity"/>
    </application>

</manifest>


我的课

public class EscolhaGUI extends Activity {

Intent intent = new Intent(this, Actv.class);
Intent intent2 = new Intent(this, MainActivity.class);

@Override
protected void onCreate(Bundle savedInstanceState) {
    try {
        final Button btn_esp = (Button) findViewById(R.id.btn_esp);
        final Button btn_conv = (Button) findViewById(R.id.btn_conv);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.escolher);
        btn_esp.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                finish();
                startActivity(intent);

            }

        });

        btn_conv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                finish();
                startActivity(intent2);

            }

        });

    } catch (Exception e) {
        AlertDialog alertDialog;
        alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("Erro");
        alertDialog.setMessage("Este: ." + e);
        alertDialog.show();
    }


}

最佳答案

您遇到的问题是在调用setContentView(R.layout.escolher)之前要检索这些按钮。因此,在findViewById之后放置对setContentView的调用,您的onCreate方法应如下所示:

@Override
protected void onCreate(Bundle savedInstanceState) {
    try {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.escolher);
        final Button btn_esp = (Button) findViewById(R.id.btn_esp);
        final Button btn_conv = (Button) findViewById(R.id.btn_conv);
        btn_esp.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                finish();
                startActivity(intent);

            }

        });

        btn_conv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                finish();
                startActivity(intent2);

            }

        });

    } catch (Exception e) {
        AlertDialog alertDialog;
        alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("Erro");
        alertDialog.setMessage("Este: ." + e);
        alertDialog.show();
    }


}


编辑:
如果您不太清楚我要说的是什么,请尝试调试。您可以打赌,两个按钮都等于null,从而在此行中引起NPE:

btn_esp.setOnClickListener(new OnClickListener() {...}


就像logcat跟踪在这里指出的那样:

06-09 14:41:28.902: E/AndroidRuntime(769): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{br.com.projeto_tcc/br.com.projeto_tcc.view.EscolhaGUI}: java.lang.NullPointerException

09-13 13:03