Reputation: 287
I am trying to implement an onItemClicklister()
in which when an item is clicked, the text values are carried over from one activity to another. Below is the code snippet.
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);
}
});
Next Activity
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);
}
But it throws a
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)
I dont seem know what the problem is.Thanks for your help in advance.
Upvotes: 0
Views: 298
Reputation: 109237
Un Comment following code line from SingleMenuItemActivity's onCreate()
.
setContentView(R.layout.single_list_item);
I think your TextViews
are null.
And NullPointerException comes from these lines,
lblName.setText(name);
lblCost.setText(cost);
lblDesc.setText(description);
Upvotes: 3