在 Main Activity 中触摸任何地方时无法启动 Activity

Cannot Launch an Activity on touching anywhere in Main Activity

有人能帮帮我吗?几个小时以来,我一直在努力解决这个问题。我有一个 Main 和 Start activity,Main 是带有一堆信息(TextViews)的启动器,它应该在有人触摸屏幕时立即转换到 Start activity。我在堆栈溢出上找到了一些关于如何执行此操作的信息(在相对布局上设置 onTouchListener())但是它对我不起作用,所以每次我尝试使用:

    RelativeLayout layout= (RelativeLayout)findViewById(R.id.relativeLayout1);
    layout.setOnTouchListener(this);

它告诉我将 'this' 转换为 'OnTouchListener',结果如下:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

TextView display1;
TextView display2;
TextView display3;
TextView display4;
ImageView image1;
RelativeLayout layout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.start_screen);
    getActionBar().setTitle("AutoSMS");
    RelativeLayout layout= (RelativeLayout)findViewById(R.id.relativeLayout1);
    layout.setOnTouchListener((OnTouchListener) this);
    display1 = (TextView) findViewById(R.id.textView1);
    display2 = (TextView) findViewById(R.id.textView2);
    display3 = (TextView) findViewById(R.id.textView3);
    display4 = (TextView) findViewById(R.id.textView4);
    image1 = (ImageView) findViewById(R.id.imageView2);
    Intent openStarting = new Intent("android.intent.action.MAIN");
    startActivity(openStarting);
    }

   }

这是清单,以防万一

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.autosms"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="15"
    android:targetSdkVersion="15" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".Start"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.START" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".End"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.END" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

</manifest>

DDMS 给我 ClassCastException,无法将 MainActivity 转换为 ViewOnTouchListener

任何帮助将不胜感激!

您可以将整个 activity 实现为 onTouchListener 或在添加时创建一个新的 onTouchListener。喜欢

public class myActivity extends Activity implements onTouchListener

thisObject.add(new onTouchListener(){

//TODO: Add code

});

没有必要将您的 Activity 转换为 onTouchListener。转换是当你获得某些东西的特定值时,如果它是可能的,就像你有一个 double 并且你想获得它的值作为 int。您只需要制作一个新的 onTouchListener 或将您的 activity 变成一个,因为您没有。