我想從活動調用一個方法到另一個活動,但我給出了空指針異常。空指針例外定義來自其他活動的方法
SidebarClass.java
public class SidebarClass extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
public void Sidebar(final Context c) {
MyAdapter adapter = new MyAdapter(c, generateData());
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.listview);
mDrawerList.setAdapter(adapter);
mDrawerList
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent,
android.view.View view, int position, long id) {
switch (position) {
case 1:
Intent i1 = new Intent(c, Map.class);
startActivity(i1);
break;
case 2:
Intent i2 = new Intent(c,
Basket.class);
startActivity(i2);
break;
}
}
});
}
private ArrayList<Model> generateData() {
ArrayList<Model> models = new ArrayList<Model>();
models.add(new Model("TimiT "));
models.add(new Model(R.drawable.home, "home", 1));
models.add(new Model(R.drawable.basket, "basket", 2));
return models;
}
}
MainActivity.java
公共類MainActivity延伸活動實現OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SidebarClass side = new SidebarClass();
side.Sidebar(getBaseContext());
}
}
logcat的
05-30 13:30:29.897: E/AndroidRuntime(6850): FATAL EXCEPTION: main
05-30 13:30:29.897: E/AndroidRuntime(6850): java.lang.RuntimeException:
Unable to start activity
ComponentInfo{com.example.timittest/com.example.timittest.MainActivity}:
java.lang.NullPointerException
05-30 13:30:29.897: E/AndroidRuntime(6850): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-30 13:30:29.897: E/AndroidRuntime(6850): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-30 13:30:29.897: E/AndroidRuntime(6850): at
android.app.ActivityThread.access$600(ActivityThread.java:141)
05-30 13:30:29.897: E/AndroidRuntime(6850): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-30 13:30:29.897: E/AndroidRuntime(6850): at
android.os.Handler.dispatchMessage(Handler.java:99)
05-30 13:30:29.897: E/AndroidRuntime(6850): at
android.os.Looper.loop(Looper.java:137)
05-30 13:30:29.897: E/AndroidRuntime(6850): at
android.app.ActivityThread.main(ActivityThread.java:5041)
05-30 13:30:29.897: E/AndroidRuntime(6850): at
java.lang.reflect.Method.invokeNative(Native Method)
05-30 13:30:29.897: E/AndroidRuntime(6850): at
java.lang.reflect.Method.invoke(Method.java:511)
05-30 13:30:29.897: E/AndroidRuntime(6850): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:79
3)
05-30 13:30:29.897: E/AndroidRuntime(6850): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-30 13:30:29.897: E/AndroidRuntime(6850): at dalvik.system.NativeStart.main(Native Method)
05-30 13:30:29.897: E/AndroidRuntime(6850): Caused by: java.lang.NullPointerException
05-30 13:30:29.897: E/AndroidRuntime(6850): at
android.app.Activity.findViewById(Activity.java:1839)
05-30 13:30:29.897: E/AndroidRuntime(6850): at
com.example.tools.SidebarClass.Sidebar(SidebarClass.java:29)
05-30 13:30:29.897: E/AndroidRuntime(6850): at
com.example.timittest.MainActivity.onCreate(MainActivity.java:42)
05-30 13:30:29.897: E/AndroidRuntime(6850): at
android.app.Activity.performCreate(Activity.java:5104)
05-30 13:30:29.897: E/AndroidRuntime(6850): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-30 13:30:29.897: E/AndroidRuntime(6850): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
添加錯誤日誌。 –
**不要手動實例化活動。使用startActivity(新的意圖(this,SideBarClass.class)); ** – EpicPandaForce
雖然技術上你需要閱讀如何使用'片段'。 – EpicPandaForce