1

对 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>
4

1 回答 1

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

  }

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

于 2012-04-16T15:37:07.157 回答