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

时间:2015-02-06 03:50:30

标签: java android string global-variables

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

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类上访问它时:

    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.access$700(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类:

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类:

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();
            }

        });

}

4 个答案:

答案 0 :(得分:2)

在ReservationProcessActivity中更改此代码:

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

到:

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

答案 1 :(得分:1)

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

答案 2 :(得分:1)

如果ReservationProcessActivity在同一个jvm中的ReservationActivity的onCreate之前调用了onCreate(),那么ReservationActivity中的静态变量UnitCode仍然为null。当你从ReservationProcessActivity使用它时,如下所示:

UnitCode = ReservationActivity.UnitCode.toString();

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

答案 3 :(得分:1)

如果您正在玩全局变量,我建议您使用getter和setter。如果我是你,我会为全局变量设一个单独的类。

例如,有一个名为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方法。

公共类ReservationActivity扩展了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类中,从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);