繁体   English   中英

正确解决Intent以修复ActivityNotFoundException

[英]Correctly addressing Intents to fix ActivityNotFoundException

使用以下代码时,我得到一个ActivityNotFoundException:

    public void addListenerOnButton3(){

    button3 = (Button) findViewById(R.id.btnSettings);
    button3.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {    
    Intent intentSettings = new Intent("net.stuffilike.kanaflash.Settings");
    showToast("Settings clicked,");
    try{
    startActivity(intentSettings); 
    }
    catch(Exception e){
        showToastL("Exception" + e);
    }
    return;
}
});
}

足够公平,除了我无法说出它想让我告诉它“活动”在哪里。 这是清单的相关部分:

   <activity
        android:name="net.stuffilike.kanaflash.Settings"
        android:label="@string/settings" >
        <intent-filter>
            <action android:name="android.intent.action.SETTINGS" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter> 
        </activity>

如何确定编译器找到我的Settings.java文件? 哦,我的包裹叫

package net.stuffilike.kanaflash;

尝试这个

@Override
public void onClick(View arg0) {    
    Intent intentSettings = new Intent(X.this,Settings.class);
    showToast("Settings clicked,");
    try{
    startActivity(intentSettings); 
    }
    catch(Exception e){
        showToastL("Exception" + e);
    }
    return;
}
});

用您当前的活动名称替换X。

new Intent(String action)constructor将action作为参数。

根据清单,您正在使用的操作是android.intent.action.SETTINGS

1.所以你的意图应该如下

Intent intentSettings = new Intent("android.intent.action.SETTINGS");

要么

2.您可以使用“活动”名称直接调用活动,

Intent intentSettings = new Intent(this, Settings.class);

要么

3,您也可以定义一个自定义动作,例如net.stuffilike.intent.action.SETTINGS ,然后使用它来创建您的Intent

Intent intentSettings = new Intent("net.stuffilike.intent.action.SETTINGS");

您可以通过2种方式执行此操作。

  1. 使用操作String可以使系统查看哪些Activity可以解决Intent上的操作。 如果有多个Activity可以解决该操作,则用户将获得一个选择来选择他们想要的活动。

     Intent intentSettings = new Intent("android.intent.action.SETTINGS"); 
  2. 直接使用其类打开Activity (仅当两个Activity都在同一应用中时才能执行)。

     Intent intentSettings = new Intent(this, Settings.class); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM