当我触摸按钮

时间:2018-01-30 15:40:15

标签: java android xml

当我触摸“saldir”按钮或我的应用程序的另一个按钮时,这是我的代码人员 崩溃了,这是我的应用图片https://ibb.co/ce3ATm

这是我的错误(https://ibb.co/jBxb16):

  

Dergilik APP已停止

package com.example.rg.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements 
View.OnClickListener
{
    int sayac =0;
    TextView tv;
    **//kind of turkish words don't focus the variables**
    TextView tvkarekterOzellikleri;
    Button byemekye;
    @Override
    **//THIS IS MY MAIN_Activity.Java**
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv = (TextView)findViewById(R.id.Answer);
    //I'm trying very basic app when I touch button this layout'll changed
    tvkarekterOzellikleri = (TextView) findViewById(R.id.Question);
    Button bsaldir = (Button) findViewById(R.id.saldir);
    //comment/i have 3 buttons as you can see on link at top 
    Button buyu = (Button) findViewById(R.id.uyu);
    Button byemekye = (Button) findViewById(R.id.yemekYe);
    bsaldir.setOnClickListener(this);
    buyu.setOnClickListener(this);
    byemekye.setOnClickListener(this);*

    //This part useless now because I didn't added this part later I'll add this part
    //Character k =  new Character();
    //k.Movementnum = 10;
    //k.Kilos = 10;
    //k.FightPower = 10;
    //}
    @Override
    //This part is PROBLEM WHEN I DELETE THIS PART MY CODE IS WORKING
    public void onClick(View v){
    if (v.getId()==byemekye.getId())
        tv.setText("yemek yenildi");
    else
        tv.setText("Nar" + (sayac++));
    }
}

当我删除if。(v.getId ....)app正常运行时。
这边是我的activity_main.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.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>

1 个答案:

答案 0 :(得分:1)

我认为你的问题是这样的:
定义按钮:

private Button bt;

在xml上设置按钮的id,而不是在onCrate()上设置id

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
                    }


               });
}

on xml

<Button
                    android:id="@+id/butto1" <!-- the id -->
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Button" />