• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

Android的setImageResource导致应用程序崩溃

java 来源:Omar Samara 5次浏览

首先,我很新的android编程,所以这个错误可能是非常简单的东西,我没有注意到。但是每当我打开我的简单应用程序时,它都会从图像开始。当我滑动它应该改变为一个不同的图像,但相反,应用程序崩溃。任何想法,为什么这是?Android的setImageResource导致应用程序崩溃

这里是我的Java代码

package example.course.bananapeeler; 
import android.gesture.Gesture; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.view.MenuItem; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.ImageView; 
import android.view.MotionEvent; 
import android.view.GestureDetector; 
import android.support.v4.view.GestureDetectorCompat; 

public class MainActivity extends ActionBarActivity implements GestureDetector.OnGestureListener, 
     GestureDetector.OnDoubleTapListener{ 

    private ImageView mainPic; 
    private GestureDetectorCompat gestureDetector; 
    private int moneyPerSecond; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     this.gestureDetector = new GestureDetectorCompat(this, this); 
     gestureDetector.setOnDoubleTapListener(this); 

     //ImageView mainPic = (ImageView)findViewById(R.id.mainPic); 
     ImageView mainPic = (ImageView)findViewById(R.id.mainPic); 
     } 

    @Override 
    public boolean onSingleTapConfirmed(MotionEvent motionEvent) { 

     return false; 
    } 

    @Override 
    public boolean onDoubleTap(MotionEvent motionEvent) { 

     return false; 
    } 

    @Override 
    public boolean onDoubleTapEvent(MotionEvent motionEvent) { 

     return false; 
    } 

    @Override 
    public boolean onDown(MotionEvent motionEvent) { 
     return true; 
    } 

    @Override 
    public void onShowPress(MotionEvent motionEvent) { 


    } 

    @Override 
    public boolean onSingleTapUp(MotionEvent motionEvent) { 

     return false; 
    } 

    @Override 
    public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent2, float v, float v2) { 

     return false; 
    } 

    @Override 
    public void onLongPress(MotionEvent motionEvent) { 

    } 

    @Override 
    public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent2, float v, float v2) { 
     /* 
     ImageView img = new ImageView(this); 
     findViewById(R.id.mainPic); 
     */ 
     mainPic.setImageResource(R.drawable.peeledbanana); 

     return true; 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     this.gestureDetector.onTouchEvent(event); 
     return super.onTouchEvent(event); 

    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

的xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"><![CDATA[ 


/"peeledbanana"; 
    android:id="@+id/imageView" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true" /> 
]]> 

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/mainPic" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true" 
    android:src="@drawable/mainbanana" 
    android:contentDescription="@string/mainPic" /> 
</RelativeLayout> 

不知道为什么语法文件的格式很奇怪。这是否导致崩溃?

logcat的错误

05-13 17:16:47.329 2333年至2333年/ example.course.bananapeeler E/InputEventReceiver:异常分派输入事件。 05-13 17:16:47.329 2333年至2333年/ example.course.bananapeeler E /的MessageQueue-JNI:异常中的MessageQueue回调: handleReceiveCallback 05-13 17:16:47.339
2333年至2333年/ example.course。 bananapeeler E /的MessageQueue-JNI: 显示java.lang.NullPointerException:尝试在example.course.bananapeeler.MainActivity.onFling空对象 参考 上调用虚拟方法 ‘无效 android.widget.ImageView.setImageResource(INT)’ (MainActivity.java:88) at android.view.GestureDetector.onTouchEvent(GestureDetector.java:620) at android.support.v4.view.GestureDetectorCompat $ GestureDetectorCompatImplJellybeanMr2.onTouchEvent(GestureDetectorCompat.java:475) at android.support.v4.view.GestureDetectorCompat.onTouchEvent(GestureDetectorCompat.java:538) at example.course.bananapeeler.MainActivity.onTouchEvent(MainActivity.java:102) at android.app.Activity.dispatchTouchEvent(Activity。 java:2869) at android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:59) at com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchTouchEvent(PhoneWindow.java:2529) 在android.view.View.dispatchPointerEvent(View.java:9180) 在android.view.ViewRootImpl $ ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4734) 在android.view.ViewRootImpl $ ViewPostImeInputStage.onProcess(ViewRootImpl.java: 4572) at在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:4149) at android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java: 4115) 在android.view.ViewRootImpl $ AsyncInputStage.forward(ViewRootImpl.java:4229) 在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:4123) 在android.view.ViewRootImpl $ AsyncInputStage.apply( ViewRootImpl.java:4286) 在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:4096) 在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:4149) 在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:4115) at android.view.View RootImpl $ InputStage.apply(ViewRootImpl.java:4123) at android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:4096) at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6600) at android。 view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6484) at android.view.ViewRootImpl。enqueueInputEvent(ViewRootImpl.java:6455) 在android.view.ViewRootImpl $ WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6690) 在android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185) 在android.os.MessageQueue。 nativePollOnce(本地方法) at android.os.MessageQueue.next(MessageQueue.java:143) at android.os.Looper.loop(Looper.java:130) at android.app.ActivityThread.main(ActivityThread.java :5972) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller。运行(ZygoteInit.java:1388) at com.android.internal.os.Zygot eInit.main(ZygoteInit.java:1183)05-13 17:16:47.339 2333-2333/example.course.bananapeeler D/AndroidRuntime:关闭VM 05-13 17:16:47.339
2333-2333/example.course.bananapeeler E/AndroidRuntime:致命 EXCEPTION:main 进程:example.course.bananapeeler,PID:2333 java.lang.NullPointerException:尝试调用虚方法void void android.widget.ImageView.setImageResource(int) ‘on null object reference at example.course.bananapeeler.MainActivity.onFling(MainActivity.java:88) at android.view.GestureDetector.onTouchEvent(GestureDetector.java:620) at android.support.v4.view .GestureDetectorCompat $ GestureDetectorCompatImplJ ellybeanMr2.onTouchEvent(GestureDetectorCompat.java:475) 在android.support.v4.view.GestureDetectorCompat.onTouchEvent(GestureDetectorCompat.java:538) 在example.course.bananapeeler.MainActivity.onTouchEvent(MainActivity.java:102) 在android.app.Activity.dispatchTouchEvent(Activity.java:2869) at android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:59) at com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchTouchEvent(PhoneWindow.java:2529) 在android.view.View.dispatchPointerEvent(View.java:9180) 在android.view.ViewRootImpl $ ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4734) 在android.view。 ViewRootImpl $ ViewPostImeInputStage.onProcess(查看RootImpl.java:4572) at android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:4096) at android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:4149) at android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:4115) at android.view.ViewRootImpl $ AsyncInputStage.forward(ViewRootImpl.java:4229) at android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:4123) at android。 view.ViewRootImpl $ AsyncInputStage.apply(ViewRootImpl.java:4286) at android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:4096) at android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:4149) at android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:4 115) at android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:4123) at android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:4096) at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl。 Java的:6600) 在android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6484) 在android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6455) 在android.view.ViewRootImpl $ WindowInputEventReceiver.onInputEvent(ViewRootImpl。的java:6690) 在android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185) 在android.os.MessageQueue.nativePollOnce(本机方法) 在android.os.MessageQueue.next(的MessageQueue。java:143) at android.os.Looper.loop(Looper.java:130) at android.app.ActivityThread.main(ActivityThread.java:5972) at java.lang.reflect.Method.invoke(Native Method ) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1388) at com.android.internal.os .ZygoteInit.main(ZygoteInit.java:1183)


===========解决方案如下:

您正在本地初始化mainPic而不是类变量。

变化

ImageView mainPic = (ImageView)findViewById(R.id.mainPic); 

mainPic = (ImageView)findViewById(R.id.mainPic); 

版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)