2

我正在尝试将意图附加信息传递给同一活动,但在单击页面时给出空指针异常。我试图采取额外的意图并使用它来确定要加载的布局文件。

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)

任何援助将不胜感激。

谢谢,

4

3 回答 3

0

尝试这样

 public void onClick(View view) {
                    Intent myIntent = new Intent(Activity.this, stuff.class);
                    myIntent.putExtra("name", "steve");
                    startActivity(myIntent);
                }

清单活动部分的变化

  <activity android:name=".stuff">
        <intent-filter>
         <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
于 2013-06-20T13:51:22.153 回答
0

问题出在您传递给意图 ( view.getContext()) 的上下文中,请尝试以下操作:

Button btnSteve = (Button) findViewById(R.id.btnSteve);
        btnSteve.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(YourActivity.this, stuff.class);
                myIntent.putExtra("name", "steve");
                startActivityForResult(myIntent, 0);
            }

        });

并且要处理从返回的结果stuff,您需要覆盖onActivityResult

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    }
于 2013-06-20T13:51:49.453 回答
0

您的代码没有问题,除非您必须第一次设置内容视图,此时额外内容为空。异常上升是因为 Activity 找不到 Button 视图。

public class stuff extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getIntent().hasExtra("name")) {
            String value = getIntent().getStringExtra("name");
            ...

        } else {
            // For the first time, when Intent has no extras
            setContentView(R.layout.activity_stuff)
        }

        ...
    }
于 2016-06-17T09:58:27.173 回答