[英]Error as soon as I add setOnClickListener Android Studio
so for a school project I'm making an app, but I"m stuck. I've got a button on my main class. XML code of this class: 因此,对于一个学校项目,我正在制作一个应用程序,但我遇到了麻烦。我的主要班级上有一个按钮。该班级的XML代码:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.larsb.csvvg.Home" android:background="@drawable/home"> <Button android:id="@+id/Lariks" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="Button" android:visibility="visible" /> <Button android:id="@+id/Salland" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="45dp" android:text="Button" android:visibility="visible" android:layout_alignTop="@+id/Lariks" android:layout_alignLeft="@+id/Lariks" android:layout_alignStart="@+id/Lariks" /> <Button android:id="@+id/CSG" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:visibility="visible" android:layout_below="@+id/Salland" android:layout_alignLeft="@+id/Salland" android:layout_alignStart="@+id/Salland" /> </RelativeLayout>
So what I want to do is when I button click Lariks I switch to a new activity. 所以我想做的是,当我点击Lariks时,我切换到一个新的活动。 I try to do this with the following code: 我尝试使用以下代码执行此操作:
public class MainActivity extends AppCompatActivity { private static int WELCOME=4000; Button lariks; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lariks = (Button)findViewById(R.id.Lariks); lariks.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { } }); new Handler().postDelayed(new Runnable() { @Override public void run() { Intent intent=new Intent(MainActivity.this,Home.class ); startActivity(intent); finish(); } },WELCOME); } }
lariks.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { } });
the app can't launch anymore. 该应用程序无法启动。 It starts the emulator but it keeps saying the app has stopped. 它启动了模拟器,但一直说应用程序已停止。 Anyone has any idea why this is? 有人知道为什么会这样吗? If I remove that part of the code the app actually does run. 如果我删除了那部分代码,则该应用程序实际上会运行。
Your Button
with android:id="@+id/Lariks"
is not in the activity_main
layout (as evidenced by tools:context="com.example.larsb.csvvg.Home"
and your crash). 您的带有android:id="@+id/Lariks"
Button
不在activity_main
布局中(如tools:context="com.example.larsb.csvvg.Home"
和您的崩溃所证明)。 Therefore findViewById(R.id.Lariks)
returns null, and attempting to call setOnClickListener()
on this null reference causes a crash. 因此, findViewById(R.id.Lariks)
返回null,并且尝试对此null引用调用setOnClickListener()
会导致崩溃。
Your findViewById()
- setOnClickListener()
pair should likely be in the Home
activity, or the button should be in the activity_main
layout. 您的findViewById()
- setOnClickListener()
对可能应该在Home
活动中,或者按钮应该在activity_main
布局中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.