私は2つのアクティビティを作成するAndroidアプリケーションを作成しようとしています。最初のアクティビティでは、ユーザーはボタンをクリックしてログインし、ログインを保持するチェックボックスをオンにすると、次回ログアウト後にホームページにアクセスしたときに、アプリはユーザー名とパスを記憶し続け、ログアウトしなかった場合( 2 番目のアクティビティでログアウト ボタンを押すと、ホームページ (最初のアクティビティ) にアクセスできなくなります。彼がログアウトしない場合は、戻るボタンを押すかアプリケーションを再起動して、ユーザーがホームログインページにアクセスするのを止めたいと思います。
プログラムを実行 -> ログイン情報を入力 -> 次のページ -> ログアウト (これで問題ありません)
私の問題は、バックプレスボタンを押してから2番目のアクティビティでログアウトボタンをクリックすると、エミュレーターがアプリケーションの予期しない終了のメッセージを表示して強制停止することです.logcatウィンドウのエラー-
ERROR/AndroidRuntime(348): FATAL EXCEPTION: main
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=0, data=null} to activity {com.example/com.example.MyActivity}: java.lang.NullPointerException
私のホームページ(最初の活動コードは)=
package com.example;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
public class MyActivity extends Activity
{
EditText editText,editText1;
CheckBox checkBox;
int Requestcd=1;
String str,str1,str2,struser;
SharedPreferences sharedPreferences;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sharedPreferences = getSharedPreferences("computer" , MODE_PRIVATE);
str = sharedPreferences.getString("key", "hold");
if (str.equalsIgnoreCase("True") || str.equalsIgnoreCase("hold"))
{
editText=(EditText)findViewById(R.id.edit1);
editText1 =(EditText)findViewById(R.id.edit2);
checkBox = (CheckBox)findViewById(R.id.check1);
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//To change body of implemented methods use File | Settings | File Templates.
if(checkBox.isChecked())
{
str1 = editText.getText().toString();
str2 = editText1.getText().toString();
SharedPreferences.Editor editor1 = sharedPreferences.edit();
editor1.putString("key1" , str1);
editor1.putString("key2" , str2);
editor1.commit();
}
struser = editText.getText().toString();
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("key" , "false");
editor.putString("key5" , struser);
Intent intent = new Intent(MyActivity.this , Second.class );
startActivityForResult(intent , Requestcd);
editor.commit();
}
});
}
else
{
Intent intent = new Intent(MyActivity.this , Second.class );
startActivity(intent);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data); //To change body of overridden methods use File | Settings | File Templates.
String str13 = data.getExtras().getString("txt");
String str14 = data.getExtras().getString("txt1");
editText.setText(str13);
editText1.setText(str14);
}
}
私の2番目のアクティビティ(ウェルカムユーザーとログアウトボタン)コードは=
package com.example;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
/**
* Created by IntelliJ IDEA.
* User: devendra
* Date: 18/1/13
* Time: 9:57 AM
* To change this template use File | Settings | File Templates.
*/
public class Second extends Activity{
SharedPreferences sharedPreferences;
String str, str1,str2,str3;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
sharedPreferences = getSharedPreferences("computer" , MODE_PRIVATE);
str3 = sharedPreferences.getString("key5" , "none");
str = sharedPreferences.getString("key" , "null");
Button button=(Button)findViewById(R.id.button2) ;
TextView textView =(TextView)findViewById(R.id.text1);
textView.setText("Welcome"+" " +str3);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//To change body of implemented methods use File | Settings | File Templates.
SharedPreferences.Editor editor1 = sharedPreferences.edit();
editor1.putString("key", "True");
str1 = sharedPreferences.getString("key1" , "Enter your username");
str2 = sharedPreferences.getString("key2","Enter your password");
editor1.remove("key1");
editor1.remove("key2");
Intent intent = new Intent();
intent.putExtra("txt",str1);
intent.putExtra("txt1",str2);
setResult(RESULT_OK, intent);
editor1.commit();
finish();
}
});
}
@Override
public void onBackPressed() {
super.onBackPressed(); //To change body of overridden methods use File | Settings | File Templates.
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("key" , "false");
editor.commit();
System.out.println("you in back press");
if (str.equalsIgnoreCase("false"));
{
Intent intent2 = new Intent(Second.this , Second.class);
startActivity(intent2);
}
finish();
}
}
これは、logcat ウィンドウからの完全なエラーの説明です。
{01-24 16:24:02.810: DEBUG/dalvikvm(382): Debugger has detached; object registry had 1 entries
01-24 16:24:04.702: INFO/dalvikvm(68): Jit: resizing JitTable from 1024 to 2048
01-24 16:24:04.750: INFO/ActivityManager(68): Displayed com.example/.MyActivity: +2s89ms
01-24 16:24:09.940: DEBUG/dalvikvm(147): GC_EXPLICIT freed 125K, 50% free 2990K/5895K, external 5874K/7299K, paused 133ms
01-24 16:24:14.930: DEBUG/dalvikvm(242): GC_EXPLICIT freed 7K, 54% free 2544K/5511K, external 1625K/2137K, paused 92ms
01-24 16:24:20.030: DEBUG/dalvikvm(287): GC_EXPLICIT freed 503K, 55% free 2598K/5703K, external 1625K/2137K, paused 121ms
01-24 16:24:20.230: INFO/ActivityManager(68): Starting: Intent { cmp=com.example/.Second } from pid 390
01-24 16:24:20.850: INFO/ActivityManager(68): Displayed com.example/.Second: +563ms
01-24 16:24:22.800: WARN/KeyCharacterMap(390): No keyboard for id 0
01-24 16:24:22.810: WARN/KeyCharacterMap(390): Using default keymap: /system /usr/keychars/qwerty.kcm.bin
01-24 16:24:22.820: INFO/System.out(390): you in back press
01-24 16:24:22.830: INFO/ActivityManager(68): Starting: Intent { cmp=com.example/.Second } from pid 390
01-24 16:24:22.830: WARN/ActivityManager(68): Duplicate finish request for HistoryRecord{406b1eb0 com.example/.Second}
01-24 16:24:23.520: INFO/ActivityManager(68): Displayed com.example/.Second: +673ms
01-24 16:24:26.340: DEBUG/AndroidRuntime(390): Shutting down VM
01-24 16:24:26.340: WARN/dalvikvm(390): threadid=1: thread exiting with uncaught exception (group=0x40015560)
01-24 16:24:26.370: ERROR/AndroidRuntime(390): FATAL EXCEPTION: main
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=0, data=null} to activity {com.example/com.example.MyActivity}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:2574)
at android.app.ActivityThread.access$2000(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:961)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.MyActivity.onActivityResult(MyActivity.java:69)
at android.app.Activity.dispatchActivityResult(Activity.java:3908)
at android.app.ActivityThread.deliverResults(ActivityThread.java:2528)
... 11 more
01-24 16:24:26.380: WARN/ActivityManager(68): Force finishing activity com.example/.MyActivity
01-24 16:24:26.920: WARN/ActivityManager(68): Activity pause timeout for HistoryRecord{4061a9d0 com.example/.MyActivity}
01-24 16:24:33.801: DEBUG/dalvikvm(320): GC_EXPLICIT freed 323K, 54% free 2539K/5511K, external 1625K/2137K, paused 104ms
01-24 16:24:38.356: WARN/ActivityManager(68): Activity destroy timeout for HistoryRecord{4061a9d0 com.example/.MyActivity}
01-24 16:24:38.702: WARN/ActivityManager(68): Activity destroy timeout for HistoryRecord{406ef998 com.example/.Second}
01-24 16:24:50.600: INFO/Process(390): Sending signal. PID: 390 SIG: 9
01-24 16:24:51.310: INFO/ActivityManager(68): Process com.example (pid 390) has died.
01-24 16:24:51.320: INFO/WindowManager(68): WIN DEATH: Window{4069f500 com.example/com.example.MyActivity paused=false}
01-24 16:24:51.340: ERROR/InputDispatcher(68): channel '4055da70 com.example/com.example.Second (server)' ~ Consumer closed input channel or an error occurred. events=0x8
01-24 16:24:51.340: ERROR/InputDispatcher(68): channel '4055da70 com.example/com.example.Second (server)' ~ Channel is unrecoverably broken and will be disposed!
01-24 16:24:51.450: INFO/WindowManager(68): WIN DEATH: Window{4055da70 com.example/com.example.Second paused=false}}