2

我正在尝试创建一个 android 应用程序。在其中我创建了两个活动。在第一个活动中,用户通过单击按钮登录,如果他选中让我登录的复选框,那么下次当他在注销后来到其主页时,应用程序会记住他的用户名并通过,如果他没有注销(在第二个活动中按注销按钮)然后他们无法访问主页(第一个活动)。如果他没有注销,那么我想通过使用后退按钮或重新启动应用程序来阻止用户进入主页登录页面。

运行程序->输入登录信息->下一页->注销(运行正常)运行程序->输入登录信息->下一页->按回按按钮->然后按注销按钮->出现错误(强制停止)

我的问题是,当我按下后按按钮,然后在第二个活动中单击注销按钮时,Emmulator 会向我显示应用程序意外关闭和强制停止的按摩。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);
}
 }

我的第二个活动(欢迎用户和注销按钮)代码是=

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}}
4

1 回答 1

2

您有两个不同的值来标识调用的startActivityForResult()处理方式。OnrequestCode用于标识一个startActivityForResult()呼叫,因为您可能有多个呼叫。您用作Requestcd标识符。

第二个是resultCode让您了解结果状态的方法。如果它的值为RESULT_OK,则结果已成功设置在已启动的活动中。如果按下后退按钮,则值为RESULT_CANCELED

在您的情况下,您需要检查这两个值并仅在requestCoderesultCode期望的时候执行。如果值不同,您应该考虑做一些事情来对此做出反应。一种可能性是通知用户他应该执行的任务(例如:选择图片)已被取消。

仅在设置了结果的情况下尝试以下代码来读取意图的数据。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);                
    if (requestCode == Requestcd) {
        if (resultCode == RESULT_OK) {
            String str13 = data.getExtras().getString("txt");
            String str14 = data.getExtras().getString("txt1");
            editText.setText(str13);
            editText1.setText(str14);
        }
    }
}
于 2013-01-24T11:51:47.787 回答