无法实例化活动ComponentInfo;显示java.lang.NullPointerException

时间:2013-12-28 06:32:06

标签: java android nullpointerexception runtime-error adt

对不起。 Java和Java初学者节目。 我已经搜索了其他答案,但我找不到我的程序有什么问题。 顺便说一句:NullPointerException意味着在程序的某个地方使用一个带有null的变量作为其值,对吗?

主要活动代码:

package z_industries.theomnipotentbutton.theomnipotentbuttonfull;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.view.View.OnClickListener;

public class MainActivity extends BaseActivity {


//initialize Log string
final public static String MTAG = "Main Activity";

//initialize variables
private RadioGroup radioGroup;
//private RadioButton radioButton;
private Button playButton;
public int selectedPlayer = 0;

//initialize intent for opening game activity
final Intent intent = new Intent(this, GameActivity.class);

@Override
protected void onCreate(Bundle savedInstanceState) {

    //onCreate random stuff
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Log.i(MTAG, "MainActivity Start");

    /*//Initialize database
    SQLiteDatabase playerDB;
    PlayerDB dbhelper = new PlayerDB (this);
    playerDB = dbhelper.getWritableDatabase();*/

    //create new function
    addListenerOnButton();

    //log
    Log.i(MTAG, "onCreate done");

}

//create the menu
@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;
}

//function to activate button
public void addListenerOnButton (){

    //get each button
    radioGroup = (RadioGroup) findViewById(R.id.playerChoice);
    playButton = (Button) findViewById(R.id.play_button);

    playButton.setOnClickListener(new OnClickListener() {


        @Override
        public void onClick (View V){

            //get which button is selected 
            int selectedId = radioGroup.getCheckedRadioButtonId();
            final RadioButton radioButton = (RadioButton) findViewById(selectedId);

            //find out which button is selected using ID
            if (radioButton.getId() == 0x7f090006){
                selectedPlayer = 1;
                Log.i(MTAG, "Selected Player 1");
            }
            else if (radioButton.getId() == 0x7f090007){
                selectedPlayer = 2;
                Log.i(MTAG, "Selected Player 2");
            }
            else if (radioButton.getId() == 0x7f090008){
                selectedPlayer = 3;
                Log.i(MTAG, "Selected Player 3");
            }
            else {
                selectedPlayer = -1;
                Log.i(MTAG, "Error on radio button id");
            }

            //activate game activity
            startActivity(intent);

            /*public void startGame(View V){

            }*/
        }
    } );
}

/*@Override
public void openGame (View V){

}*/

}

清单文件代码:

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

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="z_industries.theomnipotentbutton.theomnipotentbuttonfull.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="GameActivity">
    </activity>
    <activity
        android:name="ComicActivity">
    </activity>
    <activity
        android:name="BaseActivity">
    </activity>
</application>

除了必需品外,BaseActivity,ComicActivity和GameActivity都是空的。

提前致谢!

3 个答案:

答案 0 :(得分:2)

更改为

  Intent intent; // declare it as instance vairable

然后在onCreate

 intent = new Intent(this, GameActivity.class);

活动上下文在onCreate of activity中可用。在此之前,您无法使用this

您也可以在onCreate中对视图进行所有初始化。

答案 1 :(得分:0)

替换以下

<activity
    android:name="GameActivity">
</activity>
<activity
    android:name="ComicActivity">
</activity>
<activity
    android:name="BaseActivity">
</activity>

用这个

<activity
    android:name=".GameActivity">
</activity>
<activity
    android:name=".ComicActivity">
</activity>
<activity
    android:name=".BaseActivity">
</activity>

答案 2 :(得分:0)

//initialize intent for opening game activity
Intent intent = new Intent(this, GameActivity.class);

将此行更改为

Intent intent;

onCreate(Bundle savedInstanceState) {
     intent = new Intent(this, GameActivity.class);
     ....
}
......