gpt4 book ai didi

java - 无法实例化 Activity ComponentInfo; java.lang.NullPointerException

转载 作者:行者123 更新时间:2023-12-02 06:23:18 25 4
gpt4 key购买 nike

抱歉。 Java 和编程初学者。我已经搜索了其他答案,但我只是找不到我的程序有什么问题。顺便说一句:NullPointerException 意味着程序中的某个位置使用了一个值为 null 的变量,对吗?

主要 Activity 代码:

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

}*/

}

list 文件代码:

<?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 除了必需品之外实际上是空的。

提前致谢!

最佳答案

更改为

  Intent intent; // declare it as instance vairable

然后在onCreate

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

Activity 上下文在 Activity 的 onCreate 中可用。在此之前您不能使用this

您还可以在 onCreate 中完成 View 的所有初始化。

关于java - 无法实例化 Activity ComponentInfo; java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20811763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com