我正在尝试制作一个具有不同 Intent 的应用程序。我已经准备好两个了我希望第一个 Intent 中的按钮使第二个 Intent 出现。 问题是该按钮始终设置为 null,并在应用程序运行时抛出 NullPointerException
我的 .java 类是:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(vocabulario);
//....
Button buttonVocabulary = (Button) findViewById(R.id.buttonVocabulary);
Button holaPlayerBtn = (Button) findViewById(R.id.holaPlayerBtn);
//...
buttonVocabulary.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view)
{
//Se abre un nuevo Intent con el primer botón
try
{
{
Intent myIntent = new Intent(view.getContext(), Vocabulario.class);
startActivityForResult(myIntent, 0);}
} catch (Exception e) {
e.printStackTrace();
}
}});
我的 main.xml 文件是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/adViewContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
/>
<Button
android:id="@+id/buttonVocabulary"
android:text="@string/vocabulario"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="true" />
</LinearLayout>
</LinearLayout>
在我的 AndroidManifest.xml 中有一个元素表示:
<activity android:name=".Vocabulario"
android:label="@string/vocabulario"/>
我做错了什么。你能帮我解决这个问题吗? 提前致谢。
最佳答案
改变
setContentView(vocabulario);
至
setContentView(R.layout.main);
关于java - Android 中按钮设置为 Null 并生成 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37542621/