我需要我的应用来创建一个对话框Seekbar。但我不能让它发挥作用。我引用了这个stackoverflow问题Android, SeekBar in dialog,这是我的代码。
我正在创建一个菜单,当用户点击size_config按钮时,它会在菜单项上显示搜索栏。
}if(item.getItemId() == R.id.size_config){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.id.dialog, (ViewGroup)findViewById(R.id.layout));
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setView(layout);
AlertDialog alertDialog = builder.create();
alertDialog.show();
SeekBar sb = (SeekBar)layout.findViewById(R.id.dialog);
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
Log.d("Dialog to change size", "It's working");
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
}
这是布局文件。
dialog.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:id="@+id/layout">
<SeekBar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialog"
android:layout_width="225dp"
android:layout_height="wrap_content"/>
</LinearLayout>
这是错误代码。
06-24 02:31:30.369: E/AndroidRuntime(379): FATAL EXCEPTION: main
06-24 02:31:30.369: E/AndroidRuntime(379): android.content.res.Resources$NotFoundException: Resource ID #0x7f070001 type #0x12 is not valid
06-24 02:31:30.369: E/AndroidRuntime(379): at android.content.res.Resources.loadXmlResourceParser(Resources.java:1874)
06-24 02:31:30.369: E/AndroidRuntime(379): at android.content.res.Resources.getLayout(Resources.java:731)
06-24 02:31:30.369: E/AndroidRuntime(379): at android.view.LayoutInflater.inflate(LayoutInflater.java:318)
06-24 02:31:30.369: E/AndroidRuntime(379): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
06-24 02:31:30.369: E/AndroidRuntime(379): at in.isuru.animation.SnowFall.onOptionsItemSelected(SnowFall.java:189)
06-24 02:31:30.369: E/AndroidRuntime(379): at android.app.Activity.onMenuItemSelected(Activity.java:2195)
06-24 02:31:30.369: E/AndroidRuntime(379): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:730)
06-24 02:31:30.369: E/AndroidRuntime(379): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
06-24 02:31:30.369: E/AndroidRuntime(379): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
06-24 02:31:30.369: E/AndroidRuntime(379): at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:137)
06-24 02:31:30.369: E/AndroidRuntime(379): at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:874)
06-24 02:31:30.369: E/AndroidRuntime(379): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
06-24 02:31:30.369: E/AndroidRuntime(379): at android.widget.ListView.performItemClick(ListView.java:3382)
06-24 02:31:30.369: E/AndroidRuntime(379): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
06-24 02:31:30.369: E/AndroidRuntime(379): at android.os.Handler.handleCallback(Handler.java:587)
06-24 02:31:30.369: E/AndroidRuntime(379): at android.os.Handler.dispatchMessage(Handler.java:92)
06-24 02:31:30.369: E/AndroidRuntime(379): at android.os.Looper.loop(Looper.java:123)
06-24 02:31:30.369: E/AndroidRuntime(379): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-24 02:31:30.369: E/AndroidRuntime(379): at java.lang.reflect.Method.invokeNative(Native Method)
06-24 02:31:30.369: E/AndroidRuntime(379): at java.lang.reflect.Method.invoke(Method.java:521)
06-24 02:31:30.369: E/AndroidRuntime(379): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-24 02:31:30.369: E/AndroidRuntime(379): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-24 02:31:30.369: E/AndroidRuntime(379): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:3)
以下是添加SeekBar对话框的方法,
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog);
dialog.setTitle("Set size!");
dialog.setCancelable(true);
//there are a lot of settings, for dialog, check them all out!
dialog.show();
seekbar = (SeekBar) dialog.findViewById(R.id.size_seekbar);
final TextView tv_dialog_size = (TextView) dialog.findViewById(R.id.set_size_help_text);
// and you can call seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
//implement methods.
}
对话框布局xml是,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="2dp"
android:layout_gravity="center" >
<TextView
android:id="@+id/set_size_help_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please select snowflake size"
android:textSize="25dp" />
<SeekBar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/size_seekbar"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
</LinearLayout>
答案 1 :(得分:0)
这个电话:
View layout = inflater.inflate(R.id.dialog, (ViewGroup)findViewById(R.id.layout));
错了。您需要R.layout.dialog
(您希望用视图扩充视图的布局资源)
View layout = inflater.inflate(R.layout.dialog, null);
答案 2 :(得分:0)
找到答案......对于那些带有自定义对话框的搜索栏,需要将它们初始化为 -
SeekBar seekBar = (SeekBar)dialog.findViewById(R.id.valueBar);