简体   繁体   中英

Android: Is it possible to use @string references in intent-filter?

I am a beginner in Android and I have encountered the following problem.

When I try to start a second activity from my main one like this

Intent intent = new Intent("com.myapp.secondActivity");
intent.setAction("com.whatever");
startActivity(intent);

while declaring this intent-filter in the second activity's section in AndroidManifest.xml

<intent-filter>
  <action android:name="com.whatever"/>
  <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

it works.

However, if I try to put the string "com.whatever" in strings.xml and to use it from there, like this:

Intent intent = new Intent("com.myapp.secondActivity");
intent.setAction(getString(R.string.secondActionIntent));
startActivity(intent);

and

<intent-filter>
  <action android:name="@string/secondActionIntent"/>
  <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

my program crashes with an exception:

android.content.ActivityNotFoundException: No Activity found to handle Intent {....}

Is this working as designed? Should I avoid any call to "@string" in AndroidManifest? And if yes, where can I find the documentation for it?

The full log of the exception (the activity and Intent names are the original ones, not edited as above):

FATAL EXCEPTION: main
                                                               Process: com.deni.dechirer, PID: 10564
                                                               java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                   at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:275)
                                                                   at android.view.View.performClick(View.java:4785)
                                                                   at android.view.View$PerformClick.run(View.java:19884)
                                                                   at android.os.Handler.handleCallback(Handler.java:739)
                                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                   at android.os.Looper.loop(Looper.java:135)
                                                                   at android.app.ActivityThread.main(ActivityThread.java:5343)
                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                   at java.lang.reflect.Method.invoke(Method.java:372)
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
                                                                Caused by: java.lang.reflect.InvocationTargetException
                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                   at java.lang.reflect.Method.invoke(Method.java:372)
                                                                   at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
                                                                   at android.view.View.performClick(View.java:4785) 
                                                                   at android.view.View$PerformClick.run(View.java:19884) 
                                                                   at android.os.Handler.handleCallback(Handler.java:739) 
                                                                   at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                   at android.os.Looper.loop(Looper.java:135) 
                                                                   at android.app.ActivityThread.main(ActivityThread.java:5343) 
                                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                                   at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) 
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) 
                                                                Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.deni.dechirer.chargerPhotoBureau (has extras) }
                                                                   at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1872)
                                                                   at android.app.Instrumentation.execStartActivity(Instrumentation.java:1568)
                                                                   at android.app.Activity.startActivityForResult(Activity.java:3755)
                                                                   at android.app.Activity.startActivityForResult(Activity.java:3716)
                                                                   at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:784)
                                                                   at android.app.Activity.startActivity(Activity.java:4036)
                                                                   at android.app.Activity.startActivity(Activity.java:3998)
                                                                   at com.deni.dechirer.MainActivity.envoyerVersBureau(MainActivity.java:120)
                                                                   at com.deni.dechirer.MainActivity.prendrePhoto(MainActivity.java:100)
                                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                                   at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                   at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270) 
                                                                   at android.view.View.performClick(View.java:4785) 
                                                                   at android.view.View$PerformClick.run(View.java:19884) 
                                                                   at android.os.Handler.handleCallback(Handler.java:739) 
                                                                   at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                   at android.os.Looper.loop(Looper.java:135) 
                                                                   at android.app.ActivityThread.main(ActivityThread.java:5343) 
                                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                                   at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) 
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

Action is not required for explicit intent. Called activity need not to have Intent filter declared in manifest if it is used locally in application. Use this:

Intent intent = new Intent(this, SecondActivity.class);
starActivity(intent);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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