gpt4 book ai didi

java - alertDialog 中的 SeekBar

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:30:47 25 4
gpt4 key购买 nike

我正在尝试将 SeekBar 放在 AlertDialog 中。我有这段代码:

AlertDialog.Builder builder = new AlertDialog.Builder(GameScreen.this);
LayoutInflater inflater = GameScreen.this.getLayoutInflater();
builder.setView(inflater.inflate(R.layout.betvalue, null))
.setPositiveButton ...
.setNegativeButton ...
.setTitle("Enter bet amount");
SeekBar sbBetVal = (SeekBar)findViewById(R.id.sbBetVal);
tvBetVal = (TextView) findViewById(R.id.tvBetVal);
sbBetVal.setMax(playerList[0].getChipCount());
sbBetVal.setProgress(0);
sbBetVal.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
tvBetVal.setText(String.valueOf(progress));
}
});

builder.create();
builder.show();

但是,这给了我一个NullPointerException。为什么会这样,我该如何解决?

这是日志:

01-11 18:16:52.898: E/AndroidRuntime(27668): FATAL EXCEPTION: main
01-11 18:16:52.898: E/AndroidRuntime(27668): java.lang.NullPointerException
01-11 18:16:52.898: E/AndroidRuntime(27668): at com.rhiokai.chipcounter.GameScreen.onClick(GameScreen.java:214)
01-11 18:16:52.898: E/AndroidRuntime(27668): at android.view.View.performClick(View.java:4202)
01-11 18:16:52.898: E/AndroidRuntime(27668): at android.view.View$PerformClick.run(View.java:17340)
01-11 18:16:52.898: E/AndroidRuntime(27668): at android.os.Handler.handleCallback(Handler.java:725)
01-11 18:16:52.898: E/AndroidRuntime(27668): at android.os.Handler.dispatchMessage(Handler.java:92)
01-11 18:16:52.898: E/AndroidRuntime(27668): at android.os.Looper.loop(Looper.java:137)
01-11 18:16:52.898: E/AndroidRuntime(27668): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-11 18:16:52.898: E/AndroidRuntime(27668): at java.lang.reflect.Method.invokeNative(Native Method)
01-11 18:16:52.898: E/AndroidRuntime(27668): at java.lang.reflect.Method.invoke(Method.java:511)
01-11 18:16:52.898: E/AndroidRuntime(27668): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-11 18:16:52.898: E/AndroidRuntime(27668): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-11 18:16:52.898: E/AndroidRuntime(27668): at dalvik.system.NativeStart.main(Native Method)

这是 xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/betvalue"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp" >

<TextView
android:id="@+id/tvBetVal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/chips_fill"
android:textAppearance="?android:attr/textAppearanceLarge" />

<SeekBar
android:id="@+id/sbBetVal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tvBetVal"
android:layout_centerHorizontal="true" />

</RelativeLayout>

最佳答案

试试这个方法..

seekbartextview 的引用 id 必须使用膨胀 View ..

    AlertDialog.Builder builder = new AlertDialog.Builder(GameScreen.this);
LayoutInflater inflater = GameScreen.this.getLayoutInflater();
View v = inflater.inflate(R.layout.betvalue, null);
builder.setView(v)
.setPositiveButton ...
.setNegativeButton ...
.setTitle("Enter bet amount");
SeekBar sbBetVal = (SeekBar)v.findViewById(R.id.sbBetVal);
tvBetVal = (TextView)v. findViewById(R.id.tvBetVal);
sbBetVal.setMax(100);
sbBetVal.setProgress(0);
sbBetVal.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
tvBetVal.setText(String.valueOf(progress));
}
});

builder.create();
builder.show();

快乐编码:)

关于java - alertDialog 中的 SeekBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14286034/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com