Java 我的android应用程序在我按下按钮时停止
当我触摸应用程序中的saldir按钮或其他按钮时,这就是我的代码人员 崩溃,这是我的应用程序的图像 这是我的错误: Dergilik应用程序已停止 当我删除if.v.getId。。。。应用程序运行正常。 这边是我的activity_main.xmlJava 我的android应用程序在我按下按钮时停止,java,android,xml,Java,Android,Xml,当我触摸应用程序中的saldir按钮或其他按钮时,这就是我的代码人员 崩溃,这是我的应用程序的图像 这是我的错误: Dergilik应用程序已停止 当我删除if.v.getId。。。。应用程序运行正常。 这边是我的activity_main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<?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.rg.myapplication.MainActivity">
<Button
android:id="@+id/saldir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:elevation="0dp"
android:text="saldir" />
<TextView
android:id="@+id/Question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|bottom|center"
android:padding="3dp"
android:text="@string/Dilektasi"
android:textAppearance="@style/TextAppearance.AppCompat.Display2"
android:layout_above="@+id/saldir"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="75dp" />
<TextView
**android:id="@+id/Answer**"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/saldir"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:text="" />
//I have 3 button for my app as you can see on image
<Button
android:id="@+id/yemekYe"
**im saying twice pls dont focus on variables**
**next time i'll make this variables english**
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/saldir"
android:layout_toLeftOf="@+id/saldir"
android:layout_toStartOf="@+id/saldir"
android:text="yemek ye" />
smth smth smth smth
<Button
android:id="@+id/uyu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/saldir"
android:layout_toEndOf="@+id/saldir"
android:layout_toRightOf="@+id/saldir"
android:text="Uyu" />
//dont focus the variable names
</RelativeLayout>
我认为你的问题是: 定义按钮: 在xml上设置按钮的id,而不是在onCrate上 代码: 关于xml
<Button
android:id="@+id/butto1" <!-- the id -->
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
先生,你能简单解释一下吗?请不要添加虚假文本。你的帖子看起来大多是代码;请添加更多的细节,意思正是它所说的:花些时间更详细地解释你的问题。
private Button bt;
bt= (Button) findViewById(R.id.button1); //button1 is the id of the button
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//do actions
}
});
public class YourActivity extends AppCompatActivity {
private Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_details);
parent_view = findViewById(android.R.id.content);
btn1= (Button) findViewById(R.id.button1); //button1 is the id of the button
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//do actions
}
});
}
<Button
android:id="@+id/butto1" <!-- the id -->
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />