我要求用户触摸TextView1并需要打印他在TextView2上触摸的'x'轴位置。但是,每当我触摸TextView1时,我的应用程序“不幸停止” - 在设备上进行测试 - 这是班级:Part3
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
public class Part3 extends Activity implements View.OnTouchListener{
private TextView txtView1;
private TextView txtView2;
float x,y;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_part3);
txtView1=(TextView)findViewById(R.id.Part3_txtView1);
txtView2=(TextView)findViewById(R.id.Part3_txtView2);
x=0;y=0;
this.txtView1.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (v.getId()==R.id.Part3_txtView1){
x= event.getX();
y= event.getY();
txtView2.setText((int)x);
return true;
}
return false;
}}
这是activity_part3:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="@+id/Part3_txtView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="30dp"
android:text="@string/Part3_s1"
android:textSize="20sp" />
<TextView
android:id="@+id/Part3_txtView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="x" />
</LinearLayout>
和LogCat:
09-29 15:14:27.043: W/dalvikvm(8454): threadid=1: thread exiting with uncaught exception (group=0x2b542210)
09-29 15:14:27.063: E/AndroidRuntime(8454): FATAL EXCEPTION: main
09-29 15:14:27.063: E/AndroidRuntime(8454): android.content.res.Resources$NotFoundException: String resource ID #0x121
09-29 15:14:27.063: E/AndroidRuntime(8454): at android.content.res.Resources.getText(Resources.java:247)
09-29 15:14:27.063: E/AndroidRuntime(8454): at android.widget.TextView.setText(TextView.java:3495)
09-29 15:14:27.063: E/AndroidRuntime(8454): at bnabaei.touchrecorder.Part3.onTouch(Part3.java:36)
09-29 15:14:27.063: E/AndroidRuntime(8454): at android.view.View.dispatchTouchEvent(View.java:5559)
09-29 15:14:27.063: E/AndroidRuntime(8454): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2058)
09-29 15:14:27.063: E/AndroidRuntime(8454): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1785)
09-29 15:14:27.063: E/AndroidRuntime(8454): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2058)
09-29 15:14:27.063: E/AndroidRuntime(8454): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1785)
09-29 15:14:27.063: E/AndroidRuntime(8454): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2058)
09-29 15:14:27.063: E/AndroidRuntime(8454): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1785)
09-29 15:14:27.063: E/AndroidRuntime(8454): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2058)
09-29 15:14:27.063: E/AndroidRuntime(8454): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1785)
09-29 15:14:27.063: E/AndroidRuntime(8454): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1931)
09-29 15:14:27.063: E/AndroidRuntime(8454): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1390)
09-29 15:14:27.063: E/AndroidRuntime(8454): at android.app.Activity.dispatchTouchEvent(Activity.java:2364)
09-29 15:14:27.063: E/AndroidRuntime(8454): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1879)
09-29 15:14:27.063: E/AndroidRuntime(8454): at android.view.View.dispatchPointerEvent(View.java:5766)
09-29 15:14:27.063: E/AndroidRuntime(8454): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2890)
09-29 15:14:27.063: E/AndroidRuntime(8454): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2466)
09-29 15:14:27.063: E/AndroidRuntime(8454): at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:845)
09-29 15:14:27.063: E/AndroidRuntime(8454): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2475)
09-29 15:14:27.063: E/AndroidRuntime(8454): at android.os.Handler.dispatchMessage(Handler.java:99)
09-29 15:14:27.063: E/AndroidRuntime(8454): at android.os.Looper.loop(Looper.java:137)
09-29 15:14:27.063: E/AndroidRuntime(8454): at android.app.ActivityThread.main(ActivityThread.java:4441)
09-29 15:14:27.063: E/AndroidRuntime(8454): at java.lang.reflect.Method.invokeNative(Native Method)
09-29 15:14:27.063: E/AndroidRuntime(8454): at java.lang.reflect.Method.invoke(Method.java:511)
09-29 15:14:27.063: E/AndroidRuntime(8454): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-29 15:14:27.063: E/AndroidRuntime(8454): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-29 15:14:27.063: E/AndroidRuntime(8454): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
Android Docs for TextView#setText(int)声明它会寻找一个String资源来设置为文本,所以你说的是
x= event.getX();
y= event.getY();
txtView2.setText((int)x);
系统正在查找资源ID为x
您应该将x
投射到String
,以便将值设置为TextView
textView2.setText(String.valueOf(x));