我正在尝试将意图附加信息传递给同一活动,但在单击页面时给出空指针异常。我试图采取额外的意图并使用它来确定要加载的布局文件。
public class stuff extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
if(extras !=null){
String value = extras.getString("name");
if(value.equals("all")){
setContentView(R.layout.allnames);
}
if(value.equals("steve")){
setContentView(R.layout.steve);
}
if(value.equals("mark")){
setContentView(R.layout.mark);
}
}
Button btnSteve = (Button) findViewById(R.id.btnSteve);
btnSteve.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), stuff.class);
myIntent.putExtra("name", "steve");
startActivityForResult(myIntent, 0);
}
});
}
}
Logcat 给出了一个
无法启动活动 ComponentInfo() java.lang.NullPointerException
按下按钮时。我对 Android 开发相当陌生,并且我在网上搜索过,但我找不到任何关于将 Intent Extras 传递回同一活动的信息。
也在清单中声明了此活动。
<intent-filter>
<action android:name=".stuff" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
这是 logcat 错误:
06-20 09:00:48.817: E/AndroidRuntime(32586): java.lang.RuntimeException: Unable to start activity ComponentInfo{app.appname.com/app.appname.com.Stuff}: java.lang.NullPointerException
06-20 09:00:48.817: E/AndroidRuntime(32586): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2136)
06-20 09:00:48.817: E/AndroidRuntime(32586): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2174)
06-20 09:00:48.817: E/AndroidRuntime(32586): at android.app.ActivityThread.access$700(ActivityThread.java:141)
06-20 09:00:48.817: E/AndroidRuntime(32586): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1267)
06-20 09:00:48.817: E/AndroidRuntime(32586): at android.os.Handler.dispatchMessage(Handler.java:99)
06-20 09:00:48.817: E/AndroidRuntime(32586): at android.os.Looper.loop(Looper.java:137)
06-20 09:00:48.817: E/AndroidRuntime(32586): at android.app.ActivityThread.main(ActivityThread.java:5059)
06-20 09:00:48.817: E/AndroidRuntime(32586): at java.lang.reflect.Method.invokeNative(Native Method)
06-20 09:00:48.817: E/AndroidRuntime(32586): at java.lang.reflect.Method.invoke(Method.java:511)
06-20 09:00:48.817: E/AndroidRuntime(32586): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
06-20 09:00:48.817: E/AndroidRuntime(32586): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
06-20 09:00:48.817: E/AndroidRuntime(32586): at dalvik.system.NativeStart.main(Native Method)
06-20 09:00:48.817: E/AndroidRuntime(32586): Caused by: java.lang.NullPointerException
06-20 09:00:48.817: E/AndroidRuntime(32586): at app.appname.com.Stuff.onCreate(AllClasses.java:32)
06-20 09:00:48.817: E/AndroidRuntime(32586): at android.app.Activity.performCreate(Activity.java:5058)
06-20 09:00:48.817: E/AndroidRuntime(32586): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-20 09:00:48.817: E/AndroidRuntime(32586): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
任何援助将不胜感激。
谢谢,