0
我嘗試從一個活動發送一個變量到另一個,但我得到一個錯誤。我使用這個,因爲我想發送有關菜單的每個項目的一些數據到另一個活動,然後修改根據收到的數據進行文本查看。我已經把它貼只有一個,我要送這是標題信息.. 我已經附上了我的活動和logcat的錯誤:將變量從一個活動傳遞到另一個
MainActivity
Bundle bundle= new Bundle();
bundle.putString("Title", lessons_titles[position]);
Intent intent = new Intent(context, Example1.class);
intent.putExtras(bundle);
context.startActivity(intent);
傳遞活動
Bundle bundle = getIntent().getExtras();
String title = bundle.getString("Title");
title_view.setText(title);
logcat的錯誤
03-12 17:18:31.529: E/AndroidRuntime(1873): FATAL EXCEPTION: main
03-12 17:18:31.529: E/AndroidRuntime(1873): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.test/com.example.test.Example1}: java.lang.NullPointerException
03-12 17:18:31.529: E/AndroidRuntime(1873): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
03-12 17:18:31.529: E/AndroidRuntime(1873): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
03-12 17:18:31.529: E/AndroidRuntime(1873): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-12 17:18:31.529: E/AndroidRuntime(1873): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
03-12 17:18:31.529: E/AndroidRuntime(1873): at android.os.Handler.dispatchMessage(Handler.java:99)
03-12 17:18:31.529: E/AndroidRuntime(1873): at android.os.Looper.loop(Looper.java:137)
03-12 17:18:31.529: E/AndroidRuntime(1873): at android.app.ActivityThread.main(ActivityThread.java:5103)
03-12 17:18:31.529: E/AndroidRuntime(1873): at java.lang.reflect.Method.invokeNative(Native Method)
03-12 17:18:31.529: E/AndroidRuntime(1873): at java.lang.reflect.Method.invoke(Method.java:525)
03-12 17:18:31.529: E/AndroidRuntime(1873): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-12 17:18:31.529: E/AndroidRuntime(1873): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-12 17:18:31.529: E/AndroidRuntime(1873): at dalvik.system.NativeStart.main(Native Method)
03-12 17:18:31.529: E/AndroidRuntime(1873): Caused by: java.lang.NullPointerException
03-12 17:18:31.529: E/AndroidRuntime(1873): at android.app.Activity.findViewById(Activity.java:1853)
03-12 17:18:31.529: E/AndroidRuntime(1873): at com.example.test.Example1.<init>(Example1.java:10)
03-12 17:18:31.529: E/AndroidRuntime(1873): at java.lang.Class.newInstanceImpl(Native Method)
03-12 17:18:31.529: E/AndroidRuntime(1873): at java.lang.Class.newInstance(Class.java:1130)
03-12 17:18:31.529: E/AndroidRuntime(1873): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
03-12 17:18:31.529: E/AndroidRuntime(1873): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
03-12 17:18:31.529: E/AndroidRuntime(1873): ... 11 more
03-12 17:21:55.112: E/AndroidRuntime(2031): FATAL EXCEPTION: main
03-12 17:21:55.112: E/AndroidRuntime(2031): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.test/com.example.test.Example1}: java.lang.NullPointerException
03-12 17:21:55.112: E/AndroidRuntime(2031): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
03-12 17:21:55.112: E/AndroidRuntime(2031): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
03-12 17:21:55.112: E/AndroidRuntime(2031): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-12 17:21:55.112: E/AndroidRuntime(2031): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
03-12 17:21:55.112: E/AndroidRuntime(2031): at android.os.Handler.dispatchMessage(Handler.java:99)
03-12 17:21:55.112: E/AndroidRuntime(2031): at android.os.Looper.loop(Looper.java:137)
03-12 17:21:55.112: E/AndroidRuntime(2031): at android.app.ActivityThread.main(ActivityThread.java:5103)
03-12 17:21:55.112: E/AndroidRuntime(2031): at java.lang.reflect.Method.invokeNative(Native Method)
03-12 17:21:55.112: E/AndroidRuntime(2031): at java.lang.reflect.Method.invoke(Method.java:525)
03-12 17:21:55.112: E/AndroidRuntime(2031): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-12 17:21:55.112: E/AndroidRuntime(2031): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-12 17:21:55.112: E/AndroidRuntime(2031): at dalvik.system.NativeStart.main(Native Method)
03-12 17:21:55.112: E/AndroidRuntime(2031): Caused by: java.lang.NullPointerException
03-12 17:21:55.112: E/AndroidRuntime(2031): at android.app.Activity.findViewById(Activity.java:1853)
03-12 17:21:55.112: E/AndroidRuntime(2031): at com.example.test.Example1.<init>(Example1.java:10)
03-12 17:21:55.112: E/AndroidRuntime(2031): at java.lang.Class.newInstanceImpl(Native Method)
03-12 17:21:55.112: E/AndroidRuntime(2031): at java.lang.Class.newInstance(Class.java:1130)
03-12 17:21:55.112: E/AndroidRuntime(2031): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
03-12 17:21:55.112: E/AndroidRuntime(2031): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
03-12 17:21:55.112: E/AndroidRuntime(2031): ... 11 more
03-12 17:22:02.132: E/AndroidRuntime(2050): FATAL EXCEPTION: main
03-12 17:22:02.132: E/AndroidRuntime(2050): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.test/com.example.test.Example1}: java.lang.NullPointerException
03-12 17:22:02.132: E/AndroidRuntime(2050): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
03-12 17:22:02.132: E/AndroidRuntime(2050): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
03-12 17:22:02.132: E/AndroidRuntime(2050): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-12 17:22:02.132: E/AndroidRuntime(2050): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
03-12 17:22:02.132: E/AndroidRuntime(2050): at android.os.Handler.dispatchMessage(Handler.java:99)
03-12 17:22:02.132: E/AndroidRuntime(2050): at android.os.Looper.loop(Looper.java:137)
03-12 17:22:02.132: E/AndroidRuntime(2050): at android.app.ActivityThread.main(ActivityThread.java:5103)
03-12 17:22:02.132: E/AndroidRuntime(2050): at java.lang.reflect.Method.invokeNative(Native Method)
03-12 17:22:02.132: E/AndroidRuntime(2050): at java.lang.reflect.Method.invoke(Method.java:525)
03-12 17:22:02.132: E/AndroidRuntime(2050): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-12 17:22:02.132: E/AndroidRuntime(2050): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-12 17:22:02.132: E/AndroidRuntime(2050): at dalvik.system.NativeStart.main(Native Method)
03-12 17:22:02.132: E/AndroidRuntime(2050): Caused by: java.lang.NullPointerException
03-12 17:22:02.132: E/AndroidRuntime(2050): at android.app.Activity.findViewById(Activity.java:1853)
03-12 17:22:02.132: E/AndroidRuntime(2050): at com.example.test.Example1.<init>(Example1.java:10)
03-12 17:22:02.132: E/AndroidRuntime(2050): at java.lang.Class.newInstanceImpl(Native Method)
03-12 17:22:02.132: E/AndroidRuntime(2050): at java.lang.Class.newInstance(Class.java:1130)
03-12 17:22:02.132: E/AndroidRuntime(2050): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
03-12 17:22:02.132: E/AndroidRuntime(2050): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
03-12 17:22:02.132: E/AndroidRuntime(2050): ... 11 more
什麼是Example1.java的第10行?它看起來像你可能在調用setContentView之前從你的構造函數調用findViewById,這是無效的。活動視圖尚未設置。 – goto10
TextView title_view =(TextView)findViewById(R.id.titleView); –
這是行10 –