簡體   English   中英

setOnClickListener錯誤

[英]setOnClickListener error

這是我的源代碼。 我只是在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的 在此處輸入圖片說明

更改滾動結果分配

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是否已實例化,並僅更​​改文本。

嘗試

new View.OnClickListener()

如果將main.xml和代碼一起發布會更好。

根據您的代碼,shakesDice似乎為Null,並且在setOnClickListener時將引發NullPointerException。

您試圖將int傳遞給方法要求輸入String參數的位置。

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

嘗試這個。

diceScore.setText(String.valueOf(diceNumber));
// 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);

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM