有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java错误打开意图

调用其他意图时出错

我无法解决这个问题,我需要尽快解决它,以完成我的工作当然是完成课程。提前感谢您的帮助

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 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
06-09 14:41:28.902: E/AndroidRuntime(769): 

我的舱单

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

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

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

    <application
        安卓:allowBackup="true"
        安卓:icon="@drawable/ic_launcher"
        安卓:label="@string/app_name"
        安卓:theme="@style/AppTheme" >
        <activity
            安卓:name="br.com.projeto_tcc.view.NewPlayerGUI"
            安卓:label="@string/app_name" >
            <intent-filter>
                <action 安卓:name="安卓.intent.action.MAIN" />

                <category 安卓:name="安卓.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity 安卓:name="br.com.projeto_tcc.view.PlayerGUI"/>
        <activity 安卓:name="br.com.projeto_tcc.view.OpcaoGUI"/>
        <activity 安卓:name="br.com.projeto_tcc.view.JogoGUI"/>
        <activity 安卓:name="br.com.projeto_tcc.view.EscolhaGUI"/>
        <activity 安卓:name="br.com.projeto_tcc.bluetooth.Actv"/>
        <activity 安卓: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();
    }


}

共 (1) 个答案

  1. # 1 楼答案

    您遇到的问题是,在调用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