我的Android应用程序编译得很好,但是已经停止了应用已停止的消息。我的代码有什么问题?
package com.example.set;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
private Button btn1;
private Button btn2;
private Button btn3;
private Button btn4;
private Button btn5;
Intent Intentlist;
int intentcode1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1=(Button)findViewById(R.id.btn1);
btn2=(Button)findViewById(R.id.btn2);
btn3=(Button)findViewById(R.id.btn3);
btn4=(Button)findViewById(R.id.btn4);
btn5=(Button)findViewById(R.id.btn5);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.btn1:
Intent myAplikasi1 = new Intent(this, Main.class);
startActivity(myAplikasi1);
finish();
break;
case R.id.btn2:
Intent myAplikasi2 = new Intent(this, Credit.class);
startActivity(myAplikasi2);
finish();
break;
case R.id.btn5:
Intent myAplikasi5 = new Intent(this, Galeri.class);
startActivity(myAplikasi5);
finish();
break;
case R.id.btn4:
Intent myAplikasi3 = new Intent(this, sejarah.class);
startActivity(myAplikasi3);
setResult(RESULT_OK);
finish();
break;
case R.id.btn3:
keluar();
finish();
return;
}
}
public void keluar(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Apakah Anda Benar-Benar ingin keluar?")
.setCancelable(false)
.setPositiveButton("Ya",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
MainActivity.this.finish();
}
})
.setNegativeButton("Tidak",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
}).show();
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main,menu);
return true;
}
}
这是我的布局
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/AbsoluteLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/asss"
android:orientation="horizontal"
tools:context=".MainActivity" >
<Button
android:id="@+id/btn2"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_x="27dp"
android:layout_y="340dp"
android:text="credit" />
<Button
android:id="@+id/btn3"
android:layout_width="273dp"
android:layout_height="wrap_content"
android:layout_x="26dp"
android:layout_y="439dp"
android:text="exit" />
<Button
android:id="@+id/btn4"
android:layout_width="271dp"
android:layout_height="wrap_content"
android:layout_x="33dp"
android:layout_y="392dp"
android:text="Sejarah" />
<Button
android:id="@+id/Button5"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_x="27dp"
android:layout_y="289dp"
android:hint="Galeri"
android:text="Galeri" />
<Button
android:id="@+id/btn1"
android:layout_width="274dp"
android:layout_height="wrap_content"
android:layout_x="29dp"
android:layout_y="240dp"
android:hint="Mulai"
android:text="Mulai" />
</AbsoluteLayout>
这一切都发生在我使用干净的项目之后所有错误都消失但是从这个apk开始说停止了工作
logcat的
06-09 08:15:07.398: E/AndroidRuntime(657): FATAL EXCEPTION: main
06-09 08:15:07.398: E/AndroidRuntime(657): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.set/com.example.set.MainActivity}: java.lang.NullPointerException
06-09 08:15:07.398: E/AndroidRuntime(657): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
06-09 08:15:07.398: E/AndroidRuntime(657): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
06-09 08:15:07.398: E/AndroidRuntime(657): at android.app.ActivityThread.access$600(ActivityThread.java:130)
06-09 08:15:07.398: E/AndroidRuntime(657): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
06-09 08:15:07.398: E/AndroidRuntime(657): at android.os.Handler.dispatchMessage(Handler.java:99)
06-09 08:15:07.398: E/AndroidRuntime(657): at android.os.Looper.loop(Looper.java:137)
06-09 08:15:07.398: E/AndroidRuntime(657): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-09 08:15:07.398: E/AndroidRuntime(657): at java.lang.reflect.Method.invokeNative(Native Method)
06-09 08:15:07.398: E/AndroidRuntime(657): at java.lang.reflect.Method.invoke(Method.java:511)
06-09 08:15:07.398: E/AndroidRuntime(657): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-09 08:15:07.398: E/AndroidRuntime(657): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-09 08:15:07.398: E/AndroidRuntime(657): at dalvik.system.NativeStart.main(Native Method)
06-09 08:15:07.398: E/AndroidRuntime(657): Caused by: java.lang.NullPointerException
06-09 08:15:07.398: E/AndroidRuntime(657): at com.example.set.MainActivity.onCreate(MainActivity.java:36)
06-09 08:15:07.398: E/AndroidRuntime(657): at android.app.Activity.performCreate(Activity.java:5008)
06-09 08:15:07.398: E/AndroidRuntime(657): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-09 08:15:07.398: E/AndroidRuntime(657): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
06-09 08:15:07.398: E/AndroidRuntime(657): ... 11 more