我是新来的,我想找到一些解决方案。每次我点击登录按钮我总是有一个错误告诉“不幸的是, android停止了“我无法想到我有任何错误。
First.java
package com.example.android;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
import android.content.Intent;
public class First extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.firstlayout);
Button admin = (Button) findViewById (R.id.admin);
admin.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent it = new Intent (First.this, Secondadmin.class);
startActivity(it);
System.exit(0);
}
});
Button sec = (Button) findViewById(R.id.sec);
sec.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent is = new Intent(First.this, Secondsec.class);
startActivity(is);
System.exit(0);
}
});
Button stock = (Button) findViewById (R.id.stock);
stock.setOnClickListener(new OnClickListener () {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent iu= new Intent(First.this,Secondstock.class);
startActivity(iu);
System.exit(0);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.first, menu);
return true;
}
}
firstlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/widget32"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/admin"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:text="ADMIN" />
<Button
android:id="@+id/sec"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:text="SECRETARY" />
<Button
android:id="@+id/stock"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:text="CHECKERS" />
</LinearLayout>
Secondadmin.java
package com.example.android;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.view.View;
import android.content.DialogInterface;
import android.content.Intent;
public class Secondadmin extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.secondlayout);
Button login= (Button) findViewById (R.id.login);
login.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
{
Intent log = new Intent(Secondadmin.this, Thirdadmin.class);
startActivity(log);
}
}
});
Button exit = (Button) findViewById (R.id.exit);
exit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
AlertDialog.Builder builder = new AlertDialog.Builder(Secondadmin.this);
builder.setMessage("Are you sure you want to exit?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener () {
public void onClick(DialogInterface arg0, int arg1) {
Secondadmin.this.finish();
System.exit(0);
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int arg1) {
dialog.cancel();
}
});
AlertDialog alert= builder.create();
alert.show();
}});
Button back= (Button) findViewById (R.id.back);
back.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent is = new Intent (Secondadmin.this, First.class);
startActivity(is);
}
});
}
}
secondlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="91dp"
android:layout_height="wrap_content"
android:text="Password" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword" >
<requestFocus />
</EditText>
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login" />
<Button
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back" />
<Button
android:id="@+id/exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Exit" />
</LinearLayout>
Thirdadmin.java
package com.example.android;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class Thirdadmin extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.thirdlayoutadmin);
Button view= (Button)findViewById(R.id.view);
view.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent id= new Intent(Thirdadmin.this, Fourthview.class);
startActivity(id);
}
});
Button update= (Button)findViewById(R.id.update);
update.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i= new Intent(Thirdadmin.this, Fourthupdate.class);
startActivity(i);
}
});
Button add= (Button) findViewById (R.id.add);
add.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent it= new Intent(Thirdadmin.this, Fourthadmin.class);
startActivity(it);
}
});
Button delete= (Button) findViewById (R.id.delete);
delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent t= new Intent(Thirdadmin.this, Fourthdelete.class);
startActivity(t);
}
});
Button exit = (Button) findViewById (R.id.exit);
exit.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
AlertDialog.Builder builder = new AlertDialog.Builder(Thirdadmin.this);
builder.setMessage("Are you sure you want to exit?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener () {
public void onClick(DialogInterface arg0, int arg1) {
Thirdadmin.this.finish();
System.exit(0);
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int arg1) {
dialog.cancel();
}
});
AlertDialog alert= builder.create();
alert.show();
}});
Button back= (Button) findViewById (R.id.back);
back.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent is = new Intent (Thirdadmin.this, Secondadmin.class);
startActivity(is);
}
});
}
}
thirdlayoutadmin.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/add"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Add" />
<Button
android:id="@+id/view"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="View" />
<Button
android:id="@+id/update"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Update" />
<Button
android:id="@+id/delete"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Delete"/>
</LinearLayout>
机器人:清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.android.First"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Secondadmin"
android:label="@string/app_name">
</activity>
<activity
android:name=".Secondsec"
android:label="@string/app_name">
</activity>
<activity
android:name=".Secondstock"
android:label="@string/app_name">
</activity>
<activity
android:name=".Thirdsec"
android:label="@string/app_name">
</activity>
<activity
android:name=".Thirdstock"
android:label="@string/app_name">
</activity>
<activity
android:name=".Thirdadmin"
android:label="@string/app_name">
</activity>
<activity
android:name=".Fourthadmin"
android:label="@string/app_name">
</activity>
<activity
android:name=".Fourthstock"
android:label="@string/app_name">
</activity>
<activity
android:name=".Fourthdelete"
android:label="@string/app_name">
</activity>
<activity
android:name=".Fourthview"
android:label="@string/app_name">
</activity>
</application>
</manifest>
每次我点击登录按钮我总是有一个错误,告诉“不幸的是,android已停止”我无法想到我有任何错误。
logcat的:
04-15 02:29:54.553: W/dalvikvm(790): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-15 02:29:54.603: E/AndroidRuntime(790): FATAL EXCEPTION: main
04-15 02:29:54.603: E/AndroidRuntime(790): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android/com.example.android.Thirdadmin}: java.lang.NullPointerException
04-15 02:29:54.603: E/AndroidRuntime(790): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-15 02:29:54.603: E/AndroidRuntime(790): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-15 02:29:54.603: E/AndroidRuntime(790): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-15 02:29:54.603: E/AndroidRuntime(790): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-15 02:29:54.603: E/AndroidRuntime(790): at android.os.Handler.dispatchMessage(Handler.java:99)
04-15 02:29:54.603: E/AndroidRuntime(790): at android.os.Looper.loop(Looper.java:137)
04-15 02:29:54.603: E/AndroidRuntime(790): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-15 02:29:54.603: E/AndroidRuntime(790): at java.lang.reflect.Method.invokeNative(Native Method)
04-15 02:29:54.603: E/AndroidRuntime(790): at java.lang.reflect.Method.invoke(Method.java:511)
04-15 02:29:54.603: E/AndroidRuntime(790): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-15 02:29:54.603: E/AndroidRuntime(790): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-15 02:29:54.603: E/AndroidRuntime(790): at dalvik.system.NativeStart.main(Native Method)
04-15 02:29:54.603: E/AndroidRuntime(790): Caused by: java.lang.NullPointerException
04-15 02:29:54.603: E/AndroidRuntime(790): at com.example.android.Thirdadmin.onCreate(Thirdadmin.java:73)
04-15 02:29:54.603: E/AndroidRuntime(790): at android.app.Activity.performCreate(Activity.java:5104)
04-15 02:29:54.603: E/AndroidRuntime(790): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-15 02:29:54.603: E/AndroidRuntime(790): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-15 02:29:54.603: E/AndroidRuntime(790): ... 11 more
答案 0 :(得分:1)
您不应该使用System.exit(0);
。请改用finish()
。