我尝试将一个变量从一个活动发送到另一个但我得到一个错误。我正在使用它,因为我想将一些菜单的一些数据发送到另一个活动,然后根据数据修改文本视图接收。我只附加了一个我要发送的信息,即标题。 我已经附上了我的活动和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
答案 0 :(得分:0)
您正在从Activity构造函数中调用findViewById
。您无法通过调用findViewById
设置活动视图后调用setContentView
,这应该在onCreate
中完成。例如:
class MyActivity extends Activity {
public MyActivity() {
// Do not do anything with views here
}
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myLayout);
// Now you can do stuff with your views
final TextView title_view = (TextView)findViewById(R.id.titleView);
}
}