Android - 从每个函数

时间:2017-04-11 13:18:43

标签: java android

我使用Android Studio制作应用。我有一个名为' GlobalVariables.java '的文件,其中包含以下代码:

public class GlobalVariables extends Application {
    public String CallingActivity;

    public String getCallVariable() {
        return CallingActivity;
    }

    public void setCallVariable(String Value) {
        CallingActivity = Value;

    }
}

在清单文件中我有:

<application android:name=".GlobalVariables" .....

我还有一个 LanguageActivity.java 文件,其中包含以下代码:

package com.testApp;

//import android.content.Intent;
//import android.support.v7.app.ActionBarActivity;
import android.app.ListActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;


public class LanguageActivity extends ListActivity {
 //   public class CountrycodeActivity extends ListActivity {
 public static final String TAG = "MyActivity";
       // public static String RESULT_CONTRYCODE = "countrycode";
        public String[] countrynames;
        private TypedArray imgs;
        private List<Country> countryList;
        Locale myLocale;
        Context context;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            populateCountryList();
            ArrayAdapter<Country> adapter = new CountryListArrayAdapter(this, countryList);
            setListAdapter(adapter);
            getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    ReadWriteFile RWFile = new ReadWriteFile();
                    if (position == 0) {
                        ChangeLanguage("el", getBaseContext());
                        try {
                            RWFile.LangWrite("en",getBaseContext());
                            Log.e(TAG, "LANG === en");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }else{
                        ChangeLanguage("en", getBaseContext());
                        try {
                            RWFile.LangWrite("en",getBaseContext());
                            Log.e(TAG, "LANG === en");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    imgs.recycle(); //recycle images
                    finish();
                }
            });
        }

        private void populateCountryList() {
            countryList = new ArrayList<Country>();
            countrynames = getResources().getStringArray(R.array.country_names);
            //countrycodes = getResources().getStringArray(R.array.country_codes);
            imgs = getResources().obtainTypedArray(R.array.country_flags);
            for(int i = 0; i < countrynames.length; i++){
                countryList.add(new Country(countrynames[i], imgs.getDrawable(i)));
            }
        }

        public class Country {
            private String name;
           // private String code;
            private Drawable flag;
            public Country(String name, Drawable flag){
                this.name = name;
               // this.code = code;
                this.flag = flag;
            }
            public String getName() {
                return name;
            }
            public Drawable getFlag() {
                return flag;
            }
           // public String getCode() {
               // return code;
           // }
        }
        public void ChangeLanguage(String value, Context context){
            Resources res = context.getResources();
            DisplayMetrics dm = res.getDisplayMetrics();
            android.content.res.Configuration conf = res.getConfiguration();
            conf.locale = new Locale(value.toLowerCase());
            res.updateConfiguration(conf, dm);


            GlobalVariables mApp = ((GlobalVariables)getApplicationContext());
            String Activity = mApp.getCallVariable();
           if (Activity.equals("Login")){
                final Intent intent = new Intent(LanguageActivity.this, LoginActivity.class);
                startActivity(intent);
            }else if (Activity.equals("Signup")){
                final Intent intent = new Intent(LanguageActivity.this, SignupActivity.class);
                startActivity(intent);
            }
        }
    }

当我运行代码时,应用程序会出现此错误: java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'android.content.Context android.content.Context.getApplicationContext()' 如果我改变

GlobalVariables mApp = ((GlobalVariables)getApplicationContext());
        String Activity = mApp.getCallVariable();

String Activity = ((GlobalVariables) this.getApplication()).getCallVariable();

然后我收到错误: java.lang.NullPointerException:尝试在空对象引用上调用虚方法'java.lang.String com.testApp.GlobalVariables.getCallVariable()'

我做了很多尝试,但没有任何帮助。无论如何,什么能解决我的问题? 为什么我这样做?我想知道哪个Activity调用ChangeLanguage()来重新启动它以显示所选语言。

4 个答案:

答案 0 :(得分:0)

依赖注入将解决您的问题。如果使用dagger2,解决这类问题会更​​好,因为会更容易创建单例。如果你想我可以在这里发布你如何做到这一点。如果您希望我使用dagger2基础知识编辑此答案,请在此答案下方发表评论。

答案 1 :(得分:0)

将其添加到AndroidManifest.xml:

<application
    android:name=".GlobalVariables"/>

答案 2 :(得分:0)

尝试:

public class GlobalVariables extends Application {
public String CallingActivity;
public staric GlobalVariables instance;

@Override
public void onCreate()
{super.onCreate();
 this.instance = this;
}

public String getCallVariable() {
    return CallingActivity;
}

public void setCallVariable(String Value) {
    CallingActivity = Value;

}

public static GlobalVariables getInstance()
{
    return instance;
}
}

String Activity = GlobalVariables.getInstance()。getCallVariable();

答案 3 :(得分:0)

尝试替换

GlobalVariables mApp = ((GlobalVariables)getApplicationContext());

使用:

GlobalVariables mApp = new GlobalVariables();

排队:

String Activity = mApp.getCallVariable();

替换&#39;活动&#39;通过&#39;活动&#39;因为Activity是一个预定义的单词。

编辑1:这是您尝试的方式:

GlobalVariables mApp = GlobalVariables.getInstance();
mApp.setCallVariable(value);
mApp.getCallVariable();