Android:是否可以在intent-filter中使用@string引用?

时间:2016-02-14 16:24:09

标签: java android android-xml

我是Android的初学者,我遇到了以下问题。

当我尝试从我的主要活动开始第二个活动时

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

在AndroidManifest.xml的第二个活动部分中声明此intent-filter

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

它有效。

但是,如果我尝试在strings.xml中输入字符串“com.whatever”并从那里使用它,就像这样:

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

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

我的程序因异常而崩溃:

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

这是否按设计工作?我应该避免在AndroidManifest中调用“@string”吗?如果是,我在哪里可以找到它的文档?

异常的完整日志(活动和意图名称是原始名称,未按上述方式编辑):

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)

1 个答案:

答案 0 :(得分:0)

明确意图不需要采取行动。如果在应用程序中本地使用,则调用活动不需要在清单中声明Intent过滤器。 使用此:

void main(void)

{
    R_MAIN_UserInit();

uart1Status = R_UART1_Receive(&RxBuf[0],1); // Prime UART1 Rx

while (1U)
{
    //Check if byte received on UART
            if (RxFlag)
            {
                // clear rx flag
                RxFlag = 0U;

                //Echo back the received character
                TxBuf[0] = RxBuf[0];

                //Send TX buffer, and specify how many characters to write
                uart1Status = R_UART1_Send(TxBuf,1);

                // re-Prime UART Rx
                uart1Status = R_UART1_Receive(RxBuf,1);
            }


            //If a character has been transmitted
            if (TxFlag)
            {
                // End of UART2 transmit
                TxFlag = 0U;   // clear tx flag
            }
}

/***********************************************************************************************************************
* Function Name: R_UART1_Receive
* Description  : This function receives UART1 data.
* Arguments    : rx_buf -
*                    receive buffer pointer
*                rx_num -
*                    buffer size
* Return Value : status -
*                    MD_OK or MD_ARGERROR
***********************************************************************************************************************/
MD_STATUS R_UART1_Receive(uint8_t * const rx_buf, uint16_t rx_num)
{
    MD_STATUS status = MD_OK;
if (rx_num < 1U)
{
    status = MD_ARGERROR;
}
else
{
    g_uart1_rx_count = 0U;
    g_uart1_rx_length = rx_num;
    gp_uart1_rx_address = rx_buf;
}

return (status);
}
/***********************************************************************************************************************
* Function Name: R_UART1_Send
* Description  : This function sends UART1 data.
* Arguments    : tx_buf -
*                    transfer buffer pointer
*                tx_num -
*                    buffer size
* Return Value : status -
*                    MD_OK or MD_ARGERROR
***********************************************************************************************************************/
MD_STATUS R_UART1_Send(uint8_t * const tx_buf, uint16_t tx_num)
{
    MD_STATUS status = MD_OK;

if (tx_num < 1U)
{
    status = MD_ARGERROR;
}
else
{
    gp_uart1_tx_address = tx_buf;
    g_uart1_tx_count = tx_num;
    STMK1 = 1U;    /* disable INTST1 interrupt */
    TXD1 = *gp_uart1_tx_address;
    gp_uart1_tx_address++;
    g_uart1_tx_count--;
    STMK1 = 0U;    /* enable INTST1 interrupt */
}

return (status);
}