【问题标题】:Start an Intent from onTouch从 onTouch 开始一个 Intent
【发布时间】:2014-07-22 15:03:59
【问题描述】:

我尝试从 onTouch 块启动新活动,但应用程序突然终止。我的代码是:

public boolean onTouch(View view, MotionEvent motionEvent) {

    ....
    Intent myIntent = new Intent(context,SecondOne.class);
    myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(myIntent);
    break;
    .....
    return true;
}

【问题讨论】:

  • 请发布你的日志
  • 当它崩溃时你能显示你的日志吗?
  • 请发布您的 logcat 或您收到的错误。
  • “突然终止”过于模糊,无法最终诊断出问题。请发布 logcat,stacktrace,任何东西..

标签: android android-intent android-activity ontouch


【解决方案1】:

我最初没有看到日志的猜测是某些东西要么是 Null,要么你的清单中没有声明第二个活动。

我也想说:

  Intent myIntent = new Intent(FirstOne.this,SecondOne.class);
    myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(myIntent);
    break;

【讨论】:

  • 非常感谢。有一行代码抛出空指针 exc。现在它工作正常
【解决方案2】:

您需要将实际上下文放入意图中。

Intent myIntent = new Intent(ThisClass.this, SecondOne.class);

thisClass 是您编写此代码的任何类。

【讨论】:

    【解决方案3】:

    您的代码似乎有问题。

    1. 您不能在循环外使用break;。所以首先,确保从onTouch() 函数中消除它。
    2. 确保您的 AndroidManifest 文件中有此内容。 <activity android:name=".SecondOne" > </activity>
    3. 确保您的context 变量有效。即如果您的第一个活动名称是First,则其有效的context 应等于First.this

    我假设你的 OnTouchListener 实现没问题。

    您的OnTouchListener 实现

    private final class OT implements OnTouchListener{
    
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            context = First.this;
            Intent intent = new Intent(context, SecondOne.class);
            context.startActivity(intent);
            return true;
        }
    
    }
    

    绑定到您的按钮以开始另一个活动。

    yourButton.setOnTouchListener(new OT());
    

    在您的 AndroidManifest.xml 中

    <activity android:name=".SecondOne" > 
    </activity>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-20
      • 1970-01-01
      相关资源
      最近更新 更多