简体   繁体   中英

SeekBar in an alertDialog

I am trying to place a SeekBar in an AlertDialog . I have this code:

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

However, this gives me a NullPointerException . Why is this and how can I fix this?

Here's the logcat:

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)

And here's the 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>

Try this way..

Reference id of seekbar and textview has to be made using inflated 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();

Happy Coding :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM