【问题标题】:Start activity in a try/catch bloc在 try/catch 块中启动活动
【发布时间】:2011-04-25 16:51:42
【问题描述】:

我只是想知道如何在 try/catch 块中启动 Activity,我做了这个

    public void onReceive(Context context, Intent intent) {
            SipAudioCall incomingCall = null;
            try {
                Intent monIntent = new Intent(this,dialog.class);
                startActivity(monIntent);
                SipAudioCall.Listener listener = new SipAudioCall.Listener() {
@Override
                public void onRinging(SipAudioCall call, SipProfile caller) {

但我有错误:

constructor Intent(IncomingCallReceiver, Class<dialog>) is not defined
Method startActivity(Intent) is undefined for the type IncomingCallReceiver

我想在有电话时显示警报对话框。 我该如何解决这个问题?

非常感谢。 全班:

public class IncomingCallReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        SipAudioCall incomingCall = null;
        try {

            SipAudioCall.Listener listener = new SipAudioCall.Listener() {
                @Override
                public void onRinging(SipAudioCall call, SipProfile caller) {
                    try {
                        call.answerCall(30);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };

            SIPCommunicator wtActivity = (SIPCommunicator) context;

            incomingCall = wtActivity.manager.takeAudioCall(intent, listener);
            incomingCall.answerCall(30);
            incomingCall.startAudio();
            incomingCall.setSpeakerMode(true);
            if(incomingCall.isMuted()) {
                incomingCall.toggleMute();
            }

            wtActivity.call = incomingCall;

            wtActivity.updateStatus(incomingCall);

        } catch (Exception e) {
            if (incomingCall != null) {
                incomingCall.close();
            }
        }
    }

}

【问题讨论】:

  • 你有什么错误?为什么要尝试从 try/catch 块开始?
  • 请说明您遇到的具体错误,也许我们可以帮助您。
  • @Robert @Mayra @Jonathan Engelsma:请看我编辑的第一篇文章。

标签: android android-activity dialog


【解决方案1】:

您面临的问题不是try catch 块,而是您在侦听器实现中启动活动的事实。替代

Intent monIntent = new Intent(this,dialog.class);

Intent monIntent = new Intent(<Name of this class>.this,dialog.class);

 startActivity(monIntent);

<Name of this class>.this.startActivity(monIntent);

&lt;Name of this class&gt; 指的是你编写代码的头类。

【讨论】:

  • 我也有同样的问题。
【解决方案2】:

从外观上看,您正在从 try/catch 块中启动一个新线程。这是不好的做法,因为如果抛出异常,线程可能会失败。

相反,在值之前声明一个变量,并在 catch 语句之前的行上分配给它。

如果引发异常,也分配给它,但值不同。

然后根据变量的成功评估在 try/catch 块之外启动线程。

使用回调来捕获失败的线程,而不是异常。

int x = 0;

try
{
   /* Do some logic here */

   x = 1;        // we have success

}
catch(Exception e)
{
   x = -1;      // failure
}

if (x > 0) 
{
   Intent monIntent = new Intent(this,dialog.class);
                    startActivity(monIntent);

   ...
}

【讨论】:

  • 如果我想开始一个活动怎么办?
  • 没有看到你所有的代码,我不能确定。粘贴整个班级。
  • dialog.class 的类型是什么?
  • 您是否缺少构造函数?您实际上是否在其他地方创建了一个名为 startActivity() 的方法?
  • 我添加了整个班级! dialog.class 启动一个警报对话框。
【解决方案3】:

作为一个新手,我想说一下我的观点:- 尝试放置一些错误消息以找出错误发生的位置。还要检查日志文件。

try catch 示例:-

try
{
    //some code

    //Toast message on success
    Toast.makeText(this, R.string.msgStatusUpdatedSuccefully, Toast.LENGTH_LONG).show();
}
catch (TwitterException e)
{
    //Toast message on failure 
    Toast.makeText(this, R.string.msgStatusUpdateFailed, Toast.LENGTH_LONG).show();
    e.printStackTrace();
}

我会检查我的导入是否适用于我正在使用的所有 api。

您的主要公共类也如下所示来解决未定义的错误:-

public class StatusActivity extends Activity implements OnClickListener{}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多