对同一活动问题的多个不同调用

时间:2014-05-02 05:57:31

标签: android

我有一个简单的应用程序,iam在webview中显示weblinks。 我有两个webViews,一个用于我想以纵向或横向打开的链接

当我只有一个意向调用并且只处理所请求活动中的一个时,我的代码运行良好

现在从我的mainActivty发送意向调用按钮点击 这是代码

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button intra = (Button) findViewById(R.id.buttonIntra);
    intra.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent launchactivity = new Intent(MainActivity.this,
                    potraitWebview.class);
            launchactivity
                    .putExtra("QI",
                            "https://TEST.COM");
            launchactivity.putExtra("viewport", t);
            launchactivity.putExtra("overviewmode", t);

            startActivity(launchactivity);
            finish();
        }
    });

    Button chat = (Button) findViewById(R.id.buttonChat);
    chat.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent launchactivity = new Intent(MainActivity.this,
                    potraitWebview.class);
            launchactivity.putExtra("chat",
                    "http://CHAT.COM");
            launchactivity.putExtra("viewport", t);
            launchactivity.putExtra("overviewmode", t);
            startActivity(launchactivity);
            finish();
        }
    });     

}

现在我使用此代码调用im的活动来处理这些请求并显示网页

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.potraitlayout);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
            R.layout.customtitlebar);
    web = (WebView) findViewById(R.id.webViewPotrait);

    // Handling incoming activity requests

    Bundle intraExtras = getIntent().getExtras();
    if (intraExtras != null) {
        String value = intraExtras.getString("QI");
        viewport = intraExtras.getBoolean("viewport", false);
        overviewmode = intraExtras.getBoolean("overviewmode", false);
        url = value.trim();
        Toast.makeText(getApplicationContext(), value+" "+viewport+" "+overviewmode, Toast.LENGTH_SHORT)
                .show();
    }

    Bundle chat = getIntent().getExtras();
    if (chat != null) {
        String value = chat.getString("chat");
        viewport = chat.getBoolean("viewport", false);
        overviewmode = chat.getBoolean("overviewmode", false);
        url = value.trim();
        Toast.makeText(getApplicationContext(), value+" "+viewport+" "+overviewmode, Toast.LENGTH_SHORT)
                .show();
    }
  

这里的问题是 getIntent()。getExtras(); 如果它对于当前尚未处理的任何活动请求为null,我将使用   称应用程序崩溃

logcat的

    05-02 11:33:49.153: E/AndroidRuntime(19950): FATAL EXCEPTION: main
05-02 11:33:49.153: E/AndroidRuntime(19950): Process: com.android.qintra, PID: 19950
05-02 11:33:49.153: E/AndroidRuntime(19950): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.qintra/com.android.qintra.potraitWebview}: java.lang.NullPointerException
05-02 11:33:49.153: E/AndroidRuntime(19950):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
05-02 11:33:49.153: E/AndroidRuntime(19950):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
05-02 11:33:49.153: E/AndroidRuntime(19950):    at android.app.ActivityThread.access$800(ActivityThread.java:139)
05-02 11:33:49.153: E/AndroidRuntime(19950):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
05-02 11:33:49.153: E/AndroidRuntime(19950):    at android.os.Handler.dispatchMessage(Handler.java:102)
05-02 11:33:49.153: E/AndroidRuntime(19950):    at android.os.Looper.loop(Looper.java:136)
05-02 11:33:49.153: E/AndroidRuntime(19950):    at android.app.ActivityThread.main(ActivityThread.java:5102)
05-02 11:33:49.153: E/AndroidRuntime(19950):    at java.lang.reflect.Method.invokeNative(Native Method)
05-02 11:33:49.153: E/AndroidRuntime(19950):    at java.lang.reflect.Method.invoke(Method.java:515)
05-02 11:33:49.153: E/AndroidRuntime(19950):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
05-02 11:33:49.153: E/AndroidRuntime(19950):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
05-02 11:33:49.153: E/AndroidRuntime(19950):    at dalvik.system.NativeStart.main(Native Method)
05-02 11:33:49.153: E/AndroidRuntime(19950): Caused by: java.lang.NullPointerException
05-02 11:33:49.153: E/AndroidRuntime(19950):    at com.android.qintra.potraitWebview.onCreate(potraitWebview.java:54)
05-02 11:33:49.153: E/AndroidRuntime(19950):    at android.app.Activity.performCreate(Activity.java:5248)
05-02 11:33:49.153: E/AndroidRuntime(19950):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
05-02 11:33:49.153: E/AndroidRuntime(19950):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
05-02 11:33:49.153: E/AndroidRuntime(19950):    ... 11 more

第54行是 http://i.stack.imgur.com/5Ctse.png

1 个答案:

答案 0 :(得分:0)

使用if (intraExtras.containsKey("keyname"))检查bundle是否包含您尝试获取值的密钥。

if (intraExtras != null) {
    if (intraExtras.containsKey("QI")) {
        String value = intraExtras.getString("QI");
    }
    if (intraExtras.containsKey("viewport")) {
        viewport = intraExtras.getBoolean("viewport", false);
    }
    if (intraExtras.containsKey("overviewmode")) {
        overviewmode = intraExtras.getBoolean("overviewmode", false);
    }
    if (intraExtras.containsKey("chat")) {
        String value = intraExtras.getString("chat");
    }
}