setOnClickListener错误

时间:2014-01-23 03:53:07

标签: java android onclick

这是我的源代码。 我在setOnClickListener中遇到错误。我不知道setOnClickListener里面有什么问题。我跟踪了所有代码,错误发生在setOnClickListener ......

我有这样的搜索问题。但我找不到我想要的答案。 我是新手android程序员

public class Main extends Activity {
    TextView diceScore;
    RelativeLayout mainLayout;
    Button shakeDice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         shakeDice = (Button) findViewById(R.id.shakeDice);

        shakeDice.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                randomDiceNumber();
            }
        });

    }


    public void randomDiceNumber() { 
        int diceNumber = 0;
        int max = 6;
        int min = 1;

        Random r = new Random();
        diceNumber = r.nextInt(max - min + 1) + min;
        diceScore = new TextView(this);
        diceScore.setText(diceNumber);
        diceScore.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT) );
        mainLayout = (RelativeLayout) findViewById(R.id.main);
        mainLayout.addView(diceScore);

    }


    }
编辑Button.OnClickListener到View.OnClickListener

后,

我的导入

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

logcat的enter image description here

5 个答案:

答案 0 :(得分:2)

更改滚动结果分配

diceScore.setText(String.valueOf(diceNumber));

和OnClickListener类型为

android.view.View.OnClickListener;

并按以下方式尝试(请注意导入)。您只需要根据自己的喜好调整滚动结果TextView的位置

import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class Main extends Activity {
    TextView diceScore;
    RelativeLayout mainLayout;
    Button shakeDice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        shakeDice = (Button) findViewById(R.id.shakeDice);


    shakeDice.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            randomDiceNumber();

        }
    });

}


@SuppressWarnings("deprecation")
public void randomDiceNumber() { 
    int diceNumber = 0;
    int max = 6;
    int min = 1;

    Random r = new Random();
    diceNumber = r.nextInt(max - min + 1) + min;
    diceScore = new TextView(this);
    diceScore.setText(String.valueOf(diceNumber));
    diceScore.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)         );
    mainLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
    mainLayout.addView(diceScore);

    }
}

这是布局XML文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relativeLayout"
    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=".Main" >

    <Button
        android:id="@+id/shakeDice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="66dp"
        android:text="Button" />

</RelativeLayout>

如果多次按下按钮,结果将自行覆盖,因此请检查滚动时diceScore是否已实例化,只需更改文本。

答案 1 :(得分:1)

尝试

new View.OnClickListener()

答案 2 :(得分:1)

如果将main.xml与代码一起发布会更好。

基于你的代码,shakeDice似乎是Null,并且它会在setOnClickListener时抛出NullPointerException。

答案 3 :(得分:0)

您正在尝试传递方法请求String参数的位置。

int diceNumber = 0;
diceScore.setText(diceNumber);

试试这个。

diceScore.setText(String.valueOf(diceNumber));

答案 4 :(得分:0)

// try this way
    TextView diceScore;
    RelativeLayout mainLayout;
    Button shakeDice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        shakeDice = (Button) findViewById(R.id.shakeDice);

        // here you set View.OnClickListener rather than Button.OnClickListener
        shakeDice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                randomDiceNumber();
            }
        });

    }


    public void randomDiceNumber() {
        int diceNumber;
        int max = 6;
        int min = 1;

        Random r = new Random();
        diceNumber = r.nextInt(max - min + 1) + min;
        diceScore = new TextView(this);
        // here TextView setText() accept String or CharacterSequence so can't gave int becz when gave as int it's try finding String from value.xml like String resource
        diceScore.setText(String.valueOf(diceNumber));
        diceScore.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT) );
        mainLayout = (RelativeLayout) findViewById(R.id.main);
        try{
            mainLayout.removeAllViews();
        }catch (Exception e){
            e.printStackTrace();
        }
        mainLayout.addView(diceScore);

}