我的应用程序运行正常,没有任何功能的albiet,但由于我添加了对手势的支持(代码改编自教程),它现在只是在运行时崩溃。
package com.mystraldesign.memorable;
import android.app.Activity;
//import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGestureListener;
import android.graphics.Typeface;
import android.os.Bundle;
//import android.widget.RelativeLayout;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.MotionEvent;
import android.view.Window;
import android.widget.TextView;
public class MemorableActivity extends Activity implements OnGestureListener,OnDoubleTapListener
{
//Define text views
private TextView textView1;
private TextView textView2;
private TextView textView3;
private TextView textView4;
//Gesture Detectors
private GestureDetector gTap;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
gTap = new GestureDetector(this,(android.view.GestureDetector.OnGestureListener) this);
//Remove title bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
//Define textView
textView1 = (TextView) findViewById(R.id.textView1);
textView2 = (TextView) findViewById(R.id.textView2);
textView3 = (TextView) findViewById(R.id.textView3);
textView4 = (TextView) findViewById(R.id.textView4);
//Load font file
Typeface type = Typeface.createFromAsset(getAssets(),"fonts/optima.ttf");
//Set various textViews to font
textView1.setTypeface(type);
textView2.setTypeface(type);
textView3.setTypeface(type);
textView4.setTypeface(type);
}
public boolean onTouchEvent(MotionEvent me){
this.gTap.onTouchEvent(me);
return super.onTouchEvent(me);
}
public boolean onDown(MotionEvent e) {
textView1.setText("Down");
return false;
}
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
textView1.setText("Fling");
return false;
}
public void onLongPress(MotionEvent e) {
textView1.setText("Long Press");
}
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
textView1.setText("Scroll");
return false;
}
public void onShowPress(MotionEvent e) {
textView1.setText("Show Press");
}
public boolean onSingleTapUp(MotionEvent e) {
textView1.setText("Sing Tap Up");
return false;
}
public boolean onDoubleTap(MotionEvent e) {
textView1.setText("Double Tap");
return false;
}
public boolean onDoubleTapEvent(MotionEvent e) {
textView1.setText("Double Tap Event");
return false;
}
public boolean onSingleTapConfirmed(MotionEvent e) {
textView1.setText("Single Tap Confirmed");
return false;
}
}
我从每个方法的开头删除了一个@override语句,因为Eclipse用它们声明了一个错误并给了自动修复删除@override语句。
它在public class MemorableActivity extends Activity implements OnGestureListener,OnDoubleTapListener
行上也出错:
Multiple markers at this line
- The type MemorableActivity must implement the inherited abstract method
GestureOverlayView.OnGestureListener.onGestureEnded(GestureOverlayView, MotionEvent)
- The type MemorableActivity must implement the inherited abstract method
GestureOverlayView.OnGestureListener.onGestureCancelled(GestureOverlayView, MotionEvent)
- The type MemorableActivity must implement the inherited abstract method
GestureOverlayView.OnGestureListener.onGestureStarted(GestureOverlayView, MotionEvent)
- The type MemorableActivity must implement the inherited abstract method
GestureOverlayView.OnGestureListener.onGesture(GestureOverlayView, MotionEvent)
它建议修复
Add unimplemented methods
Make MemorableActivity abstract
Rename in file
Rename in workspace
我尝试了前两个,但它仍然崩溃,我不确定第二个和第三个是什么/做什么。
在日志中似乎没有任何内容,但这是控制台的输出
[2012-04-30 03:54:37 - Memorable] ------------------------------
[2012-04-30 03:54:37 - Memorable] Android Launch!
[2012-04-30 03:54:37 - Memorable] adb is running normally.
[2012-04-30 03:54:38 - Memorable] Performing com.mystraldesign.memorable.MemorableActivity activity launch
[2012-04-30 03:54:38 - Memorable] Automatic Target Mode: launching new emulator with compatible AVD 'Memorable'
[2012-04-30 03:54:38 - Memorable] Launching a new emulator with Virtual Device 'Memorable'
[2012-04-30 03:54:44 - Emulator] 2012-04-30 03:54:44.155 emulator-arm[2796:1107] Warning once: This application, or a library it uses, is using NSQuickDrawView, which has been deprecated. Apps should cease use of QuickDraw and move to Quartz.
[2012-04-30 03:54:44 - Memorable] New emulator found: emulator-5554
[2012-04-30 03:54:44 - Memorable] Waiting for HOME ('android.process.acore') to be launched...
[2012-04-30 03:55:36 - Memorable] HOME is up on device 'emulator-5554'
[2012-04-30 03:55:36 - Memorable] Uploading Memorable.apk onto device 'emulator-5554'
[2012-04-30 03:55:41 - Memorable] Installing Memorable.apk...
[2012-04-30 03:55:57 - Memorable] Success!
[2012-04-30 03:55:57 - Memorable] Starting activity com.mystraldesign.memorable.MemorableActivity on device emulator-5554
[2012-04-30 03:56:00 - Memorable] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.mystraldesign.memorable/.MemorableActivity }
编辑:
从Eclipse添加日志
04-30 05:43:25.339: D/AndroidRuntime(333): Shutting down VM
04-30 05:43:25.339: W/dalvikvm(333): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-30 05:43:25.369: E/AndroidRuntime(333): FATAL EXCEPTION: main
04-30 05:43:25.369: E/AndroidRuntime(333): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mystraldesign.memorable/com.mystraldesign.memorable.MemorableActivity}: java.lang.ClassCastException: com.mystraldesign.memorable.MemorableActivity
04-30 05:43:25.369: E/AndroidRuntime(333): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
04-30 05:43:25.369: E/AndroidRuntime(333): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-30 05:43:25.369: E/AndroidRuntime(333): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-30 05:43:25.369: E/AndroidRuntime(333): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-30 05:43:25.369: E/AndroidRuntime(333): at android.os.Handler.dispatchMessage(Handler.java:99)
04-30 05:43:25.369: E/AndroidRuntime(333): at android.os.Looper.loop(Looper.java:123)
04-30 05:43:25.369: E/AndroidRuntime(333): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-30 05:43:25.369: E/AndroidRuntime(333): at java.lang.reflect.Method.invokeNative(Native Method)
04-30 05:43:25.369: E/AndroidRuntime(333): at java.lang.reflect.Method.invoke(Method.java:507)
04-30 05:43:25.369: E/AndroidRuntime(333): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-30 05:43:25.369: E/AndroidRuntime(333): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-30 05:43:25.369: E/AndroidRuntime(333): at dalvik.system.NativeStart.main(Native Method)
04-30 05:43:25.369: E/AndroidRuntime(333): Caused by: java.lang.ClassCastException: com.mystraldesign.memorable.MemorableActivity
04-30 05:43:25.369: E/AndroidRuntime(333): at com.mystraldesign.memorable.MemorableActivity.onCreate(MemorableActivity.java:33)
04-30 05:43:25.369: E/AndroidRuntime(333): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-30 05:43:25.369: E/AndroidRuntime(333): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-30 05:43:25.369: E/AndroidRuntime(333): ... 11 more
答案 0 :(得分:1)
您正在实施一种类型的OnGestureListener
,但正在投射到另一种类型。将Activity类语句更改为:
public class MemorableActivity extends Activity implements android.view.GestureDetector.OnGestureListener,OnDoubleTapListener
然后实施必要的方法,你的崩溃应该停止发生。