ButtonClick正在打破程序

时间:2017-05-26 14:32:03

标签: android onclicklistener

当我点击第一个按钮进入新窗口但是当我点击它打破程序时,我正在尝试实现onclick。我有错误:

W / OpenGLRenderer:无法在表面0x97154160上设置EGL_SWAP_BEHAVIOR,错误= EGL_BAD_MATCH。

这是主要的:

package FirstApp.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.content.Intent;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity implements 
View.OnClickListener{
TextView text;
Button button;
Button button2;
boolean onClick = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    button = (Button) findViewById(R.id.button);


}
@Override

public void onClick(View view) {


    if(view.getId() == R.id.button){

        text.setText("Prvi button je kliknut");

    }

    else if(view.getId() == R.id.button2){

       /* Intent i = new Intent(this, DetailActivity.class);
        i.putExtra("TEXT", input.getText().toString());
        startActivity(i);/*/

        text.setText("Drugi button je kliknut");
    }
  }
 }

这是第二堂课:

package dvino.myapplication;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;


/**
  * Created by zeroOne on 26.5.2017..
*/

public class Dvino extends Activity{
@Override
protected void onCreate(Bundle saveInstanceState){

    super.onCreate(saveInstanceState);
    setContentView(R.layout.dvino);
  }


}

编辑 - 这是我在程序崩溃时重新启动的错误消息:

<'E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: dvino.myapplication, PID: 3044
                  java.lang.IllegalStateException: Could not find method 
     OnClick(View) in a parent or ancestor Context for android:onClick attribute 
      defined on view class android.support.v7.widget.AppCompatButton with id 
    'button'
                      at 


 android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
                      at 


  android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
                      at android.view.View.performClick(View.java:5610)
                      at android.view.View$PerformClick.run(View.java:22265)
                      at android.os.Handler.handleCallback(Handler.java:751)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6077)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)'

主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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="dvino.myapplication.MainActivity">


    <Button
        android:text="D'vino"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:layout_marginLeft="18dp"
        android:layout_alignBottom="@+id/button2"
        android:layout_alignParentEnd="true"
        android:layout_marginBottom="49dp"
        android:onClick="OnClick"
        />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/dvino"
        android:id="@+id/imageView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <Button
        android:text="D'vino Kitchen"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button2"
        android:layout_marginLeft="18dp"
        android:layout_marginBottom="12dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:onClick="onClick" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/title_message"
        android:id="@+id/text"
        android:textAppearance="?android:textAppearanceLarge"
        android:layout_below="@+id/imageView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="31dp" />


</RelativeLayout>

5 个答案:

答案 0 :(得分:2)

当您尝试调用textview.setText()时,由于空指针异常,您的程序崩溃了 - textview为null因为您还没有初始化它。

button = (Button)findView...一样,你必须为textview做到这一点

答案 1 :(得分:0)

您的代码有TextView问题和 这部分: 公共类MainActivity扩展了AppCompatActivity实现 View.OnClickListener {} 改变: 公共类MainActivity扩展了Activity实现 View.OnClickListener {

编辑--- 哦,试试吧。

公共类MainActivity扩展了Activity {

private Button button;
private TextView text;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button=(Button) findViewById(R.id.testB);
    text=(TextView) findViewById(R.id.testT);


    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            button.setText("ClickedButton");
            text.setText("ChangedText");
        }
    });


}

}

答案 2 :(得分:0)

更改data = pd.read_csv('amazon.csv') from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(data.iloc[:, 1:], data.iloc[:, 0], test_size=0.3, random_state=241) runs = 0 def distOne( x1, x2 ): global runs runs = runs+1 if (runs%100000)==0: print(runs) distance=0 a=(x1!=x2) b = sum(a) return b cl1=KNeighborsClassifier(n_neighbors=10, \ algorithm='ball_tree',\ metric=distOne) cl1.fit( X_train, y_train) cl1.predict_proba( X_test.iloc[1::2] ) 使用ID =“button”的按钮小部件onclick="OnClick"

请记住小心使用大写字母;)

答案 3 :(得分:0)

在初始化按钮onCreate后的button=(Button)findViewBy...方法中,button.onClicklistener(this);

答案 4 :(得分:0)

  

java.lang.IllegalStateException:无法找到方法OnClick(View)   在已定义Context属性的父级或祖先android:onClick中   在具有id的视图类android.support.v7.widget.AppCompatButton上   'button'

1。在您的布局XML中,将Button属性android:onClick值更改为onClick

<?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"
    ..........
    ...............
    tools:context="dvino.myapplication.MainActivity">

    <Button         
        android:id="@+id/button"
        ..........
        .................
        android:onClick="onClick" />


    <Button
        android:id="@+id/button2"
        ..........
        ................
        android:onClick="onClick" />

    ...........
    .................

</RelativeLayout>

2。在您的MainActivity中,您试图在空Text个对象上设置TextView,因为您没有获得reference它来自你所附的layout。它可能会导致NullPointerException。要避免此异常,请更新您的活动onCreate()方法,如下所示:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button = (Button) findViewById(R.id.button);
    text = (TextView) findViewById(R.id.text);
}

希望这会有所帮助〜