Android setImageResource导致应用程序崩溃

时间:2015-05-13 21:04:41

标签: java android android-studio android-imageview

首先,我对android编程很新,所以这个错误可能非常简单,我没有注意到。但每当我打开我的简单应用程序时,它就会从图像开始。当我滑动它应该更改为不同的图像,但应用程序崩溃。任何想法为什么会这样?

这是我的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:尝试调用虚方法&#39; void   android.widget.ImageView.setImageResource(INT)&#39;在null对象上   参考               在example.course.bananapeeler.MainActivity.onFling(MainActivity.java:88)               在android.view.GestureDetector.onTouchEvent(GestureDetector.java:620)               在android.support.v4.view.GestureDetectorCompat $ GestureDetectorCompatImplJellybeanMr2.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)               在android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:59)               在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)               在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:4096)               在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:4149)               在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)               在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:4123)               在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:4096)               在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)               在android.os.Looper.loop(Looper.java:130)               在android.app.ActivityThread.main(ActivityThread.java:5972)               at java.lang.reflect.Method.invoke(Native Method)               在java.lang.reflect.Method.invoke(Method.java:372)               在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1388)               在com.android.internal.os.ZygoteInit.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:致命   例外:主要       过程:example.course.bananapeeler,PID:2333       java.lang.NullPointerException:尝试调用虚方法&#39; void android.widget.ImageView.setImageResource(int)&#39;在null对象上   参考               在example.course.bananapeeler.MainActivity.onFling(MainActivity.java:88)               在android.view.GestureDetector.onTouchEvent(GestureDetector.java:620)               在android.support.v4.view.GestureDetectorCompat $ GestureDetectorCompatImplJellybeanMr2.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)               在android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:59)               在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)               在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:4096)               在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:4149)               在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)               在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:4123)               在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:4096)               在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)               在android.os.Looper.loop(Looper.java:130)               在android.app.ActivityThread.main(ActivityThread.java:5972)               at java.lang.reflect.Method.invoke(Native Method)               在java.lang.reflect.Method.invoke(Method.java:372)               在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1388)               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)

1 个答案:

答案 0 :(得分:3)

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

更改

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

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