快速通知:我正在使用SharedPreferences
这样我可以在重新打开应用程序时重新加载数据。
Problem我有一个LinearLayout
在我的应用程序的主要片段中。一切都运行顺利,直到我重新打开应用程序并尝试重新初始化LinearLayout
.
我正在尝试初始化LinearLayout
with findViewById()
。我已经把这个函数放在很多不同的地方了。目前我正在尝试让它工作onCreate
和一个被调用的函数onCreate
。到目前为止,这是我的代码:
public class MainActivity extends AppCompatActivity {
LinearLayout linearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout = findViewById(R.id.linearLayout);
// this is where is load the SharedPreferences
// this is where I implement them back into the app ('reload' the app)
reload();
}
public void reload() {
// bunch of other irrelevant stuff
linearLayout = findViewById(R.id.linearLayout);
linearLayout.addView(/*other view*/); // this is where it complains
}
// the is for when the button is clicked
public void submitEntry(View view) {
// this is fine according to Logcat
linearLayout = findViewById(R.id.linearLayout);
}
}
我希望在初始化它两次之后,或者至少尝试一下,它会流行起来,但没有。 Logcat 抱怨 LinearLayout 是一个null object reference
。我现在不知道该怎么做,但这可能是我忽略的简单事情。任何帮助,将不胜感激。
LinearLayout LinearLayout= new LinearLayout(context);像这样初始化它会对你有帮助!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)