将Intent Extras传递给相同的活动ANDROID的问题

时间:2013-06-20 13:48:31

标签: android android-intent extras

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

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-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)

任何帮助都将不胜感激。

谢谢,

3 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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) { 
    }

答案 2 :(得分:0)

您的代码没有,只有,因为当附加内容为空时,您必须首次设置内容视图。异常上升是因为Activity无法找到Button视图。

pclose(popen("scrapy crawl example -a siteid=$id > /dev/null &", 'r'));