这是我的源代码。 我只是在 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,然后更改文本。
关于java - setOnClickListener 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21299010/