【发布时间】:2015-01-08 02:19:54
【问题描述】:
有人可以帮我一把吗?我已经为这个问题苦苦挣扎了好几个小时。我有一个 Main 和 Start 活动,Main 是带有一堆信息(TextViews)的 Launcher,只要有人触摸屏幕,它就应该转换为 Start 活动。我找到了一些关于如何做到这一点的堆栈溢出信息(在相对布局上设置 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
任何帮助将不胜感激!
【问题讨论】:
-
你的 MainActivity 必须
implement OnTouchListener及其方法。并且不要在没有需要的情况下施放任何东西。
标签: java android eclipse android-layout android-intent