我正在尝试实现一个onItemClicklister()
,当单击一个项目时,文本值会从一个活动转移到另一个活动。下面是代码片段。
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String name = ((TextView) view.findViewById(R.id.title)).getText().toString();
String cost = ((TextView) view.findViewById(R.id.artist)).getText().toString();
String description = ((TextView) view.findViewById(R.id.duration)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(KEY_TITLE, name);
in.putExtra(KEY_ARTIST, cost);
in.putExtra(KEY_DURATION, description);
startActivity(in);
}
});
下一个活动
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.single_list_item);
// getting intent data
Intent in = getIntent();
// Get JSON values from previous intent
String name = in.getStringExtra(KEY_TITLE);
String cost = in.getStringExtra(KEY_ARTIST);
String description = in.getStringExtra(KEY_DURATION);
// Displaying all values on the screen
TextView lblName = (TextView) findViewById(R.id.name_label);
TextView lblCost = (TextView) findViewById(R.id.email_label);
TextView lblDesc = (TextView) findViewById(R.id.mobile_label);
lblName.setText(name);
lblCost.setText(cost);
lblDesc.setText(description);
}
但它抛出一个
E/AndroidRuntime( 626): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.eve_haps/com.example.eve_haps.SingleMenuItemActivity}: java.lang.NullPointerException
E/AndroidRuntime( 626): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
E/AndroidRuntime( 626): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
E/AndroidRuntime( 626): at android.app.ActivityThread.access$600(ActivityThread.java:123)
E/AndroidRuntime( 626): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
E/AndroidRuntime( 626): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 626): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 626): at android.app.ActivityThread.main(ActivityThread.java:4424)
E/AndroidRuntime( 626): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 626): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 626): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
E/AndroidRuntime( 626): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
E/AndroidRuntime( 626): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 626): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 626): at com.example.eve_haps.SingleMenuItemActivity.onCreate(SingleMenuItemActivity.java:32)
E/AndroidRuntime( 626): at android.app.Activity.performCreate(Activity.java:4465)
E/AndroidRuntime( 626): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
E/AndroidRuntime( 626): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
我似乎不知道问题是什么。提前感谢您的帮助。