我做了一项活动:
这是活动:
Classic g;
LinearLayout gameover;
LinearLayout surface;
LinearLayout screen;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gameover=(LinearLayout) findViewById(R.id.gameover);
setContentView(R.layout.activity_classic);
screen = (LinearLayout)findViewById(R.id.screen);
surface = (LinearLayout)findViewById(R.id.surface);
g= new Classic(this, surface);
surface.addView(g);
screen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(g.getGameover()){
gameover.setVisibility(View.VISIBLE);
g.setGameover(false);
}
}
});
布局:activity_classic
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/screen"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="bottom"
android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
<LinearLayout
android:id="@+id/surface"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1" >
</LinearLayout>
<LinearLayout
android:id="@+id/gameover"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/gameover"
android:orientation="vertical"
android:visibility="gone"
android:layout_gravity="center"
android:gravity="center" >
<TextView
android:id="@+id/score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<EditText
android:id="@+id/name"
android:hint="Enter name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center" >
<Button
android:id="@+id/yes"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save" />
<Button
android:id="@+id/no"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No Tnx, Back to menu" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/stoped"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/gameover"
android:orientation="vertical"
android:visibility="gone"
android:layout_gravity="center"
android:gravity="center" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do you you want to leave?" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center" >
<Button
android:id="@+id/Continue"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Continue Game" />
<Button
android:id="@+id/leave"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Leave to menu" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_gravity="bottom" >
<ImageView
android:id="@+id/right"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignTop="@+id/down"
android:layout_toRightOf="@+id/down"
android:src="@drawable/right" />
<ImageView
android:id="@+id/left"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignTop="@+id/down"
android:layout_toLeftOf="@+id/down"
android:src="@drawable/left" />
<ImageView
android:id="@+id/up"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/right"
android:src="@drawable/up" />
<ImageView
android:id="@+id/down"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="@+id/up"
android:layout_centerHorizontal="true"
android:src="@drawable/down" />
<TextView
android:id="@+id/Back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Go Back To Menu" />
<TextView
android:id="@+id/Pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Pause" />
</RelativeLayout>
</LinearLayout>
类classic
是一款游戏,当您被取消资格时,Boolean
变量gameover
会获得true
。因此,当您点击屏幕上的任意位置时,gameover
layout
visability
将成为visible
。
而应用程序失败,堆栈跟踪:
03-23 08:02:11.850: E/AndroidRuntime(1589): FATAL EXCEPTION: main
03-23 08:02:11.850: E/AndroidRuntime(1589): java.lang.NullPointerException
03-23 08:02:11.850: E/AndroidRuntime(1589): at com.example.snake.ClassicActivity$11.onClick(ClassicActivity.java:204)
03-23 08:02:11.850: E/AndroidRuntime(1589): at android.view.View.performClick(View.java:4204)
03-23 08:02:11.850: E/AndroidRuntime(1589): at android.view.View$PerformClick.run(View.java:17355)
03-23 08:02:11.850: E/AndroidRuntime(1589): at android.os.Handler.handleCallback(Handler.java:725)
03-23 08:02:11.850: E/AndroidRuntime(1589): at android.os.Handler.dispatchMessage(Handler.java:92)
03-23 08:02:11.850: E/AndroidRuntime(1589): at android.os.Looper.loop(Looper.java:137)
03-23 08:02:11.850: E/AndroidRuntime(1589): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-23 08:02:11.850: E/AndroidRuntime(1589): at java.lang.reflect.Method.invokeNative(Native Method)
03-23 08:02:11.850: E/AndroidRuntime(1589): at java.lang.reflect.Method.invoke(Method.java:511)
03-23 08:02:11.850: E/AndroidRuntime(1589): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-23 08:02:11.850: E/AndroidRuntime(1589): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-23 08:02:11.850: E/AndroidRuntime(1589): at dalvik.system.NativeStart.main(Native Method)
我该如何解决?谢谢你的帮助
答案 0 :(得分:0)
更改此
gameover=(LinearLayout) findViewById(R.id.gameover);
setContentView(R.layout.activity_classic);
到
setContentView(R.layout.activity_classic);
gameover=(LinearLayout) findViewById(R.id.gameover);