i want to pass data between two activities.In the first activity i m using this code:
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch(position){
case 0:
Intent newActivity = new Intent(museum.this, museum_item.class);
newActivity.putExtra("TXT_RESOURCE",R.string.infomuseum1);
newActivity.putExtra("IMG_RESOURCE",R.drawable.infohistory);
startActivity(newActivity);
break;
and in the second i m using
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.museum);
Bundle extras = getIntent().getExtras();
String textResourceId = extras.getString("TXT_RESOURCE");
int imgResourceId = extras.getInt("IMG_RESOURCE");
TextView museum_item_text = (TextView) findViewById(R.id.museum_item_text);
museum_item_text.setText(textResourceId);
ImageView museum_item_image = (ImageView) findViewById(R.id.museum_item_image);
museum_item_image.setImageResource(imgResourceId);
but i m getting error and my app forces down..Where is my wrong?This is the logcat
12-08 19:35:04.905: E/AndroidRuntime(7689): Uncaught handler: thread main exiting due to uncaught exception
12-08 19:35:04.905: E/AndroidRuntime(7689): java.lang.RuntimeException: Unable to start activity ComponentInfo{kostas.menu.heraklion/kostas.menu.heraklion.museum_item}: java.lang.NullPointerException
12-08 19:35:04.905: E/AndroidRuntime(7689): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
12-08 19:35:04.905: E/AndroidRuntime(7689): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
12-08 19:35:04.905: E/AndroidRuntime(7689): at android.app.ActivityThread.access$1800(ActivityThread.java:112)
12-08 19:35:04.905: E/AndroidRuntime(7689): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
12-08 19:35:04.905: E/AndroidRuntime(7689): at android.os.Handler.dispatchMessage(Handler.java:99)
12-08 19:35:04.905: E/AndroidRuntime(7689): at android.os.Looper.loop(Looper.java:123)
12-08 19:35:04.905: E/AndroidRuntime(7689): at android.app.ActivityThread.main(ActivityThread.java:3948)
12-08 19:35:04.905: E/AndroidRuntime(7689): at java.lang.reflect.Method.invokeNative(Native Method)
12-08 19:35:04.905: E/AndroidRuntime(7689): at java.lang.reflect.Method.invoke(Method.java:521)
12-08 19:35:04.905: E/AndroidRuntime(7689): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
12-08 19:35:04.905: E/AndroidRuntime(7689): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
12-08 19:35:04.905: E/AndroidRuntime(7689): at dalvik.system.NativeStart.main(Native Method)
12-08 19:35:04.905: E/AndroidRuntime(7689): Caused by: java.lang.NullPointerException
12-08 19:35:04.905: E/AndroidRuntime(7689): at kostas.menu.heraklion.museum_item.onCreate(museum_item.java:24)
12-08 19:35:04.905: E/AndroidRuntime(7689): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
12-08 19:35:04.905: E/AndroidRuntime(7689): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
12-08 19:35:04.905: E/AndroidRuntime(7689): ... 11 more
museum_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/museum_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/museum_item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
It would help if you pointed out which line of your code (line 24, according to the logcat) is causing the problem. My guess is that you aren’t finding one of the views
museum_item_textormuseum_item_image, the corresponding view variable is being set to null by the call tofindViewById, and then you’re getting a NPE on the next line.