我想使用此代碼兩者之間傳遞數據activities.In所述第一活動IM:應用力向下時,即時通訊使用放/ getExtra
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;
並且在第二即時通訊使用
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);
但即時得到錯誤,我的應用程序的力量down..Where是我錯了嗎?這是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_it em.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>
第24行是museum_item_text .setText(textResourceId); –
@kgr - 然後問題是您的museum.xml佈局中沒有使用id'museum_item_text'的視圖。如果您有特定於配置的佈局目錄(res/layout-land等),請務必檢查museum.xml中是否存在該問題。 –
我也發佈了我的.xml文件...我只是使用簡單的佈局文件夾 –