语言集成android期间应用程序崩溃

时间:2015-05-14 04:42:27

标签: android nullpointerexception localization

你好朋友我正在我的应用程序中集成语言集成,所以下面是我的代码

Main.java

public class ActivitySetting extends Activity {

    Locale myLocale;
    RadioButton mRadioButtonMile;
    RadioButton mRadioButtonKiloMile;
    Spinner mSpinnerLanguage;
    String selectedItem="";
    BathRoomAdapter mLanAdapter;
    String mStringGetCurrency;
    String ll;
    String[] mStringsArrayLanguage = new String[] { "English",
              "German", "Russian", "Spanish"   };
    public static final String MY_PREF = "MyPreferences";
     AdView mAdView;
     LinearLayout layout;
     boolean firstAdReceived = true;
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);
        ActionBar actionBar = getActionBar();
        actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#932691")));
        actionBar.setTitle(R.string.set);
        actionBar.setHomeButtonEnabled(true);
        mSpinnerLanguage=(Spinner)findViewById(R.id.spn_single_mul_type);
        mRadioButtonMile=(RadioButton)findViewById(R.id.rad_ml);
        mRadioButtonKiloMile=(RadioButton)findViewById(R.id.rad_km);

        mLanAdapter=new BathRoomAdapter(mStringsArrayLanguage);
        mSpinnerLanguage.setAdapter(mLanAdapter);

        SharedPreferences mSharedPreferences = getSharedPreferences("MyPreferences", 0);
        mStringGetCurrency=mSharedPreferences.getString("units", "");



        SharedPreferences mSharedPreferences1 = getSharedPreferences("CommonPrefs", 0);
        ll=mSharedPreferences1.getString("in", "");

        if (ll.equalsIgnoreCase("English")) {
            mSpinnerLanguage.setSelection(0);
        }

        else if (ll.equalsIgnoreCase("German")) {
            mSpinnerLanguage.setSelection(1);
        }
        else if (ll.equalsIgnoreCase("Russian")) {
            mSpinnerLanguage.setSelection(2);
        }
        else if (ll.equalsIgnoreCase("Spanish")) {
            mSpinnerLanguage.setSelection(3);
        }

        mAdView = new AdView(ActivitySetting.this);
        mAdView.setAdSize(AdSize.SMART_BANNER);
        mAdView.setAdUnitId(getResources().getString(R.string.admob_unit_id));      //its a your admob key declare in String File.

        mAdView.setAdListener(new AdListener() {

        @Override
        public void onAdLoaded() {
        firstAdReceived = true;
        mAdView.setVisibility(View.VISIBLE);
        }

        @Override
        public void onAdFailedToLoad(int error) {
        if (!firstAdReceived) {
        mAdView.setVisibility(View.GONE);
        }
        }

        });
        layout = (LinearLayout) findViewById(R.id.adLayout);
        layout.addView(mAdView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);

        if (mStringGetCurrency.equalsIgnoreCase("K")) {
            mRadioButtonKiloMile.setChecked(true);
            mRadioButtonMile.setChecked(false);
        }
        else {
            mRadioButtonKiloMile.setChecked(false);
            mRadioButtonMile.setChecked(true);
        }
        mRadioButtonKiloMile.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                SharedPreferences sp = getSharedPreferences(MY_PREF, 0);
                SharedPreferences.Editor edit = sp.edit();
                edit.putString("units", "K");
                edit.commit();
            }
        });
        mRadioButtonMile.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                SharedPreferences sp = getSharedPreferences(MY_PREF, 0);
                SharedPreferences.Editor edit = sp.edit();
                edit.putString("units", "M");
                edit.commit();
            }
        });
        mSpinnerLanguage.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {
                selectedItem=mStringsArrayLanguage[position].toString() ;

                if (selectedItem .equalsIgnoreCase("English")) {
                    changeLang("en");
                }   else if (selectedItem .equalsIgnoreCase("German")) {
                    changeLang("de");
                } else if (selectedItem.equalsIgnoreCase("Russian")) {
                    changeLang("ru");
                } else if (selectedItem .equalsIgnoreCase("Spanish")) {
                    changeLang("es");
                } 


            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

     public class BathRoomAdapter extends ArrayAdapter<String>
        {

         String ds[];
            public BathRoomAdapter(String[] list) {
                super( ActivitySetting.this, R.layout.spin_layout, list);
                this.ds = list;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                convertView= getLayoutInflater().inflate(R.layout.spin_layout, null);
                 TextView imageView = (TextView)convertView.findViewById(android.R.id.text1);
                 imageView.setText(ds[position]);
                return convertView;
            }

        }


    public void loadLocale()
    {
        String langPref = "Language";
        SharedPreferences prefs = getSharedPreferences("CommonPrefs", Activity.MODE_PRIVATE);
        String language = prefs.getString(langPref, "en");
        selectedItem=prefs.getString("in", "");
        changeLang(language);
    }

    public void changeLang(String lang)
    {
        if (lang.equalsIgnoreCase(""))
            return;
        myLocale = new Locale(lang);
        saveLocale(lang);
        Locale.setDefault(myLocale);
        android.content.res.Configuration config = new android.content.res.Configuration();
        config.locale = myLocale;
        getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
     }

    public void saveLocale(String lang)
    {
        String langPref = "Language";
        SharedPreferences prefs = getSharedPreferences("CommonPrefs", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(langPref, lang);
        editor.putString("in", selectedItem);
        editor.commit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.next, menu);
        return true;
    }

     @Override 
     public boolean onOptionsItemSelected(MenuItem item) 
     {
        switch (item.getItemId())
        {
        case android.R.id.home:

            Intent mIntentq=new Intent(ActivitySetting.this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(mIntentq);
            return true;
        case R.id.cl:

            Intent mIntent22=new Intent(ActivitySetting.this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(mIntent22);
            return true;
        case R.id.cd:

            Intent mIntent=new Intent(ActivitySetting.this, ActivityFav.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(mIntent);
            return true;
        case R.id.normal:
            Intent mIntent1=new Intent(ActivitySetting.this, About.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(mIntent1);

            return true;
        case R.id.satelite:
            Intent mIntent2=new Intent(ActivitySetting.this, Contact.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(mIntent2);
            return true;

        case R.id.pin:
            Intent mIntent21=new Intent(ActivitySetting.this, PinActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(mIntent21);
            return true;

        case R.id.st:
            Intent mIntent2ss1=new Intent(ActivitySetting.this, ActivitySetting.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(mIntent2ss1);
            return true;

        default:
            return super.onOptionsItemSelected(item);
        }
    }

      /** Called when leaving the activity */

    /** Called when returning to the activity */
    @Override
    public void onResume() {
        super.onResume();
         loadLocale();
    }

    /** Called before the activity is destroyed */
    @Override
    public void onDestroy() {
         super.onDestroy();

        System.gc();
        unbindDrawables(layout);
    }
    private void unbindDrawables(View view)
    {
        if (view.getBackground() != null)
        {
            view.getBackground().setCallback(null);
        }
        try
        {
            ((ViewGroup) view).removeAllViews();   
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }
}

当我更改语言时,它会在此行中给出错误Null指针 actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor(“#932691”)));

任何想法我怎么能解决这个问题?

0 个答案:

没有答案