我正试图移动'长颈鹿'图像视图。似乎从strings.xml调用资源的方式出现了问题,或者它在giraffe.setOnTouchListener(this);上继续作为NullPointerException返回。这是代码:
public class MainGameActivity extends Activity implements OnTouchListener {
float x,y,dx,dy;
ImageView giraffe;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
ImageView giraffe = (ImageView)findViewById(R.id.giraffe);
setContentView(R.layout.activity_main_game);
giraffe.setOnTouchListener(this);
}
public boolean onTouch(View v, MotionEvent me){
switch(me.getAction()){
case MotionEvent.ACTION_DOWN:
{
x = me.getX();
y = me.getY();
dx = x - giraffe.getX();
dy = y - giraffe.getY();
}
break;
case MotionEvent.ACTION_MOVE :
{
giraffe.setX(me.getX()-dx);
giraffe.setY(me.getY()-dy);
}
break;
}
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.activity_main_game, menu);
return true;
}
}
<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"
tools:context=".MainGameActivity" >
<ImageView
android:id="@+id/giraffe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="58dp"
android:contentDescription="@string/giraffe"
android:src="@drawable/giraffeicon" />
<?xml version="1.0" encoding="utf-8"?>
<string name="app_name">Falling Objects</string>
<string name="menu_settings">Settings</string>
<string name="splash_text">Start dodging in:</string>
<string name="splash_number_countdown" > </string>
<string name="title_activity_main_game" > </string>
<string name="version">Version 1.0</string>
<string name="giraffe">Giraffe</string>
<string name="s_and_b_productions">Created by: S&B Productions</string>
<drawable name="giraffe">#F00</drawable>
07-05 19:41:52.006: E/AndroidRuntime(1826): FATAL EXCEPTION: main
07-05 19:41:52.006: E/AndroidRuntime(1826): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.SandBProductions.fallingobjects/com.SandBProductions.fallingobjects.MainGameActivity}: java.lang.NullPointerException
07-05 19:41:52.006: E/AndroidRuntime(1826): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-05 19:41:52.006: E/AndroidRuntime(1826): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-05 19:41:52.006: E/AndroidRuntime(1826): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-05 19:41:52.006: E/AndroidRuntime(1826): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-05 19:41:52.006: E/AndroidRuntime(1826): at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 19:41:52.006: E/AndroidRuntime(1826): at android.os.Looper.loop(Looper.java:137)
07-05 19:41:52.006: E/AndroidRuntime(1826): at android.app.ActivityThread.main(ActivityThread.java:5039)
07-05 19:41:52.006: E/AndroidRuntime(1826): at java.lang.reflect.Method.invokeNative(Native Method)
07-05 19:41:52.006: E/AndroidRuntime(1826): at java.lang.reflect.Method.invoke(Method.java:511)
07-05 19:41:52.006: E/AndroidRuntime(1826): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-05 19:41:52.006: E/AndroidRuntime(1826): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-05 19:41:52.006: E/AndroidRuntime(1826): at dalvik.system.NativeStart.main(Native Method)
07-05 19:41:52.006: E/AndroidRuntime(1826): Caused by: java.lang.NullPointerException
07-05 19:41:52.006: E/AndroidRuntime(1826): at com.SandBProductions.fallingobjects.MainGameActivity.onCreate(MainGameActivity.java:24)
07-05 19:41:52.006: E/AndroidRuntime(1826): at android.app.Activity.performCreate(Activity.java:5104)
07-05 19:41:52.006: E/AndroidRuntime(1826): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-05 19:41:52.006: E/AndroidRuntime(1826): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
答案 0 :(得分:6)
你需要在findViewById之前设置SessionContent,否则活动不知道在哪里看。
答案 1 :(得分:1)
这段代码错了:
ImageView giraffe = (ImageView)findViewById(R.id.giraffe);
setContentView(R.layout.activity_main_game);
在搜索任何布局元素之前,您必须先调用SetContentView()
。这部分应该是
setContentView(R.layout.activity_main_game);
ImageView giraffe = (ImageView)findViewById(R.id.giraffe);
答案 2 :(得分:0)
尝试:
setContentView(R.layout.activity_main_game);
ImageView giraffe = (ImageView)findViewById(R.id.giraffe);
giraffe.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent me){
switch(me.getAction()){
case MotionEvent.ACTION_DOWN:
{
x = me.getX();
y = me.getY();
dx = x - giraffe.getX();
dy = y - giraffe.getY();
}
break;
case MotionEvent.ACTION_MOVE :
{
giraffe.setX(me.getX()-dx);
giraffe.setY(me.getY()-dy);
}
break;
}
return true;
}
});
您需要在调用Imageview
之前设置内容视图