试图让一个按钮打开一个新的活动。我的按钮 xml 如下所示:
<Button
android:id="@+id/buttonStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dip"
android:text=""
android:onClick="viewStyle"
/>
我的按钮功能如下所示:
public void viewStyle(View view) {
// launch new brewery page class
Intent i = new Intent(this, BreweryPage.class);
i.putExtra("myBeerObject", e);
i.setClass(this, StylePage.class);
startActivity(i);
}
我的 stylePage.java 看起来像这样:
public class StylePage extends Activity {
BeerData e;
//get beer details from bundle
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.brewery_page);
//get data from listview
Intent intent = getIntent();
Bundle b = intent.getExtras();
e = b.getParcelable("myBeerObject");
//show title
TextView tv1 = (TextView) findViewById(R.id.styleTitle);
TextView tv2 = (TextView) findViewById(R.id.styleDescription);
//show description
tv1.setText(e.beerStyle);
tv2.setText(e.beerStyleDescription);
}
}
我的错误是空指针异常:
06-20 22:33:48.210: E/AndroidRuntime(29904): FATAL EXCEPTION: main
06-20 22:33:48.210: E/AndroidRuntime(29904): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.beerportfoliopro/com.example.beerportfoliopro.StylePage}: java.lang.NullPointerException
06-20 22:33:48.210: E/AndroidRuntime(29904): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
06-20 22:33:48.210: E/AndroidRuntime(29904): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2357)
06-20 22:33:48.210: E/AndroidRuntime(29904): at android.app.ActivityThread.access$600(ActivityThread.java:153)
06-20 22:33:48.210: E/AndroidRuntime(29904): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
06-20 22:33:48.210: E/AndroidRuntime(29904): at android.os.Handler.dispatchMessage(Handler.java:99)
06-20 22:33:48.210: E/AndroidRuntime(29904): at android.os.Looper.loop(Looper.java:137)
06-20 22:33:48.210: E/AndroidRuntime(29904): at android.app.ActivityThread.main(ActivityThread.java:5226)
06-20 22:33:48.210: E/AndroidRuntime(29904): at java.lang.reflect.Method.invokeNative(Native Method)
06-20 22:33:48.210: E/AndroidRuntime(29904): at java.lang.reflect.Method.invoke(Method.java:511)
06-20 22:33:48.210: E/AndroidRuntime(29904): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
06-20 22:33:48.210: E/AndroidRuntime(29904): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
06-20 22:33:48.210: E/AndroidRuntime(29904): at dalvik.system.NativeStart.main(Native Method)
06-20 22:33:48.210: E/AndroidRuntime(29904): Caused by: java.lang.NullPointerException
06-20 22:33:48.210: E/AndroidRuntime(29904): at com.example.beerportfoliopro.StylePage.onCreate(StylePage.java:28)
06-20 22:33:48.210: E/AndroidRuntime(29904): at android.app.Activity.performCreate(Activity.java:5104)
06-20 22:33:48.210: E/AndroidRuntime(29904): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-20 22:33:48.210: E/AndroidRuntime(29904): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)