This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12个答案)
上个月关闭。
我正在创建测验应用程序。当所有问题都回答了。我打算开放分数活动。但是应用程序崩溃并在设置活动中显示错误
结构(MainActivity-> CategoryActivity-> SetsActivity-> QuestionActivity-> ScoreActivity)
这是logcat
从CategoryActivity传递
错误在这行
这是SetsActivity.java
希望这会帮助你。
(12个答案)
上个月关闭。
我正在创建测验应用程序。当所有问题都回答了。我打算开放分数活动。但是应用程序崩溃并在设置活动中显示错误
结构(MainActivity-> CategoryActivity-> SetsActivity-> QuestionActivity-> ScoreActivity)
这是logcat
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.marsad.quiz/com.marsad.quizapp.SetsActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2890)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2968)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1672)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:192)
at android.app.ActivityThread.main(ActivityThread.java:6711)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:826)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
at com.marsad.quizapp.SetsActivity.onCreate(SetsActivity.java:68)
at android.app.Activity.performCreate(Activity.java:7181)
at android.app.Activity.performCreate(Activity.java:7172)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2843)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2968)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1672)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:192)
at android.app.ActivityThread.main(ActivityThread.java:6711)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:826)
从CategoryActivity传递
public static List<CategoryModel> list;
错误在这行
sets = CategoryActivity.list.get(getIntent().getIntExtra("position", 0)).getSets();
这是SetsActivity.java
public class SetsActivity extends AppCompatActivity {
private GridView gridView;
private List<String> sets;
private InterstitialAd mInterstitialAd;
//Theme
Constant constant;
SharedPreferences app_preferences;
int appTheme;
int themeColor;
int appColor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
appColor = app_preferences.getInt("color", 0);
appTheme = app_preferences.getInt("theme", 0);
//defaultColor = app_preferences.getInt("default", 0);
themeColor = appColor;
constant.color = appColor;
if (themeColor == 0){
setTheme(Constant.theme);
}else if (appTheme == 0){
setTheme(Constant.theme);
}else{
setTheme(appTheme);
}
setContentView(R.layout.activity_sets);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle(getIntent().getStringExtra("title"));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
gridView = findViewById(R.id.gridView);
sets = CategoryActivity.list.get(getIntent().getIntExtra("position", 0)).getSets();
GridAdapter adapter = new GridAdapter(sets, getIntent().getStringExtra("title"), mInterstitialAd);
gridView.setAdapter(adapter);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
}
return super.onOptionsItemSelected(item);
}
}
最佳答案
您是sets = CategoryActivity.list.get(getIntent().getIntExtra("position", 0)).getSets();
行中的正确问题
CategoryActivity.list实际上为null
请检查对象是否已初始化或不在列表中,因此在访问check之前-
if(CategoryActivity.list != null){
sets = CategoryActivity.list.get(getIntent().getIntExtra("position", 0)).getSets();
GridAdapter adapter = new GridAdapter(sets, getIntent().getStringExtra("title"), mInterstitialAd);
gridView.setAdapter(adapter);
}
希望这会帮助你。
09-18 05:35