Android应用程序运行时出现致命异常?
我正在 Android 中开发一个小应用程序,我必须在其中显示图像。我的应用程序中有 2 个图像,但我必须提供通过手指触摸移动图像的功能,因此我在应用程序中使用视图翻转器,并且我还想要放大和缩小功能。
我还为 xoom 输入和输出创建了 3 个类。但是当我运行应用程序时,它会显示第一个图像,并且当 itouch 和翻转它时缩放它,它将移动到第二个图像,之后我将其翻转回来并尝试缩放它我的应用程序将崩溃并在 logcat 中给出以下消息
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): FATAL EXCEPTION: main
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): java.lang.ClassCastException: android.widget.LinearLayout
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at org.example.touch.Touch.onTouch(Touch.java:117)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at android.view.View.dispatchTouchEvent(View.java:3762)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:897)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at android.os.Handler.dispatchMessage(Handler.java:99)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at android.os.Looper.loop(Looper.java:123)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at java.lang.reflect.Method.invokeNative(Native Method)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at java.lang.reflect.Method.invoke(Method.java:521)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at dalvik.system.NativeStart.main(Native Method)
这是我的 java 代码
/***
* Excerpted from "Hello, Android! 3e",
* published by The Pragmatic Bookshelf.
* Copyrights apply to this code. It may not be used to create training material,
* courses, books, articles, and the like. Contact us if you are in doubt.
* We make no guarantees that this code is fit for any purpose.
* Visit http://www.pragmaticprogrammer.com/titles/eband3 for more book information.
***/
package org.example.touch;
import android.app.Activity;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.os.Bundle;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ViewFlipper;
public class Touch extends Activity implements OnTouchListener {
private static final String TAG = "Touch";
// These matrices will be used to move and zoom image
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
// We can be in one of these 3 states
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;
float downXValue;
int counter = 0;
// Remember some things for zooming
PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView view = (ImageView) findViewById(R.id.imageView);
ImageView view1 = (ImageView) findViewById(R.id.imageView1);
view.setOnTouchListener(this);
LinearLayout layMain = (LinearLayout) findViewById(R.id.layout_main);
layMain.setOnTouchListener((OnTouchListener) this);
// ...
// Work around a Cupcake bug
matrix.setTranslate(1f, 1f);
view.setImageMatrix(matrix);
view1.setImageMatrix(matrix);
}
@Override
/* public boolean onTouch(View arg0, MotionEvent arg1) {
// Get the action that was done on this touch event
switch (arg1.getAction())
{
case MotionEvent.ACTION_DOWN:
{
// store the X value when the user's finger was pressed down
downXValue = arg1.getX();
break;
}
case MotionEvent.ACTION_UP:
{
// Get the X value when the user released his/her finger
float currentX = arg1.getX();
// going backwards: pushing stuff to the right
if (downXValue < currentX)
{
// Get a reference to the ViewFlipper
ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
// Set the animation
// vf.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
// Flip!
if(counter > 0){
vf.showPrevious();
counter--;
}
}
// going forwards: pushing stuff to the left
if (downXValue > currentX)
{
// Get a reference to the ViewFlipper
ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
// Set the animation
//vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
// Flip!
if(counter < 1){
vf.showNext();
counter++;
}
}
break;
}
}
// if you return false, these actions will not be recorded
return true;
}*/
public boolean onTouch(View v, MotionEvent rawEvent) {
WrapMotionEvent event = WrapMotionEvent.wrap(rawEvent);
// ...
ImageView view = (ImageView) v;
// Dump touch event to log
dumpEvent(event);
// Handle touch events here...
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
Log.d(TAG, "mode=DRAG");
mode = DRAG;
downXValue = rawEvent.getX();
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
Log.d(TAG, "oldDist=" + oldDist);
if (oldDist > 10f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
Log.d(TAG, "mode=ZOOM");
}
break;
case MotionEvent.ACTION_UP:
{
// Get the X value when the user released his/her finger
float currentX = rawEvent.getX();
// going backwards: pushing stuff to the right
if (downXValue < currentX)
{
// Get a reference to the ViewFlipper
ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
// Set the animation
// vf.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
// Flip!
if(counter > 0){
vf.showPrevious();
counter--;
}
}
// going forwards: pushing stuff to the left
if (downXValue > currentX)
{
// Get a reference to the ViewFlipper
ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
// Set the animation
//vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
// Flip!
if(counter < 1){
vf.showNext();
counter++;
}
}
}
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
Log.d(TAG, "mode=NONE");
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
// ...
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - start.x,
event.getY() - start.y);
}
else if (mode == ZOOM) {
float newDist = spacing(event);
Log.d(TAG, "newDist=" + newDist);
if (newDist > 10f) {
matrix.set(savedMatrix);
float scale = newDist / oldDist;
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
break;
}
view.setImageMatrix(matrix);
return true; // indicate event was handled
}
/** Show an event in the LogCat view, for debugging */
private void dumpEvent(WrapMotionEvent event) {
// ...
String names[] = { "DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE",
"POINTER_DOWN", "POINTER_UP", "7?", "8?", "9?" };
StringBuilder sb = new StringBuilder();
int action = event.getAction();
int actionCode = action & MotionEvent.ACTION_MASK;
sb.append("event ACTION_").append(names[actionCode]);
if (actionCode == MotionEvent.ACTION_POINTER_DOWN
|| actionCode == MotionEvent.ACTION_POINTER_UP) {
sb.append("(pid ").append(
action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
sb.append(")");
}
sb.append("[");
for (int i = 0; i < event.getPointerCount(); i++) {
sb.append("#").append(i);
sb.append("(pid ").append(event.getPointerId(i));
sb.append(")=").append((int) event.getX(i));
sb.append(",").append((int) event.getY(i));
if (i + 1 < event.getPointerCount())
sb.append(";");
}
sb.append("]");
Log.d(TAG, sb.toString());
}
/** Determine the space between the first two fingers */
private float spacing(WrapMotionEvent event) {
// ...
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
/** Calculate the mid point of the first two fingers */
private void midPoint(PointF point, WrapMotionEvent event) {
// ...
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}
}
I am developing an small application in Android in which I have to show images. I have 2 images in my application but i have to make functionality to move the images by finger touch therefore i use the viewflipper in my application and I have also want zoomin and zoomout functionality.
I had also make 3 classes for xoom in and out.But while I am running the application it will show the first image and also zoom it when itouch and flip it it will move to second image after that i flip it back and try to zoom it my application will crash and give the below message in the logcat
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): FATAL EXCEPTION: main
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): java.lang.ClassCastException: android.widget.LinearLayout
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at org.example.touch.Touch.onTouch(Touch.java:117)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at android.view.View.dispatchTouchEvent(View.java:3762)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:897)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at android.os.Handler.dispatchMessage(Handler.java:99)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at android.os.Looper.loop(Looper.java:123)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at java.lang.reflect.Method.invokeNative(Native Method)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at java.lang.reflect.Method.invoke(Method.java:521)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-17 15:22:17.003: ERROR/AndroidRuntime(6770): at dalvik.system.NativeStart.main(Native Method)
here is my java code
/***
* Excerpted from "Hello, Android! 3e",
* published by The Pragmatic Bookshelf.
* Copyrights apply to this code. It may not be used to create training material,
* courses, books, articles, and the like. Contact us if you are in doubt.
* We make no guarantees that this code is fit for any purpose.
* Visit http://www.pragmaticprogrammer.com/titles/eband3 for more book information.
***/
package org.example.touch;
import android.app.Activity;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.os.Bundle;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ViewFlipper;
public class Touch extends Activity implements OnTouchListener {
private static final String TAG = "Touch";
// These matrices will be used to move and zoom image
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
// We can be in one of these 3 states
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;
float downXValue;
int counter = 0;
// Remember some things for zooming
PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView view = (ImageView) findViewById(R.id.imageView);
ImageView view1 = (ImageView) findViewById(R.id.imageView1);
view.setOnTouchListener(this);
LinearLayout layMain = (LinearLayout) findViewById(R.id.layout_main);
layMain.setOnTouchListener((OnTouchListener) this);
// ...
// Work around a Cupcake bug
matrix.setTranslate(1f, 1f);
view.setImageMatrix(matrix);
view1.setImageMatrix(matrix);
}
@Override
/* public boolean onTouch(View arg0, MotionEvent arg1) {
// Get the action that was done on this touch event
switch (arg1.getAction())
{
case MotionEvent.ACTION_DOWN:
{
// store the X value when the user's finger was pressed down
downXValue = arg1.getX();
break;
}
case MotionEvent.ACTION_UP:
{
// Get the X value when the user released his/her finger
float currentX = arg1.getX();
// going backwards: pushing stuff to the right
if (downXValue < currentX)
{
// Get a reference to the ViewFlipper
ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
// Set the animation
// vf.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
// Flip!
if(counter > 0){
vf.showPrevious();
counter--;
}
}
// going forwards: pushing stuff to the left
if (downXValue > currentX)
{
// Get a reference to the ViewFlipper
ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
// Set the animation
//vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
// Flip!
if(counter < 1){
vf.showNext();
counter++;
}
}
break;
}
}
// if you return false, these actions will not be recorded
return true;
}*/
public boolean onTouch(View v, MotionEvent rawEvent) {
WrapMotionEvent event = WrapMotionEvent.wrap(rawEvent);
// ...
ImageView view = (ImageView) v;
// Dump touch event to log
dumpEvent(event);
// Handle touch events here...
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
Log.d(TAG, "mode=DRAG");
mode = DRAG;
downXValue = rawEvent.getX();
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
Log.d(TAG, "oldDist=" + oldDist);
if (oldDist > 10f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
Log.d(TAG, "mode=ZOOM");
}
break;
case MotionEvent.ACTION_UP:
{
// Get the X value when the user released his/her finger
float currentX = rawEvent.getX();
// going backwards: pushing stuff to the right
if (downXValue < currentX)
{
// Get a reference to the ViewFlipper
ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
// Set the animation
// vf.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
// Flip!
if(counter > 0){
vf.showPrevious();
counter--;
}
}
// going forwards: pushing stuff to the left
if (downXValue > currentX)
{
// Get a reference to the ViewFlipper
ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
// Set the animation
//vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
// Flip!
if(counter < 1){
vf.showNext();
counter++;
}
}
}
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
Log.d(TAG, "mode=NONE");
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
// ...
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - start.x,
event.getY() - start.y);
}
else if (mode == ZOOM) {
float newDist = spacing(event);
Log.d(TAG, "newDist=" + newDist);
if (newDist > 10f) {
matrix.set(savedMatrix);
float scale = newDist / oldDist;
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
break;
}
view.setImageMatrix(matrix);
return true; // indicate event was handled
}
/** Show an event in the LogCat view, for debugging */
private void dumpEvent(WrapMotionEvent event) {
// ...
String names[] = { "DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE",
"POINTER_DOWN", "POINTER_UP", "7?", "8?", "9?" };
StringBuilder sb = new StringBuilder();
int action = event.getAction();
int actionCode = action & MotionEvent.ACTION_MASK;
sb.append("event ACTION_").append(names[actionCode]);
if (actionCode == MotionEvent.ACTION_POINTER_DOWN
|| actionCode == MotionEvent.ACTION_POINTER_UP) {
sb.append("(pid ").append(
action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
sb.append(")");
}
sb.append("[");
for (int i = 0; i < event.getPointerCount(); i++) {
sb.append("#").append(i);
sb.append("(pid ").append(event.getPointerId(i));
sb.append(")=").append((int) event.getX(i));
sb.append(",").append((int) event.getY(i));
if (i + 1 < event.getPointerCount())
sb.append(";");
}
sb.append("]");
Log.d(TAG, sb.toString());
}
/** Determine the space between the first two fingers */
private float spacing(WrapMotionEvent event) {
// ...
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
/** Calculate the mid point of the first two fingers */
private void midPoint(PointF point, WrapMotionEvent event) {
// ...
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您将
v
转换为ImageView
时,您会收到ClassCastException
。这意味着用户触摸的视图不是ImageView
。为了避免这种情况,您可以删除
onCreate
方法中的下一行代码 (51, 52):或者在第 116 行的
onTouch
方法中添加额外的检查:When you're casting
v
toImageView
you receiveClassCastException
. This means that user touched the view that is not anImageView
.In order to avoid this you can removed next lines of code (51, 52) in your
onCreate
method:Or add an extra check in
onTouch
method on line 116: