Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用搜索栏创建警报对话框窗口?_Java_Android_Android Studio - Fatal编程技术网

Java 如何使用搜索栏创建警报对话框窗口?

Java 如何使用搜索栏创建警报对话框窗口?,java,android,android-studio,Java,Android,Android Studio,我的布局如下: <SeekBar android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/seekBar" android:max="100" android:progress="20" android:layout_gravity="center_horizontal" android:progressDraw

我的布局如下:

<SeekBar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/seekBar"
    android:max="100"
    android:progress="20"
    android:layout_gravity="center_horizontal"
    android:progressDrawable="@android:color/white"
    android:thumb="@drawable/drunk"
    />
这是一个警报对话框,当我点击一个按钮时会出现

public void press(){
   LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   View mView = inflater.inflate(R.layout.seekbar, null);
   SeekBar mySeekBar = (SeekBar) findViewById(R.id.seekBar);
   mySeekBar.setOnSeekBarChangeListener(customSeekBarListener);
  // final EditText input = new EditText(this);
  // input.setInputType(InputType.TYPE_CLASS_NUMBER );

    AlertDialog.Builder myAlert = new AlertDialog.Builder(this);
   //LayoutInflater inflater = MainActivity.this.getLayoutInflater();
   myAlert.setView(inflater.inflate(R.layout.fragment_edit_name,null))
            .setView(mView)
            .setMessage("SeekBar")
            .setTitle("title")
            .setIcon(R.drawable.ic_launcher)
            .create();
    myAlert.show();
}
我确信我错过了一些东西,因为它不起作用。我怎样才能修好它?我正在使用android studio

我现在遇到的错误是:

java.lang.NullPointerException
            at projectName.MainActivity.press(MainActivity.java:109)

这一行是mySeekBar.setonseekbarchangelistener自定义seekbarlistener

首先使用LayoutInflater将要设置为对话框的视图充气:

然后初始化seekbar:

SeekBar mySeekBar = (SeekBar) mView.findViewById(R.id.seekBar);
mySeekBar.setOnSeekBarChangeListener(customSeekBarListener);
将对话框视图设置为查看您的对话框:

AlertDialog.Builder myAlert = new AlertDialog.Builder(this);
myAlert.setMessage("SeekBar")
        .setView(mView)
        .setTitle("Title")
        .setIcon(R.drawable.ic_launcher)
        .create();
myAlert.show();

希望它能帮助和快乐的编码

您需要对布局进行充气


SeekBar mySeekBar=SeekBar getLayoutInflater.inflateR.layout.mySeekBar,null

到底是什么不起作用?我在“mySeekBar.setOnSeekBarChangeListenercustomSeekBarListener;”上收到一个错误。它说这是空的,我现在就试试:D.我现在必须创建一个对话框视图吗?我现在没有。我建议用单独的xml创建对话框的视图:我是按照您的方式创建的,但现在我发现“mySeekBar.setOnSeekBarChangeListenercustomSeekBarListener;”有一个错误那不行。我明白了:“android.widget.LinearLayout不能强制转换为android.widget.SeekBar”SeekBar是布局中唯一的东西吗?你的.setViewmySeekBar行让我这么想。如果您将seekbar更改为根元素,那么它将工作。将其更改为根元素是什么意思?android.widget.LinearLayout不能强制转换为android.widget.seekbar意味着LinearLayout是您的根元素。如果您的布局只包含一个SeekBar,那么它将是您的根元素。是的,SeekBar是唯一的元素。你说我该怎么办?
SeekBar mySeekBar = (SeekBar) mView.findViewById(R.id.seekBar);
mySeekBar.setOnSeekBarChangeListener(customSeekBarListener);
AlertDialog.Builder myAlert = new AlertDialog.Builder(this);
myAlert.setMessage("SeekBar")
        .setView(mView)
        .setTitle("Title")
        .setIcon(R.drawable.ic_launcher)
        .create();
myAlert.show();