我正在尝试使用Intent
从Activity
调用新的MainActivity
。我试图通过Intent Extras
传递一些数据。以下是我调用新Activity
的代码。
public void tabSelector2(View v) {
switch (v.getId()) {
case R.id.veg_tab:
currentDB = "veg";
break;
case R.id.meat_tab :
currentDB = "meat";
break;
default:
break;
}
Intent tabSelect = new Intent(this, TabSelector.class);
tabSelect.putExtra("db_passed", currentDB);
startActivity(tabSelect);
}
onCreate
内Activity2
我尝试使用我传递的变量currentDB
。变量正确传递。我使用System.out.println()
打印了值,logcat
生成了正确的输出。但是当我尝试使用该值时,我得到NullPointerException
。 onCreate
的代码段如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(android.R.style.Theme_Holo); // (for Android Built In Theme)
mydb = new DBHelper(this);
Bundle extras = getIntent().getExtras();
if (extras != null) {
currentDB = extras.getString("db_passed");
}
System.out.println(currentDB); // Printing here, correct value
currentList2 = mydb.getAllItemsAsCollection(currentDB); // Works Perfectly
itemAdder2.setDbName(currentDB); // Produces an error :(
itemAdder2.updateData2(currentList2);
switch (currentDB) {
case "veg" :
setContentView(R.layout.vegetable);
currentListView = (ListView) findViewById(R.id.veg_list);
break;
case "meat" :
setContentView(R.layout.meat_drawer);
currentListView = (ListView) findViewById(R.id.meat_list);
break;
default:
break;
}
currentListView.setAdapter(itemAdder2);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
相应的setDBName
方法非常简单。
public void setDbName(String dbName){
this.dbName = dbName;
}
我在运行时遇到以下错误...
08-02 13:20:23.757 2641-2701/com.example.user.reggaeshark I/OpenGLRenderer? Initialized EGL, version 1.4
08-02 13:20:23.838 2641-2701/com.example.user.reggaeshark D/OpenGLRenderer? Enabling debug mode 0
08-02 13:20:24.150 2641-2701/com.example.user.reggaeshark V/RenderScript? Application requested CPU execution
08-02 13:20:24.163 2641-2701/com.example.user.reggaeshark V/RenderScript? 0xb857f848 Launching thread(s), CPUs 4
08-02 13:20:25.817 2641-2659/com.example.user.reggaeshark W/art? Suspending all threads took: 8.192ms
08-02 13:20:25.843 2641-2641/com.example.user.reggaeshark I/System.out? meat
08-02 13:20:25.861 2641-2641/com.example.user.reggaeshark D/AndroidRuntime? Shutting down VM
08-02 13:20:25.868 2641-2641/com.example.user.reggaeshark E/AndroidRuntime? FATAL EXCEPTION: main
Process: com.example.user.reggaeshark, PID: 2641
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.reggaeshark/com.example.user.reggaeshark.TabSelector}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.user.reggaeshark.ListViewAdapter.setDbName(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2314)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
at android.app.ActivityThread.access$800(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.user.reggaeshark.ListViewAdapter.setDbName(java.lang.String)' on a null object reference
at com.example.user.reggaeshark.TabSelector.onCreate(TabSelector.java:43)
at android.app.Activity.performCreate(Activity.java:5953)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1128)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
at android.app.ActivityThread.access$800(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
请帮忙。如果你们需要额外的东西,请告诉我。
答案 0 :(得分:1)
您的itemAdder2
变量未初始化。尝试在空引用上调用方法会导致您看到的NPE。
答案 1 :(得分:1)
Yout适配器对象为null - itemAdder2。你应该像这样“
实例化他itemAdder2 = new YourAdapter(Some params here);