我想创建一个正常的AlertDialog.我使用了android dev docs提供的示例.我刚刚将DIALOG_PAUSED_ID更改为DIALOG_DELETEDB.如果我执行我的代码并按下按钮,而该按钮应该创建对话框,我会得到以下错误日志:
04-29 01:01:20.973: WARN/dalvikvm(1168): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
04-29 01:01:20.973: ERROR/AndroidRuntime(1168): Uncaught handler: thread main exiting due to uncaught exception
04-29 01:01:20.993: ERROR/AndroidRuntime(1168): java.lang.IllegalArgumentException: Activity#onCreateDialog did not create a dialog for id 4
04-29 01:01:20.993: ERROR/AndroidRuntime(1168): at android.app.Activity.createDialog(Activity.java:871)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168): at android.app.Activity.showDialog(Activity.java:2483)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168): at mjb.project.AVV.Favs.onMenuItemSelected(Favs.java:111)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:730)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:139)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168): at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:525)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168): at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
04-29 01:01:20.993: …Run Code Online (Sandbox Code Playgroud) 我试图在不在扩展活动的类中启动警报对话框,但我在GUI线程上.我可以访问我的应用程序上下文并尝试使用它启动警报对话框,但会收到错误:
02-12 00:48:07.412:ERROR/AndroidRuntime(1322):android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌null不适用于应用程序
有谁知道我在这个上做错了什么?
我想在对话框中创建一个微调器
final Dialog viewDialog = new Dialog(NewActivity.this);
viewDialog.getWindow().setFlags(
WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
viewDialog.setTitle("Nearest "+currentLocationType);
LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dialogView = li.inflate(R.layout.nearestlocs, null);
viewDialog.setContentView(dialogView);
// error is in the next line
Spinner addressSpinner = (Spinner)
dialogView.findViewById(R.id.addressSpinner);
Button okButton = (Button) dialogView.findViewById(R.id.okbutton);
Run Code Online (Sandbox Code Playgroud)
nearestlocs.xml是
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Spinner android:id="@+id/addressSpinner"
android:drawSelectorOnTop="true" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button android:text="Show route on Map" android:id="@+id/okbutton"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"></Button>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
错误日志
03-14 01:09:24.686: WARN/dalvikvm(6583): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
03-14 01:09:24.686: ERROR/AndroidRuntime(6583): Uncaught …Run Code Online (Sandbox Code Playgroud) 我有一个AlertDialog,我只想在单击"确定"后启动下一个活动.问题出在我的方法中,我有局部变量附加到我的意图,我不能放入onCreateDialog方法.
//local variable created
ArrayList<String> students = new ArrayList<String>();
for(int i=0; i<studentList.size(); i++){
students.add(studentList.get(i).getName());
}
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
new ContextThemeWrapper(this, R.style.popup_theme));
alertDialog.setTitle("Select Game");
alertDialog.setPositiveButton("3x3", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//I want the new Activity to start only after user clicks ok.
Intent intent=new Intent(getApplicationContext(),NewActivity.class);
//Local variable, undefined, cannot reference
intent.putStringArrayListExtra("students", students);
startActivity(intent);
finish();
return;
}
});
alertDialog.show();
Run Code Online (Sandbox Code Playgroud)
有没有什么办法可以防止代码showDialog()运行后直到用户点击OK?我有局部变量,我需要放在intent中.
我有一个线程,每六秒钟就会向数据库发送一次GPS坐标,并且我会检查用户是否在一个已定义的区域内.如果用户不在该位置,我想要一个警告对话框,通知他们他们超出范围,如果他们在该区域内,我想要一个告诉他们在范围内的对话框.我检查工作正常,但我已经尝试过,我很确定我无法在后台线程上添加对话框.我已经阅读了一些关于使用处理程序的内容,但我不确定如何实现它.如果您有任何建议我会很感激!谢谢.
这是我FindLocation.java从主要活动(MainActivity.java)调用的方式:
new FindLocation(getBaseContext()).start(usr_id1); //sends a user id with it
Run Code Online (Sandbox Code Playgroud)
下边是 FindLocation.java
public class FindLocation extends Thread {
public boolean inJurisdiction;
public boolean AlertNotice = false;
private LocationManager locManager;
private LocationListener locListener;
Context ctx;
public String userId;
public FindLocation(Context ctx) {
this.ctx = ctx;
}
public void start(String userId) {
this.userId = userId;
super.start();
}
@Override
public void run() {
Looper.prepare();
final String usr = userId;
//get a reference to the LocationManager
locManager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE); …Run Code Online (Sandbox Code Playgroud) 我想在几个按钮的Onclick事件中检查一些Spinner控件(如果用户离开Activity),如果一个或多个未填写,则向用户显示一条带有2个选项的警告消息:
@Override
public void onClick(View v) {
int i1 = spinner1.getSelectedItemPosition();
(..)
if ((i1 == 0) | (i2 == 0) | (i3 == 0) | (i4 == 0) | (i5 == 0)) {
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
alertbox.setMessage("Answers missing, what do you want to do?");
final CharSequence[] items = {
"Oops.. let me fix that…",
"I want to leave the app now" };
alertbox.setTitle("Pick an item");
alertbox.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (item …Run Code Online (Sandbox Code Playgroud) 我正在开发一个应用程序,它将一些json解析为listview(背景).我正在尝试处理数据不可用于填充列表视图时的错误.
我已经通过创建一个Alert Dialog在onPostExecute方法中处理了这个,如下面的代码所示
protected void onPostExecute(String args) {
progressDialog.dismiss();
//Shows alert dialog if data is unavailable
if(args != null && args.equals(noData)){
AlertDialog.Builder builder = new AlertDialog.Builder(JsonActivity.this);
builder.setTitle("Title");
builder.setMessage(noData);
AlertDialog alert = builder.create();
alert.show();
}
}
Run Code Online (Sandbox Code Playgroud)
这工作正常,但如果我单击后退按钮,它会返回到未填充的空白列表视图.
如果在显示警告对话框时按下后退按钮,我想完成活动.
我也尝试重写onBackPressed方法,但它没有用.
@Override
public void onBackPressed() {
JsonActivity.this.finish();
}
Run Code Online (Sandbox Code Playgroud)
有什么想法吗?
我试图从自定义视图中获取AlertDialog.我想我知道问题是什么,但我不知道如何解决它.我在LongClick上有一个(View V),而Button1正在使用"alertdialog"来查找数据.我在线上获得NullPointer异常TextView stax1 =(TextView)findViewById(R.id.xxx); 唯一有意义的是我让Dialog在查看"alerthelp"时查看它来自的活动.我该如何解决?
lay1.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v)
{
LayoutInflater myLayout = LayoutInflater.from(context);
View dialogView = myLayout.inflate(R.layout.alerthelp, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialogBuilder.setView(dialogView);
alertDialog.show();
Button button1 = (Button)dialogView.findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
TextView stax1=(TextView)findViewById(R.id.xxx);
String sax1 = stax1.getText().toString();
double sx1 = Double.parseDouble(sax1);
TextView textviewlay1 =(TextView)findViewById(R.id.m1ab);
String stringl1 = textviewlay1.getText().toString();
Double doubl1 = Double.parseDouble(stringl1);
TextView textviewp1 = (TextView)findViewById(R.id.inputPrice);
String stringp1 = textviewp1.getText().toString();
Double intp1 = Double.parseDouble(stringp1);
double resultl1 …Run Code Online (Sandbox Code Playgroud) android nullpointerexception layout-inflater android-alertdialog
我正在编写一个Android应用程序,我需要以编程方式在Dialog窗口按钮上放置更大的文本.
我看到有两个选项可以设置(正,负或中性)按钮AlertDialog.Builder:
setPositiveButton(CharSequence text, DialogInterface.OnClickListener listener)
setPositiveButton(int textId, DialogInterface.OnClickListener listener) 和
在选项1中,我只能设置文本,在选项2中,我可以使用文本值的资源ID.但是没有一个选项允许我添加一个样式按钮.
还有其他方法吗?
我创建了一个AlertDialog并且想要更改AlertDialog的标题颜色但是在每次尝试中都失败了.它的工作原理很细上Android 5.0与标题颜色和上面为黑色,但是当它运行下面的是Android 5.0,它的标题颜色获取的变成白色,我用了风格和许多其他来源来自互联网但失败了,我的代码如下,
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(msg)
.setCancelable(true)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
final AppCompatDialog dialog = builder.create();
dialog.setTitle("VALIDATION_TITLE error");
dialog.setCancelable(false);
dialog.show();
Run Code Online (Sandbox Code Playgroud)
我的问题通过以下方式得到解决,
dialog.setTitle( Html.fromHtml("<font color='#FF7F27'>Set IP Address</font>"))
Run Code Online (Sandbox Code Playgroud)
但我不想用这个,任何帮助都会非常感激.您可以通过此聊天链接查看我的对话框的屏幕截图