我的问题是,如果有可能之前的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.