将数据保存到 java android 中的全局变量

Saving data to a global variable in java android

我试图通过这样做将数据保存到我的全局变量中:

public static String UnitCode;
public static String RDate;
public static String AArea;

    final EditText unitCode = (EditText) findViewById(R.id.editText1);
    final EditText reservationDate = (EditText) findViewById(R.id.editText2);
    final EditText Area = (EditText) findViewById(R.id.editText3);

            UnitCode = unitCode.getText().toString();
            RDate = reservationDate.getText().toString();
            AArea = Area.getText().toString();

这是我的 ReservationActivity.class.

但是每当我在 ReservationProcessActivity 上访问它时 class:

    UnitCode = ReservationActivity.UnitCode.toString();
    RDate = ReservationActivity.RDate.toString();
    Area = ReservationActivity.AArea.toString();

我的 android 应用程序突然崩溃,只有当我取消 ReservationProcessActivity 中的部分注释时,如果我注释它,它工作正常。我做错了吗?

这是 logcat 错误:

02-06 11:50:10.956: E/AndroidRuntime(16482): FATAL EXCEPTION: main
02-06 11:50:10.956: E/AndroidRuntime(16482): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.appletest/com.example.appletest.ReservationProcessActivity}: java.lang.NullPointerException
02-06 11:50:10.956: E/AndroidRuntime(16482):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
02-06 11:50:10.956: E/AndroidRuntime(16482):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
02-06 11:50:10.956: E/AndroidRuntime(16482):    at android.app.ActivityThread.access0(ActivityThread.java:168)
02-06 11:50:10.956: E/AndroidRuntime(16482):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
02-06 11:50:10.956: E/AndroidRuntime(16482):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-06 11:50:10.956: E/AndroidRuntime(16482):    at android.os.Looper.loop(Looper.java:137)
02-06 11:50:10.956: E/AndroidRuntime(16482):    at android.app.ActivityThread.main(ActivityThread.java:5493)
02-06 11:50:10.956: E/AndroidRuntime(16482):    at java.lang.reflect.Method.invokeNative(Native Method)
02-06 11:50:10.956: E/AndroidRuntime(16482):    at java.lang.reflect.Method.invoke(Method.java:525)
02-06 11:50:10.956: E/AndroidRuntime(16482):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
02-06 11:50:10.956: E/AndroidRuntime(16482):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
02-06 11:50:10.956: E/AndroidRuntime(16482):    at dalvik.system.NativeStart.main(Native Method)
02-06 11:50:10.956: E/AndroidRuntime(16482): Caused by: java.lang.NullPointerException
02-06 11:50:10.956: E/AndroidRuntime(16482):    at com.example.appletest.ReservationProcessActivity.onCreate(ReservationProcessActivity.java:102)
02-06 11:50:10.956: E/AndroidRuntime(16482):    at android.app.Activity.performCreate(Activity.java:5372)
02-06 11:50:10.956: E/AndroidRuntime(16482):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
02-06 11:50:10.956: E/AndroidRuntime(16482):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
02-06 11:50:10.956: E/AndroidRuntime(16482):    ... 11 more

这是我的 ReservationProcessActivity class:

public class ReservationProcessActivity extends Activity {

    WebView webView;

    HttpResponse response;
    HttpClient httpclient;
    List<NameValuePair> nameValuePairs;
    StringBuffer buffer;
    HttpPost httppost;  

    String UnitCode;     
    String RDate;        
    String Area;         

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webpayment);

        webView = (WebView)findViewById(R.id.webView1);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.setWebChromeClient(new WebChromeClient());
        webView.setWebViewClient(new MyBrowser());
        webView.getSettings().setBuiltInZoomControls(false);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setHorizontalScrollBarEnabled(false);

        UnitCode = ReservationActivity.UnitCode.toString();
        RDate = ReservationActivity.RDate.toString();
        Area = ReservationActivity.AArea.toString();


        webView.loadUrl("http://google.com/");

    }

}

ReservationActivity class:

package com.example.appletest;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.text.Html;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class ReservationActivity extends Activity {


    public static String UnitCode;
    public static String RDate;
    public static String AArea;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reservation_activity);

        //button
        final Button nextButton = (Button) findViewById(R.id.NextButton1);

        //textbox
        final EditText unitCode = (EditText) findViewById(R.id.editText1);
        final EditText reservationDate = (EditText) findViewById(R.id.editText2);
        final EditText Area = (EditText) findViewById(R.id.editText3);

        nextButton.setOnClickListener(new View.OnClickListener(){

            public void onClick(View v) {
                // TODO Auto-generated method stub

                UnitCode = unitCode.getText().toString();
                RDate = reservationDate.getText().toString();
                AArea = Area.getText().toString();                              

                Intent i = new Intent(ReservationActivity.this, ReservationProcessActivity.class);
                startActivity(i);
                finish();
            }

        });

}

我认为崩溃是因为 NullPointerException。这是因为您过早调用 findViewById。通常,我们在 Activity.setContentView 之后使用 findViewById 已被调用。 见:enter link description here

如果ReservationProcessActivity 在同一个jvm 中的ReservationActivity 的onCreate 之前调用了它的onCreate(),那么ReservationActivity 中的静态变量UnitCode 仍然是null。当您像这样从 ReservationProcessActivity 使用它时:

UnitCode = ReservationActivity.UnitCode.toString();

那会导致你崩溃。我不确定您活动中的事件顺序是什么,但我认为类似的事情正在发生。

在 ReservationProcessActivity 中更改此代码:

UnitCode = ReservationActivity.UnitCode.toString();
    RDate = ReservationActivity.RDate.toString();
    Area = ReservationActivity.AArea.toString();

至:

UnitCode = ReservationActivity.UnitCode;
    RDate = ReservationActivity.RDate;
    Area = ReservationActivity.AArea;

如果您正在使用全局变量,我建议您使用 getter 和 setter。如果我是你,我会有一个单独的 class 用于全局变量。

例如,有一个单独的 class 称为 ReservationData。

public class ReservationData {

 public static String UnitCode;
 public static String RDate;
 public static String AArea;

 public static String getUnitCode() {
     return UnitCode;
}

public static void setUnitCode(String unitCode) {
    UnitCode = unitCode;
}

public static String getRDate() {
    return RDate;
}

public static void setRDate(String RDate) {
    ReservationData.RDate = RDate;
}

public static String getAArea() {
    return AArea;
}

public static void setAArea(String AArea) {
    ReservationData.AArea = AArea;
}

}

从 Activity 调用 setter 方法。

public class 保留Activity 扩展 Activity {

public static String UnitCode;
public static String RDate;
public static String AArea;

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.reservation_activity);

    //button
    final Button nextButton = (Button) findViewById(R.id.NextButton1);

    //textbox
    final EditText unitCode = (EditText) findViewById(R.id.editText1);
    final EditText reservationDate = (EditText) findViewById(R.id.editText2);
    final EditText Area = (EditText) findViewById(R.id.editText3);

    nextButton.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v) {
            // TODO Auto-generated method stub

            UnitCode = unitCode.getText().toString();
            RDate = reservationDate.getText().toString();
            AArea = Area.getText().toString();                              

////调用Setter方法

            ReservationData.setAArea(AArea);
            ReservationData.setRDate(RDate);
            ReservationData.setUnitCode(UnitCode);

////调用Setter方法

            Intent i = new Intent(ReservationActivity.this, ReservationProcessActivity.class);
            startActivity(i);
            finish();
        }

    });

}

在 ReservationProcessActivity class 中调用 ReservationData 的 getter 方法。

而不是下面的代码,

    UnitCode = ReservationActivity.UnitCode.toString();
    RDate = ReservationActivity.RDate.toString();
    Area = ReservationActivity.AArea.toString();

使用这些,

           Area = ReservationData.setAArea(AArea);
           RDate =  ReservationData.setRDate(RDate);
           UnitCode = ReservationData.setUnitCode(UnitCode);