- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的问题是是否可以在 Activity
的 onCreate()
方法中的 setContentView()
之前编写代码。在下面的代码中,我想在 setContentView()
之前调用 setVariables()
,但这会导致我的应用程序崩溃。如果我在 setContentView()
之后调用 setVariables()
,它工作正常。为什么是这样?
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;
}
}
}
最佳答案
您可以在 setContentView()
方法之前执行任何您想要的代码,只要它不引用(部分)View
,尚未设置。
由于您的 setVariables()
方法引用了 View
的内容,因此无法执行。
关于java - setContentView 之前的代码麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10153376/
我注意到 Activity 类有一个 setContentView 方法,可以在其中加载 xml 资源文件。我想对一个最终继承自 View 的类做同样的事情。这似乎是一条死胡同,因为 View 类不存
所以我尝试切换布局以获得一个非常简单的两页应用程序。但是,当我单击后退按钮时,setContentView 不起作用。有人知道为什么以及如何解决这个问题吗? 这是我的代码: public class
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度的了解。包括尝试的解决方案、为什么它们不起作用以及预期结果
我正在尝试这个: setContentView(R.layout.activity_main); main 是我的菜单文件夹中的 main.xml ,但它在 activity_main 中向我显示错误
我搜索了一个小时 - 我找不到错误: public class favdetail extends Activity { @Override public void onCreate(
您好,我是对上一个问题的重新提问,只是因为我之前不清楚。我有一个 json 提要,我在其中找到了数组的长度。它应该产生一个。但我想要做的是,如果要添加第二个项目,则需要使用另一个布局。 所以我目前有一
我是 Android 环境的新手。出于某种原因,我在尝试构建时遇到错误。具体来说,activity_main.xml 布局文件中的这些类型被报告为未找到。但是,我确实在那里看到了它。我的环境似乎有些困
我最近收到了有关创建程序化 View 的问题。为什么所有的 View 都初始化好,所有的参数都设置好了,还要调用setContentView? 简单的例子: override fun onCr
要求 应用需求是当用户安装并首次打开时,会显示LoginActivity,登录成功后,会要求他输入4位passcode,将来使用,如果应用程序保持空闲并且手机被锁定。 设计 我决定将其设计为 One
请检查编辑。 val binding= DataBindingUtil.setContentView(this@MainActivity,R.layout.activity_main) 我收到以
有没有办法将字符串传递给“setContentView()”?即:setContentView(输出)。原因是我有一个用于多个 Activity 的类(自定义 ListView 适配器),我需要为一个
我有一个 MainActivity。有时在加载时我会观察到黑屏一秒钟。我测量了 onCreate 方法中的操作时间,发现 setContentView(R.layout.main_screen); 花
如果我的布局相同但资源发生变化,我可以多次调用 setcontentview 吗?例如,如果图像在 2 个 imageview 小部件中交换?(这实际上是我的应用程序中发生的所有事情) 最佳答案 您可
我的问题是是否可以在 Activity 的 onCreate() 方法中的 setContentView() 之前编写代码。在下面的代码中,我想在 setContentView() 之前调用 setV
在我的应用程序中有两个类,一个是 InternetActivity,它只扩展 Activity 并将 contentview 设置为 main。和扩展广播接收器的 MyClass。 我在 main.x
我正在尝试 android 网站上的第一个教程 here 目标是输入文本并将其传送到下一页以显示它。 但是,我总是收到“不幸的是,我的第一个应用已停止运行”。 我注意到当我注释掉 setContent
我的主要 Activity 是尝试从导入的包中调用方法。我已经在下面发布了调用和方法。我收到的错误消息是该行 setContentView(twitterSite); 我得到一个空点异常!
我今天在 android studio 中遇到了一个错误。我正在尝试在应用程序中创建一个关于我们的屏幕。布局 xml 文件已创建。任何帮助表示赞赏。谢谢。 错误:无法解析方法 setcontentvi
在我的主要 Activity 中,我想对其进行设置,以便我首先看到一个仅显示背景和一些文本的 contentView。 X 秒后,我想切换到另一个 View (GLSurfaceView)。 这显然是
有没有办法在一个 Activity 期间使用不同的 id 多次调用 setContentView(id) 以呈现不同的 View ,或者我是否必须启动一个新的 Activity? 最佳答案 根据 Au
我是一名优秀的程序员,十分优秀!