麻烦的setContentView之前code [英] Code before setContentView trouble

查看:107
本文介绍了麻烦的setContentView之前code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,如果有可能之前的setContentView写code()的onCreate()活动的方式。在code下面我想叫 setVariables()的setContentView()但是这会导致我的应用程序坠毁。如果我称之为 setVariables()的setContentView(),它工作正常。为什么是这样?

 包com.oxinos.android.moc;


进口android.app.Activity;
进口android.content.Intent;
进口android.content.Shared preferences;
进口android.content.res.Resources;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.CheckBox;


公共类mocActivity延伸活动{
    / **第一次创建活动时调用。 * /

    公共静态字符串prefsFile =MOC preFS;
    共享preferences MOC preFS;
    公共资源资源;
    公开复选框cafesCB,barsRestCB,clothingCB,groceriesCB,miscCB;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        setVariables();
        的setContentView(R.layout.main);

        MOC preFS = getShared preferences(prefsFile,0);
    }

    私人无效setVariables(){
        RES = getResources();
        cafesCB =(复选框)findViewById(R.id.cafesCheckBox);
        barsRestCB =(复选框)findViewById(R.id.barsRestCheckBox);
        clothingCB =(复选框)findViewById(R.id.clothingCheckBox);
        groceriesCB =(复选框)findViewById(R.id.groceriesCheckBox);
        miscCB =(复选框)findViewById(R.id.miscCheckBox);

    }
    公共无效submitHandler(查看视图){
        开关(view.getId()){
        案例R.id.submitButton:
            布尔网吧= cafesCB.isChecked();
            布尔barsRest = barsRestCB.isChecked();
            布尔服装= clothingCB.isChecked();
            布尔杂货= groceriesCB.isChecked();
            布尔杂项= miscCB.isChecked();

            共享preferences.Editor编辑= MOC prefs.edit();

            editor.putBoolean(res.getString(R.string.cafesBool),咖啡馆);
            editor.putBoolean(res.getString(R.string.barsRestBool),barsRest);
            editor.putBoolean(res.getString(R.string.clothingBool),服装);
            editor.putBoolean(res.getString(R.string.groceriesBool),杂货);
            editor.putBoolean(res.getString(R.string.miscBool),杂项);
            editor.commit();
            startActivity(新意图(这一点,mocActivity2.class));
            打破;
        }

    }
}
 

解决方案

您可以执行的任何code你想的前的setContentView()方法,只要它不是指(部分)的查看,这还没有设置。

由于您的 setVariables()法指的是查看的内容,就不能执行。

My question is if it is possible to write code before setContentView() in the onCreate() method of the main Activity. In the code below I want to call the setVariables() before setContentView() but this causes my application to crash. If I call setVariables() after setContentView(), it works fine. Why is this?

package com.oxinos.android.moc;


import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;


public class mocActivity extends Activity {
    /** Called when the activity is first created. */

    public static String prefsFile = "mocPrefs";
    SharedPreferences mocPrefs;
    public Resources res;
    public CheckBox cafesCB, barsRestCB, clothingCB, groceriesCB, miscCB;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setVariables();
        setContentView(R.layout.main);

        mocPrefs = getSharedPreferences(prefsFile,0);
    }

    private void setVariables(){
        res = getResources();
        cafesCB = (CheckBox) findViewById(R.id.cafesCheckBox);
        barsRestCB = (CheckBox) findViewById(R.id.barsRestCheckBox);
        clothingCB = (CheckBox) findViewById(R.id.clothingCheckBox);
        groceriesCB = (CheckBox) findViewById(R.id.groceriesCheckBox);
        miscCB = (CheckBox) findViewById(R.id.miscCheckBox);

    }
    public void submitHandler(View view){
        switch (view.getId()) {
        case R.id.submitButton:
            boolean cafes = cafesCB.isChecked();
            boolean barsRest = barsRestCB.isChecked();
            boolean clothing = clothingCB.isChecked();
            boolean groceries = groceriesCB.isChecked();
            boolean misc = miscCB.isChecked();

            SharedPreferences.Editor editor = mocPrefs.edit();

            editor.putBoolean(res.getString(R.string.cafesBool), cafes);
            editor.putBoolean(res.getString(R.string.barsRestBool), barsRest);
            editor.putBoolean(res.getString(R.string.clothingBool), clothing);
            editor.putBoolean(res.getString(R.string.groceriesBool), groceries);    
            editor.putBoolean(res.getString(R.string.miscBool), misc);
            editor.commit();
            startActivity(new Intent(this, mocActivity2.class));
            break;
        }

    }
}

解决方案

You can execute any code you want before the setContentView() method as long as it doesn't refer to (parts of) the View, which isn't set yet.

Since your setVariables() method refers to the contents of the View, it can't be executed.

这篇关于麻烦的setContentView之前code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆