【问题标题】:Correctly addressing Intents to fix ActivityNotFoundException正确处理 Intents 以修复 ActivityNotFoundException
【发布时间】:2013-08-27 19:59:31
【问题描述】:

我在使用此代码时收到 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 在哪里。以下是 Manifest 的相关部分:

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

【问题讨论】:

    标签: java android android-intent manifest activitynotfoundexception


    【解决方案1】:

    试试这个

    @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 替换为您当前的活动名称 ..

    【讨论】:

    • 就是这样!非常感谢。
    【解决方案2】:

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

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

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

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

    2.可以通过Activity名称直接调用Activity,

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

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

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

    【讨论】:

      【解决方案3】:

      有两种方法可以做到这一点。

      1. 使用String 操作让系统查看Activitys 可以解决Intent 上的操作。如果有多个Activity 可以解决该操作,用户将可以选择他们想要的。

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

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

      【讨论】:

        猜你喜欢
        • 2013-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-13
        • 1970-01-01
        • 2019-11-25
        • 1970-01-01
        相关资源
        最近更新 更多