java - 带有 view.getLocationOnScreen 的 NullPointerException

标签 java android android-widget

对 Android 很陌生,已经研究了一段时间,但找不到答案。我确信有一个简单的解决方案。

我收到一个 NullPointException,它连接到我的 View characterContainer = findViewById(R.id.icon_container);

据我了解,我似乎正在尝试在 onCreate() 方法中实现它,因此布局尚未完全准备好,导致 characterContainer = null.

所以我需要在 onStart()onResume() 中使用它,但我很挣扎。这是我到目前为止的代码:

package harvest.life.game;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;

public class HarvestLifeActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //find buttons on screen
        Button up = (Button)findViewById(R.id.up);
        up.setOnTouchListener(new UpListener());
        Button down = (Button)findViewById(R.id.down);
        down.setOnTouchListener(new UpListener()); // set as UpListener also for test reasons
        Button left = (Button)findViewById(R.id.left); 
        left.setOnTouchListener(new UpListener()); // set as UpListener also for test reasons
        Button right = (Button)findViewById(R.id.right);
        right.setOnTouchListener(new UpListener()); // set as UpListener also for test reasons
    }
    //what up button does 
    private final class UpListener implements OnTouchListener {
        public boolean onTouch(View v, MotionEvent event) {

            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    //as long as up button is pressed it will keep moving character up
                    while (event.getAction() == MotionEvent.ACTION_DOWN)
                    {
                        View characterContainer = findViewById(R.id.icon_container);
                        Drawable walking = getResources().getDrawable(R.drawable.test_circle_red);
                        int startX = characterContainer.getLeft();
                        int startY = characterContainer.getTop();
                        int defaultWidth = characterContainer.getWidth();
                        int defaultHeight = characterContainer.getHeight();

                        //create new position for character 1 pixel closer to the top of screen
                        int newX = startX - 1;
                        int newY = startY;

                        //remove character
                        RelativeLayout view = (RelativeLayout) characterContainer;
                        ViewGroup owner = (ViewGroup) view.getParent();
                        owner.removeView(view);

                        //re make character in new position created above and assign background as walking forwards animation
                        RelativeLayout.LayoutParams characParams = new RelativeLayout.LayoutParams(defaultWidth,defaultHeight);
                        characParams.leftMargin = newY;
                        characParams.topMargin = newX;
                        characterContainer.setLayoutParams(characParams);           
                        characterContainer.setBackgroundDrawable(walking); 
                    }
                    break;

                    // when button is let go of
                case MotionEvent.ACTION_UP:
                    RelativeLayout characterContainer = (RelativeLayout) 
                    findViewById(R.id.icon_container);
                    Drawable standing =
                        getResources().getDrawable(R.drawable.test_circle);
                    //assign background back to standing animation
                    characterContainer.setBackgroundDrawable(standing);
                default:
                    break;
            }
            return true;
        }
    }
    public void onResume() { // what goes here?

    }
}

我是否需要在 onResume 部分中放置某种函数调用,还是将 OnTouchListener 的代码放在这里?

这是我的主要内容:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/game_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="#FFFFFF">

<RelativeLayout
    android:id="@+id/side_bar_right"
    android:layout_width="50dp"
    android:layout_height="fill_parent"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:background="#000000">

    <ImageView
        android:contentDescription="@string/movement_button"
        android:id="@+id/up"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="top"
        android:src="@drawable/up" />

    <ImageView
        android:contentDescription="@string/movement_button"
        android:id="@+id/down"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:src="@drawable/down" />

    <ImageView
        android:contentDescription="@string/movement_button"
        android:id="@+id/right"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:src="@drawable/right" />

</RelativeLayout>

<RelativeLayout
    android:id="@+id/side_bar_left"
    android:layout_width="50dp"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" 
    android:background="#000000">

    <ImageView
        android:contentDescription="@string/movement_button"
        android:id="@+id/left"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:src="@drawable/left" />     

</RelativeLayout>

<RelativeLayout
    android:id="@+id/icon_container"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:background="@drawable/test_circle"  >

</RelativeLayout >

</RelativeLayout>

最佳答案

这就是我认为你想要的:

package harvest.life.game;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;

public class HarvestLifeActivity extends Activity {
/** Called when the activity is first created. */
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //find buttons on screen
        Button up = (Button)findViewById(R.id.up);
        //up.setOnTouchListener(new UpListener());
        Button down = (Button)findViewById(R.id.up);
        //down.setOnTouchListener(new UpListener());
        Button left = (Button)findViewById(R.id.up);
        //left.setOnTouchListener(new UpListener());
        Button right = (Button)findViewById(R.id.up);
        //right.setOnTouchListener(new UpListener()); 
    }

    public void onDownButtonClick(View view){
        while (event.getAction() == MotionEvent.ACTION_DOWN)
        {

             View characterContainer = findViewById(R.id.icon_container);
             Drawable walking = getResources().getDrawable(R.drawable.test_circle_red);
             int startX = characterContainer.getLeft();
             int startY = characterContainer.getTop();
             int defaultWidth = characterContainer.getWidth();
             int defaultHeight = characterContainer.getHeight();

                 //create new position for character 1 pixel closer to the top of screen
                 int newX = startX - 1;
                 int newY = startY;

                 //remove character
                 RelativeLayout view = (RelativeLayout) characterContainer;
                 ViewGroup owner = (ViewGroup) view.getParent();
                 owner.removeView(view);

                 //re make character in new position created above and assign background as walking forwards animation
                 RelativeLayout.LayoutParams characParams = new RelativeLayout.LayoutParams(defaultWidth,defaultHeight);
                 characParams.leftMargin = newY;
                 characParams.topMargin = newX;
                 characterContainer.setLayoutParams(characParams);           
                 characterContainer.setBackgroundDrawable(walking); 
                 }
    }

    public void onUpButtonClicked(View view){
        RelativeLayout characterContainer = (RelativeLayout) 
                findViewById(R.id.icon_container);
        Drawable standing =
                getResources().getDrawable(R.drawable.test_circle);
         //assign background back to standing animation
        characterContainer.setBackgroundDrawable(standing);
    }

  }

如您所见,我将“向下”代码放在一个函数中,将“向上”代码放在另一个函数中。然后,在 xml 中,将 android:onClick="onButtonDownClick" 添加到向下按钮。对向上按钮执行相同的操作(除了“onButtonUpClick”)。这将导致这些函数在单击时被调用。请务必注意,这些函数采用 View 作为参数。

关于java - 带有 view.getLocationOnScreen 的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10176881/

相关文章:

java - inflate() 后文本未出现在 TextView 框中

java - 使用 libgdx 设置新屏幕会使我的游戏崩溃?

android - 动态添加的表在android中不显示

android - 如何从 EditText OnClickListener 中动态选择文本?

android - 更改android Checkbox的大小

java - 如何在排序时忽略某个值,使其始终出现在排序列表的底部

java - 循环连接四个网格

java - 始终应用的方法的第一次模拟

java - 检测开/关按键 Android

android - 调整/缩小 Android 小部件(DatePicker 和 TimePicker)