带有view.getLocationOnScreen的NullPointerException

时间:2012-04-16 15:16:18

标签: 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>

1 个答案:

答案 0 :(得分:0)

这就是我想你想要的:

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);
    }

  }

正如您所看到的,我将“down”代码放在一个函数中,将“up”代码放在另一个函数中。然后,在xml中,将android:onClick="onButtonDownClick"添加到向下按钮。对你的向上按钮执行相同的操作(除了'onButtonUpClick')。这将导致在单击时调用这些函数。值得注意的是,这些函数以View为参数。