【问题标题】:findViewById nullPointerException [duplicate]findViewById nullPointerException [重复]
【发布时间】:2016-11-24 09:46:00
【问题描述】:

我在使用返回 null 的 ImageView 时遇到问题。问题似乎出在 findViewById 上,当我尝试为 imageView 使用 getX() 时会导致问题。我已经尝试了所有方法,但仍然无法正常工作。任何帮助将不胜感激!

xml:

ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:src="@drawable/character"
    android:id="@+id/character"
    android:scaleType="fitCenter"
    android:clickable="false"
    android:longClickable="false" /

Java: 包fozard.backuptestapp;

public class Play extends AppCompatActivity {

private GameThread thread;
private float charX=0;
ImageView character;

public void setCharacter(ImageView character) {
    this.character = character;
}

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





}

@Override
protected void onStart(){
    super.onStart();
    thread=new GameThread();
    thread.setRunning(true);
    thread.start();
}

public boolean onTouchEvent(MotionEvent event){
    switch (event.getAction()){
        case MotionEvent.ACTION_DOWN:
            thread.setMovementSpeed(5);
            thread.setMoving(true);
            break;
        case MotionEvent.ACTION_UP:
            thread.setMoving(false);
    }
    return true;
}



public void updateCharPos(float movementSpeed){
    Log.d("Updating", "Should be able to update");
    character = (ImageView)findViewById(R.id.character);
    charX = character.getX();
    System.out.print(charX);
}

}

堆栈跟踪:

11-24 09:32:32.918 16706-16706/fozard.backuptestapp D/Updating: Should be able to update
11-24 09:32:32.923 16706-16706/fozard.backuptestapp D/AndroidRuntime: Shutting down VM
11-24 09:32:32.923 16706-16706/fozard.backuptestapp W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41cbc700)
11-24 09:32:32.933 16706-16706/fozard.backuptestapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      java.lang.NullPointerException
                                                                      at fozard.backuptestapp.Play.updateCharPos(Play.java:71)
                                                                      at fozard.backuptestapp.GameThread$1.run(GameThread.java:88)
                                                                      at android.os.Handler.handleCallback(Handler.java:730)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                      at android.os.Looper.loop(Looper.java:176)
                                                                      at android.app.ActivityThread.main(ActivityThread.java:5419)
                                                                      at java.lang.reflect.Method.invokeNative(Native Method)
                                                                      at java.lang.reflect.Method.invoke(Method.java:525)
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
                                                                      at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

  • 确保您的ImageView id 的“字符”位于activity_play 布局内,否则会抛出 NullPointerExceptoin

标签: java android nullpointerexception imageview


【解决方案1】:
 ImageButton pauseButton = (ImageButton) findViewById(R.id.pausebutton);

改变这个

ImageView pauseButton = (ImageView ) findViewById(R.id.character);

您使用了错误的 ID 和错误的小部件。 首先阅读教程。

【讨论】:

    【解决方案2】:

    从我在你的 xml 中可以看到,你的 ImageView 使用了错误的 id,应该是:

    ImageView pauseButton = (ImageView) findViewById(R.id.character);
    

    您还必须为 xml 和 Javacode (ImageView) 使用相同的类

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-15
      • 2016-02-13
      • 2016-12-06
      • 1970-01-01
      相关资源
      最近更新 更多