我是android新手,我正在尝试在其中制作游戏,但是当我在模拟器中运行代码时,它会显示消息“强制关闭”。我正在同一个类中编写启动画面和菜单代码。当我没有在代码中添加 onclicklistner 但当我想在主代码中添加按钮操作时,它运行良好。
这是我的主要课程代码
package com.gillidanda;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.ViewSwitcher;
public class GilliActivity extends Activity implements OnClickListener{
//creates a ViewSwitcher object, to switch between Views
private ViewSwitcher viewSwitcher;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//Initialize a LoadViewTask object and call the execute() method
new LoadViewTask().execute();
// setContentView(R.layout.activity_gilli);
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==R.id.button1){
//Log.d("Amit","Button clicked");
startActivity(new Intent(GilliActivity.this,StartGame.class));
}
}
//To use the AsyncTask, it must be subclassed
private class LoadViewTask extends AsyncTask<Void, Integer, Void>
{
//A TextView object and a ProgressBar object
private TextView tv_progress;
private ProgressBar pb_progressBar;
//Before running code in the separate thread
@Override
protected void onPreExecute()
{
//Initialize the ViewSwitcher object
viewSwitcher = new ViewSwitcher(GilliActivity.this);
/* Initialize the loading screen with data from the
'loadingscreen.xml' layout xml file.
* Add the initialized View to the viewSwitcher.*/
viewSwitcher.addView(ViewSwitcher.inflate(GilliActivity.this,
R.layout.activity_gilli, null));
//Initialize the TextView and ProgressBar instances -
IMPORTANT: call findViewById() from viewSwitcher.
tv_progress = (TextView)
viewSwitcher.findViewById(R.id.tv_progress);
pb_progressBar = (ProgressBar)
viewSwitcher.findViewById(R.id.pb_progressbar);
//Sets the maximum value of the progress bar to 100
pb_progressBar.setMax(100);
//Set ViewSwitcher instance as the current View.
setContentView(viewSwitcher);
}
//The code to be executed in a background thread.
@Override
protected Void doInBackground(Void... params)
{
/* This is just a code that delays the thread execution 4
times,
* during 850 milliseconds and updates the current
progress. This
* is where the code that is going to be executed on a
background
* thread must be placed.
*/
try
{
//Get the current thread's token
synchronized (this)
{
//Initialize an integer (that will act as a
counter) to zero
int counter = 0;
//While the counter is smaller than four
while(counter <= 4)
{
//Wait 850 milliseconds
this.wait(850);
//Increment the counter
counter++;
//Set the current progress.
//This value is going to be passed
to the onProgressUpdate() method.
publishProgress(counter*25);
}
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
return null;
}
//Update the TextView and the progress at progress bar
@Override
protected void onProgressUpdate(Integer... values)
{
//Update the progress at the UI if progress value is
smaller than 100
if(values[0] <= 100)
{
tv_progress.setText("Progress: " +
Integer.toString(values[0]) + "%");
pb_progressBar.setProgress(values[0]);
}
}
//After executing the code in the thread
@Override
protected void onPostExecute(Void result)
{
/* Initialize the application's main interface from the
'main.xml' layout xml file.
* Add the initialized View to the viewSwitcher.*/
viewSwitcher.addView(ViewSwitcher.inflate(GilliActivity.this,
R.layout.mainmenu, null));
//Switch the Views
viewSwitcher.showNext();
}
}
//Override the default back key behavior
@Override
public void onBackPressed()
{
//Emulate the progressDialog.setCancelable(false) behavior
//If the first view is being shown
if(viewSwitcher.getDisplayedChild() == 0)
{
//Do nothing
return;
}
else
{
//Finishes the current Activity
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_gilli, 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"
android:background="@drawable/images" >
<ProgressBar
android:id="@+id/pb_progressbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="18dp" />
<TextView
android:id="@+id/tv_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/pb_progressbar"
android:layout_centerHorizontal="true"
android:shadowColor="#000000"
android:shadowDx="2.0"
android:shadowDy="2.0"
android:shadowRadius="3.0"
android:text="Progress: 0%"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#ffffffff" />
<TextView
android:id="@+id/tv_loadingtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/pb_progressbar"
android:layout_centerHorizontal="true"
android:layout_marginBottom="19dp"
android:text="Loading, please wait..."
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
这是主菜单代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/tile" >
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="81dp"
android:text="Start Game" />
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="15dp"
android:text="Help" />
<Button
android:id="@+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_alignRight="@+id/button2"
android:layout_below="@+id/button2"
android:layout_marginTop="14dp"
android:text="Setting" />
<Button
android:id="@+id/button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button3"
android:layout_alignRight="@+id/button3"
android:layout_below="@+id/button3"
android:layout_marginTop="14dp"
android:text="Credits" />
<Button
android:id="@+id/button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button4"
android:layout_alignRight="@+id/button4"
android:layout_below="@+id/button4"
android:layout_marginTop="16dp"
android:text="Exit" />
</RelativeLayout>
这是清单代码
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gillidanda"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".GilliActivity"
android:label="@string/title_activity_gilli" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".StartGame"
android:label="@string/title_activity_gilli" >
<intent-filter>
<action android:name="android.intent.action.StartGame" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
请给我一些有用的答案。感谢您的回复。
这是我的日志代码
08-13 16:05:02.257: D/AndroidRuntime(450): Shutting down VM
08-13 16:05:02.257: W/dalvikvm(450): threadid=1: thread exiting with uncaught exception
(group=0x40015560)
08-13 16:05:02.276: E/AndroidRuntime(450): FATAL EXCEPTION: main
08-13 16:05:02.276: E/AndroidRuntime(450): java.lang.RuntimeException: Unable to start
activity ComponentInfo{com.gillidanda/com.gillidanda.GilliActivity}:
java.lang.NullPointerException
08-13 16:05:02.276: E/AndroidRuntime(450): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-13 16:05:02.276: E/AndroidRuntime(450): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-13 16:05:02.276: E/AndroidRuntime(450): at
android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-13 16:05:02.276: E/AndroidRuntime(450): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-13 16:05:02.276: E/AndroidRuntime(450): at
android.os.Handler.dispatchMessage(Handler.java:99)
08-13 16:05:02.276: E/AndroidRuntime(450): at android.os.Looper.loop(Looper.java:123)
08-13 16:05:02.276: E/AndroidRuntime(450): at
android.app.ActivityThread.main(ActivityThread.java:3683)
08-13 16:05:02.276: E/AndroidRuntime(450): at
java.lang.reflect.Method.invokeNative(Native Method)
08-13 16:05:02.276: E/AndroidRuntime(450): at
java.lang.reflect.Method.invoke(Method.java:507)
08-13 16:05:02.276: E/AndroidRuntime(450): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-13 16:05:02.276: E/AndroidRuntime(450): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-13 16:05:02.276: E/AndroidRuntime(450): at dalvik.system.NativeStart.main(Native
Method)
08-13 16:05:02.276: E/AndroidRuntime(450): Caused by: java.lang.NullPointerException
08-13 16:05:02.276: E/AndroidRuntime(450): at com.
gillidanda.GilliActivity.onCreate(GilliActivity.java:27)
08-13 16:05:02.276: E/AndroidRuntime(450): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-13 16:05:02.276: E/AndroidRuntime(450): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-13 16:05:02.276: E/AndroidRuntime(450): ... 11 more
08-13 16:05:49.157: D/AndroidRuntime(459): Shutting down VM
08-13 16:05:49.176: W/dalvikvm(459): threadid=1: thread exiting with uncaught exception
(group=0x40015560)
08-13 16:05:49.216: E/AndroidRuntime(459): FATAL EXCEPTION: main
08-13 16:05:49.216: E/AndroidRuntime(459): java.lang.RuntimeException: Unable to start
activity ComponentInfo{com.gillidanda/com.gillidanda.GilliActivity}:
java.lang.NullPointerException
08-13 16:05:49.216: E/AndroidRuntime(459): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-13 16:05:49.216: E/AndroidRuntime(459): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-13 16:05:49.216: E/AndroidRuntime(459): at
android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-13 16:05:49.216: E/AndroidRuntime(459): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-13 16:05:49.216: E/AndroidRuntime(459): at
android.os.Handler.dispatchMessage(Handler.java:99)
08-13 16:05:49.216: E/AndroidRuntime(459): at android.os.Looper.loop(Looper.java:123)
08-13 16:05:49.216: E/AndroidRuntime(459): at
android.app.ActivityThread.main(ActivityThread.java:3683)
08-13 16:05:49.216: E/AndroidRuntime(459): at
java.lang.reflect.Method.invokeNative(Native Method)
08-13 16:05:49.216: E/AndroidRuntime(459): at
java.lang.reflect.Method.invoke(Method.java:507)
08-13 16:05:49.216: E/AndroidRuntime(459): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-13 16:05:49.216: E/AndroidRuntime(459): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-13 16:05:49.216: E/AndroidRuntime(459): at dalvik.system.NativeStart.main(Native
Method)
08-13 16:05:49.216: E/AndroidRuntime(459): Caused by: java.lang.NullPointerException
08-13 16:05:49.216: E/AndroidRuntime(459): at
com.gillidanda.GilliActivity.onCreate(GilliActivity.java:27)
08-13 16:05:49.216: E/AndroidRuntime(459): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-13 16:05:49.216: E/AndroidRuntime(459): at andr
oid.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-13 16:05:49.216: E/AndroidRuntime(459): ... 11 more
08-13 16:05:53.677: I/Process(459): Sending signal. PID: 459 SIG: 9