4

我正在尝试将 aSeekBar放在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>
4

1 回答 1

7

试试这个方法。。

必须使用膨胀视图来制作seekbar和参考 id 。textview

    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();

快乐编码:)

于 2013-01-11T20:51:14.977 回答