Сначала я хотел, чтобы пользователь вошел в систему, и после входа он не будет возвращаться к экрану входа до выхода из системы.Поэтому я добавил sharedPrefs.Я смог поиграть в логин> mainActivity, а затем выйти из приложения и открыть приложение, которое откроет mainActivity вместо экрана входа в систему.Затем я выйду из системы и вернусь к экрану входа.Как только я нахожусь в экране входа в систему, я выходил из приложения и снова открывал приложение значков, и это выдавало ошибку.Другое дело, когда выход на экран входа в систему и я нажимаю кнопку отмены, это также выдает ошибку.Должно быть, я испортил стек, но не смог найти решение.Я включил logcat ниже, который показывает ту часть, где я больше не мог войти.
Login.java
public class Login extends Activity {
//protected static final SharedPreferences settings = null;
private EditText etUsername;
private Button btnLogin;
private Button btnCancel;
private TextView lblResult;
private String username;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
//replaces the default 'Back' button action
if(keyCode==KeyEvent.KEYCODE_BACK)
{
this.startActivity(new Intent(Login.this,Login.class));
}
return true;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
editor.putString("username", username);
if(prefs.getString("username", null)!=null)
{Intent i = new Intent(getApplicationContext(), Customer.class);
startActivity(i);}
etUsername = (EditText)findViewById(R.id.username);
btnLogin = (Button)findViewById(R.id.login_button);
btnCancel = (Button)findViewById(R.id.cancel_button);
lblResult = (TextView)findViewById(R.id.result);
btnLogin.setOnClickListener(new OnClickListener() {
//@Override
public void onClick(View v) {
// Check Login
String username = etUsername.getText().toString();
if(username.equals("1111")){
lblResult.setText("Login successful.");
Intent i = new Intent(getApplicationContext(), Customer.class);
startActivity(i);
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Close the application
finish();
Logout.java
public class LogoutActivity extends Activity {
private Button btnLogout;
private Button btnCancel;
private TextView lblResult;
private EditText code;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
//replaces the default 'Back' button action
if(keyCode==KeyEvent.KEYCODE_BACK)
{
this.startActivity(new Intent(LogoutActivity.this,Customer.class));
}
return true;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.logout);
code = (EditText)findViewById(R.id.codeout);
btnLogout = (Button) findViewById(R.id.submit);
btnCancel = (Button) findViewById(R.id.cancel);
lblResult = (TextView)findViewById(R.id.result);
btnLogout.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String logout = code.getText().toString();
if (logout.equals("99")){
lblResult.setText("Logout successful");
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = settings.edit();
editor.remove("username");
Intent i = new Intent(getApplicationContext(), Login.class);
startActivity(i);
finish();
} else {
lblResult.setText("Logout failed");
}}});
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), Customer.class);
startActivity(i);
}
Manifest.xml
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Login"
android:noHistory="true"
android:excludeFromRecents="true"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
.............
<activity android:name=".LogoutActivity"
android:noHistory="true"
android:excludeFromRecents="true"
android:label="Logout"></activity>
Customer.java
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class Customer extends ListActivity
{
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
//replaces the default 'Back' button action
if(keyCode==KeyEvent.KEYCODE_BACK)
{
this.startActivity(new Intent(Customer.this,Customer.class));
}
return true;
}
TextView selection;
CustomerListItem[] items = {
new CustomerListItem("Start Trip", StartTripActivity.class),
new CustomerListItem("Clock in", ClockinActivity.class),
new CustomerListItem("Customer Svc", CustomerSvcActivity.class),
new CustomerListItem("Independent Inspection", InspectionActivity.class),
new CustomerListItem("Pick Up", PickUpActivity.class),
new CustomerListItem("Log Out", LogoutActivity.class)};
private TextView resultsTxt;
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.customer);
setListAdapter(new ArrayAdapter<CustomerListItem>(
this, android.R.layout.simple_list_item_1, items));
selection = (TextView) findViewById(R.id.selection);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
final Intent intent = new Intent(this, items[position].getActivity());
startActivityForResult(intent, position);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK)
{
switch (requestCode)
{
case 0:
// TODO: handle the return of the StartTripActivity
break;
case 1:
// TODO: handle the return of the ClockinActivity
break;
case 2:
// TODO: handle the return of the CustomerSvcActivity
case 3:
// TODO: handle the return of the InspectionActivity
break;
case 4:
// TODO: handle the return of the PickUpActivity
break;
case 5:
// TODO: handle the return of the LogoutActivity
break;
default:
break;
}
}
else if (resultCode == RESULT_CANCELED)
{
resultsTxt.setText("Canceled");
}
}
}
Logcat
06-02 07:01:16.981: DEBUG/dalvikvm(981): GC freed 4479 objects / 199288 bytes in 104ms
06-02 07:01:20.632: WARN/KeyCharacterMap(2878): Bad keycharmap - filesize=32
06-02 07:01:20.632: WARN/KeyCharacterMap(2878): Error loading keycharmap file '/system/usr/keychars/qtouch-touchscreen.kcm.bin'. hw.keyboards.0.devname='qtouch-touchscreen'
06-02 07:01:20.632: WARN/KeyCharacterMap(2878): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
06-02 07:01:25.362: DEBUG/DispatchService(946): Handled message = TIMED_SERVICE_UNMASK
06-02 07:01:26.281: INFO/ActivityManager(900): Starting activity: Intent { comp={com.merrill2/com.merrill2.Customer} }
06-02 07:01:26.621: DEBUG/dalvikvm(959): GC freed 1008 objects / 58176 bytes in 180ms
06-02 07:01:26.681: INFO/ActivityManager(900): Displayed activity com.errill2/.Customer: 399 ms
06-02 07:01:30.121: DEBUG/DispatchService(946): Handled message = TIMED_SERVICE_UNMASK
06-02 07:01:38.941: INFO/ActivityManager(900): Starting activity: Intent { comp={com.merrill2/com.merrill2.LogoutActivity} }
06-02 07:01:39.281: INFO/ActivityManager(900): Displayed activity com.merrill2/.LogoutActivity: 330 ms
06-02 07:01:43.071: INFO/ActivityManager(900): Starting activity: Intent { comp={com.merrill2/com.merrill2.Login} }
06-02 07:01:43.631: DEBUG/OpenSSLSessionImpl(900): Freeing OpenSSL session
06-02 07:01:43.640: DEBUG/dalvikvm(900): GC freed 23770 objects / 1125392 bytes in 299ms
06-02 07:01:43.681: INFO/ActivityManager(900): Displayed activity com.merrill2/.Login: 606 ms
06-02 07:01:45.601: INFO/ActivityManager(900): Starting activity: Intent { action=android.intent.action.MAIN categories={android.intent.category.HOME} flags=0x10200000 comp={com.android.launcher/com.android.launcher.Launcher} }
06-02 07:01:45.781: WARN/IInputConnectionWrapper(2878): showStatusIcon on inactive InputConnection
06-02 07:01:47.475: INFO/ActivityManager(900): Starting activity: Intent { action=android.intent.action.MAIN categories={android.intent.category.LAUNCHER} flags=0x10200000 comp={com.merrill2/com.merrill2.Login} }
06-02 07:01:47.501: DEBUG/AndroidRuntime(2878): Shutting down VM
06-02 07:01:47.501: WARN/dalvikvm(2878): threadid=3: thread exiting with uncaught exception (group=0x40013140)
06-02 07:01:47.501: ERROR/AndroidRuntime(2878): Uncaught handler: thread main exiting due to uncaught exception
06-02 07:01:47.521: ERROR/AndroidRuntime(2878): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=5, result=0, data=null} to activity {com.merrill2/com.merrill2.Customer}: java.lang.NullPointerException
06-02 07:01:47.521: ERROR/AndroidRuntime(2878): at android.app.ActivityThread.deliverResults(ActivityThread.java:3026)
06-02 07:01:47.521: ERROR/AndroidRuntime(2878): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3068)
06-02 07:01:47.521: ERROR/AndroidRuntime(2878): at android.app.ActivityThread.access$2300(ActivityThread.java:128)
06-02 07:01:47.521: ERROR/AndroidRuntime(2878): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1739)
06-02 07:01:47.521: ERROR/AndroidRuntime(2878): at android.os.Handler.dispatchMessage(Handler.java:99)
06-02 07:01:47.521: ERROR/AndroidRuntime(2878): at android.os.Looper.loop(Looper.java:123)
06-02 07:01:47.521: ERROR/AndroidRuntime(2878): at android.app.ActivityThread.main(ActivityThread.java:3992)
06-02 07:01:47.521: ERROR/AndroidRuntime(2878): at java.lang.reflect.Method.invokeNative(Native Method)
06-02 07:01:47.521: ERROR/AndroidRuntime(2878): at java.lang.reflect.Method.invoke(Method.java:521)
06-02 07:01:47.521: ERROR/AndroidRuntime(2878): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
06-02 07:01:47.521: ERROR/AndroidRuntime(2878): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
06-02 07:01:47.521: ERROR/AndroidRuntime(2878): at dalvik.system.NativeStart.main(Native Method)
06-02 07:01:47.521: ERROR/AndroidRuntime(2878): Caused by: java.lang.NullPointerException
06-02 07:01:47.521: ERROR/AndroidRuntime(2878): at com.merrill2.Customer.onActivityResult(Customer.java:90)
06-02 07:01:47.521: ERROR/AndroidRuntime(2878): at android.app.Activity.dispatchActivityResult(Activity.java:3595)
06-02 07:01:47.521: ERROR/AndroidRuntime(2878): at android.app.ActivityThread.deliverResults(ActivityThread.java:3022)
06-02 07:01:47.521: ERROR/AndroidRuntime(2878): ... 11 more
06-02 07:01:47.531: INFO/Process(900): Sending signal. PID: 2878 SIG: 3
06-02 07:01:47.551: INFO/dalvikvm(2878): threadid=7: reacting to signal 3
06-02 07:01:47.571: INFO/dalvikvm(2878): Wrote stack trace to '/data/anr/traces.txt'
06-02 07:01:50.482: INFO/Process(2878): Sending signal. PID: 2878 SIG: 9
06-02 07:01:50.501: INFO/ActivityManager(900): Process com.merrill2 (pid 2878) has died.
06-02 07:01:50.501: INFO/WindowManager(900): WIN DEATH: Window{437941d8 com.merrill2/com.merrill2.Customer paused=false}
06-02 07:01:50.501: INFO/WindowManager(900): WIN DEATH: Window{43877980 com.merrill2/com.merrill2.Login paused=true}
06-02 07:01:50.561: WARN/InputManagerService(900): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@435e6fe8
06-02 07:01:54.363: DEBUG/DispatchService(946): Handled message = TIMED_SERVICE_UNMASK
06-02 07:01:55.740: DEBUG/dalvikvm(1225): GC freed 315 objects / 13368 bytes in 90ms
Отладка Logcat
06-02 07:57:53.868: INFO/ActivityManager(900): Starting activity: Intent { comp={com.merrill2/com.merrill2.LogoutActivity} }
06-02 07:57:54.228: INFO/ActivityManager(900): Displayed activity com.merrill2/.LogoutActivity: 360 ms
06-02 07:57:56.968: DEBUG/dalvikvm(959): GC freed 985 objects / 56696 bytes in 86ms
06-02 07:57:57.669: WARN/KeyCharacterMap(2895): Bad keycharmap - filesize=32
06-02 07:57:57.669: WARN/KeyCharacterMap(2895): Error loading keycharmap file '/system/usr/keychars/qtouch-touchscreen.kcm.bin'. hw.keyboards.0.devname='qtouch-touchscreen'
06-02 07:57:57.669: WARN/KeyCharacterMap(2895): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
06-02 07:57:58.628: INFO/ActivityManager(900): Starting activity: Intent { comp={com.merrill2/com.merrill2.Login} }
06-02 07:57:58.768: DEBUG/dalvikvm(2895): GC freed 1710 objects / 109536 bytes in 87ms
06-02 07:57:59.118: INFO/ActivityManager(900): Displayed activity com.merrill2/.Login: 493 ms
06-02 07:58:02.565: DEBUG/DispatchService(946): Handled message = TIMED_SERVICE_UNMASK
06-02 07:58:05.438: INFO/ActivityManager(900): Starting activity: Intent { action=android.intent.action.MAIN categories={android.intent.category.HOME} flags=0x10200000 comp={com.android.launcher/com.android.launcher.Launcher} }
06-02 07:58:05.668: WARN/IInputConnectionWrapper(2895): showStatusIcon on inactive InputConnection
06-02 07:58:06.568: INFO/ActivityManager(900): Starting activity: Intent { action=android.intent.action.MAIN categories={android.intent.category.LAUNCHER} flags=0x10200000 comp={com.merrill2/com.merrill2.Login} }
06-02 07:58:06.588: DEBUG/AndroidRuntime(2895): Shutting down VM
06-02 07:58:06.588: WARN/dalvikvm(2895): threadid=3: thread exiting with uncaught exception (group=0x40013140)
06-02 07:58:06.588: ERROR/AndroidRuntime(2895): Uncaught handler: thread main exiting due to uncaught exception
06-02 07:58:06.598: ERROR/AndroidRuntime(2895): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=5, result=0, data=null} to activity {com.merrill2/com.merrill2.Customer}: java.lang.NullPointerException
06-02 07:58:06.598: ERROR/AndroidRuntime(2895): at android.app.ActivityThread.deliverResults(ActivityThread.java:3026)
06-02 07:58:06.598: ERROR/AndroidRuntime(2895): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3068)
06-02 07:58:06.598: ERROR/AndroidRuntime(2895): at android.app.ActivityThread.access$2300(ActivityThread.java:128)
06-02 07:58:06.598: ERROR/AndroidRuntime(2895): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1739)
06-02 07:58:06.598: ERROR/AndroidRuntime(2895): at android.os.Handler.dispatchMessage(Handler.java:99)
06-02 07:58:06.598: ERROR/AndroidRuntime(2895): at android.os.Looper.loop(Looper.java:123)
06-02 07:58:06.598: ERROR/AndroidRuntime(2895): at android.app.ActivityThread.main(ActivityThread.java:3992)
06-02 07:58:06.598: ERROR/AndroidRuntime(2895): at java.lang.reflect.Method.invokeNative(Native Method)
06-02 07:58:06.598: ERROR/AndroidRuntime(2895): at java.lang.reflect.Method.invoke(Method.java:521)
06-02 07:58:06.598: ERROR/AndroidRuntime(2895): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
06-02 07:58:06.598: ERROR/AndroidRuntime(2895): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
06-02 07:58:06.598: ERROR/AndroidRuntime(2895): at dalvik.system.NativeStart.main(Native Method)
06-02 07:58:06.598: ERROR/AndroidRuntime(2895): Caused by: java.lang.NullPointerException
06-02 07:58:06.598: ERROR/AndroidRuntime(2895): at com.merrill2.Customer.onActivityResult(Customer.java:90)
06-02 07:58:06.598: ERROR/AndroidRuntime(2895): at android.app.Activity.dispatchActivityResult(Activity.java:3595)
06-02 07:58:06.598: ERROR/AndroidRuntime(2895): at android.app.ActivityThread.deliverResults(ActivityThread.java:3022)
06-02 07:58:06.598: ERROR/AndroidRuntime(2895): ... 11 more
06-02 07:58:06.638: INFO/Process(900): Sending signal. PID: 2895 SIG: 3
06-02 07:58:06.638: INFO/dalvikvm(2895): threadid=7: reacting to signal 3
06-02 07:58:06.668: INFO/dalvikvm(2895): Wrote stack trace to '/data/anr/traces.txt'
06-02 07:58:08.600: INFO/Process(2895): Sending signal. PID: 2895 SIG: 9
06-02 07:58:08.618: INFO/ActivityManager(900): Process com.merrill2 (pid 2895) has died.
06-02 07:58:08.628: INFO/WindowManager(900): WIN DEATH: Window{43748a70 com.merrill2/com.merrill2.Customer paused=false}
06-02 07:58:08.638: INFO/ActivityManager(900): Start proc com.merrill2 for activity com.merrill2/.Customer: pid=2932 uid=10053 gids={1006, 3003}
06-02 07:58:08.698: INFO/jdwp(2932): received file descriptor 10 from ADB
06-02 07:58:08.718: WARN/System.err(2932): Can't dispatch DDM chunk 46454154: no handler defined
06-02 07:58:08.728: WARN/System.err(2932): Can't dispatch DDM chunk 4d505251: no handler defined
06-02 07:58:08.918: DEBUG/dalvikvm(900): threadid=13: bogus mon 1+0>0; adjusting
06-02 07:58:08.918: DEBUG/dalvikvm(900): GC freed 24573 objects / 1334712 bytes in 275ms
06-02 07:58:08.938: INFO/WindowManager(900): WIN DEATH: Window{4368b128 com.merrill2/com.merrill2.Login paused=true}
06-02 07:58:09.178: DEBUG/AndroidRuntime(2932): Shutting down VM
06-02 07:58:09.178: WARN/dalvikvm(2932): threadid=3: thread exiting with uncaught exception (group=0x40013140)
06-02 07:58:09.178: ERROR/AndroidRuntime(2932): Uncaught handler: thread main exiting due to uncaught exception
06-02 07:58:09.188: ERROR/AndroidRuntime(2932): java.lang.RuntimeException: Unable to resume activity {com.merrill2/com.merrill2.Customer}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=5, result=0, data=null} to activity {com.merrill2/com.merrill2.Customer}: java.lang.NullPointerException
06-02 07:58:09.188: ERROR/AndroidRuntime(2932): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2653)
06-02 07:58:09.188: ERROR/AndroidRuntime(2932): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2668)
06-02 07:58:09.188: ERROR/AndroidRuntime(2932): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2308)
06-02 07:58:09.188: ERROR/AndroidRuntime(2932): at android.app.ActivityThread.access$1800(ActivityThread.java:128)
06-02 07:58:09.188: ERROR/AndroidRuntime(2932): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1710)
06-02 07:58:09.188: ERROR/AndroidRuntime(2932): at android.os.Handler.dispatchMessage(Handler.java:99)
06-02 07:58:09.188: ERROR/AndroidRuntime(2932): at android.os.Looper.loop(Looper.java:123)
06-02 07:58:09.188: ERROR/AndroidRuntime(2932): at android.app.ActivityThread.main(ActivityThread.java:3992)
06-02 07:58:09.188: ERROR/AndroidRuntime(2932): at java.lang.reflect.Method.invokeNative(Native Method)
06-02 07:58:09.188: ERROR/AndroidRuntime(2932): at java.lang.reflect.Method.invoke(Method.java:521)
06-02 07:58:09.188: ERROR/AndroidRuntime(2932): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
06-02 07:58:09.188: ERROR/AndroidRuntime(2932): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
06-02 07:58:09.188: ERROR/AndroidRuntime(2932): at dalvik.system.NativeStart.main(Native Method)
06-02 07:58:09.188: ERROR/AndroidRuntime(2932): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=5, result=0, data=null} to activity {com.merrill2/com.merrill2.Customer}: java.lang.NullPointerException
06-02 07:58:09.188: ERROR/AndroidRuntime(2932): at android.app.ActivityThread.deliverResults(ActivityThread.java:3026)
06-02 07:58:09.188: ERROR/AndroidRuntime(2932): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2637)
06-02 07:58:09.188: ERROR/AndroidRuntime(2932): ... 12 more
06-02 07:58:09.188: ERROR/AndroidRuntime(2932): Caused by: java.lang.NullPointerException
06-02 07:58:09.188: ERROR/AndroidRuntime(2932): at com.merrill2.Customer.onActivityResult(Customer.java:90)
06-02 07:58:09.188: ERROR/AndroidRuntime(2932): at android.app.Activity.dispatchActivityResult(Activity.java:3595)
06-02 07:58:09.188: ERROR/AndroidRuntime(2932): at android.app.ActivityThread.deliverResults(ActivityThread.java:3022)
06-02 07:58:09.188: ERROR/AndroidRuntime(2932): ... 13 more
06-02 07:58:09.228: INFO/Process(900): Sending signal. PID: 2932 SIG: 3
06-02 07:58:09.228: INFO/dalvikvm(2932): threadid=7: reacting to signal 3
06-02 07:58:09.248: INFO/dalvikvm(2932): Wrote stack trace to '/data/anr/traces.txt'
06-02 07:58:12.538: DEBUG/DispatchService(946): Handled message = TIMED_SERVICE_UNMASK
06-02 07:58:16.577: WARN/ActivityManager(900): Launch timeout has expired, giving up wake lock!
06-02 07:58:18.971: WARN/ActivityManager(900): Activity idle timeout for HistoryRecord{43610e18 {com.merrill2/com.merrill2.Customer}}
06-02 07:58:24.058: DEBUG/dalvikvm(1225): GC freed 270 objects / 10200 bytes in 90ms
06-02 07:58:29.088: DEBUG/dalvikvm(981): GC freed 688 objects / 28808 bytes in 112ms
06-02 07:59:01.598: DEBUG/PowerManagerService(900): setPowerState: mPowerState=3 newState=1 noChangeLights=false