【问题标题】:How to use Alert Dialogue inside uiThread?如何在 uiThread 中使用警报对话?
【发布时间】:2016-03-24 14:16:04
【问题描述】:

以下是 ConnectActivity 的代码片段,它扩展了尝试创建 AlertDialogue 的 AppCompatActivity

new AsyncTask < Void, Void, Boolean > () {
  @Override
  protected Boolean doInBackground(Void...params) {
   try {
    Runnable r = new Runnable() {
     public void run() {

      while (true) {
       Socket socket = null;
       try {
        socket = new Socket("192.168.0.32", 11311);
        Log.i("CON", "Connected!");
        socket.close();
       } catch (Exception e) {
        Log.i("CON", "Disconnected!");

        runOnUiThread(new Runnable() {
         public void run() {
          AlertDialog alertDialog;
          alertDialog = new AlertDialog.Builder(getApplicationContext()).create();
          alertDialog.setTitle("Network error");
          alertDialog.setMessage("Check network connection and try again.");
          alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "ok", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
            finish();
           }
          });
          alertDialog.show();
         }
        });

        break;

       }
       try {
        wait(5000);
       } catch (Exception e) {

       }

      }

     }
    };
    Thread t = new Thread(r);
    t.start();
    return true;
   } catch (Exception e) {

    return false;
   }
  }

AlertDialog.Builder(getApplicationContext()).create(); 行给出错误:

java.lang.IllegalStateException:您需要在此活动中使用 Theme.AppCompat 主题(或后代)。

所以我尝试用AlertDialog.Builder(this).create(); 替换这一行,但这里的this 代表Runnable 而不是上下文。我应该如何更正此代码?

Styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/PrimaryColor</item>
        <item name="colorPrimaryDark">@color/PrimaryDarkColor</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:textColorPrimary">#ffffff</item>
        <item name="android:windowContentOverlay">@null</item>
    </style>
</resources>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="edu.academy.cs573.netg">

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        tools:replace="android:icon"
        android:icon="@mipmap/ic_launch"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:name="android.support.multidex.MultiDexApplication"
        android:theme="@style/SplashTheme">
        <activity android:name=".SplashActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>



        <activity android:name=".MainActivity"
            android:theme="@style/AppTheme"/>
        <activity android:name=".AboutActivity"
            android:theme="@style/AppTheme"/>
        <activity android:name=".LicenseInfoActivity"
            android:theme="@style/AppTheme"/>

        <activity
            android:name=".ConnectActivity"
            android:label="@string/app_name"
            android:launchMode="standard"
            android:theme="@style/AppTheme"/>

        <service android:name=".NodeMainExecutorService" >
            <intent-filter>
                <action android:name=".NodeMainExecutorService" />
            </intent-filter>
        </service>



    </application>

</manifest>

【问题讨论】:

  • 提供style.xml文件的内容
  • 使用 asynctask 我建议所有 UI 更改必须在 onPostexecute 方法中..
  • 您是否使用 ActionBarActivity 扩展了您的活动?

标签: android multithreading


【解决方案1】:

像这样在此处传递活动上下文

alertDialog = new AlertDialog.Builder(YourActivityName.this).create();

如果您想传递应用程序上下文,请为您的应用程序使用 app comapact 主题。这在 styles.xml 文件中明确定义

但您需要在onProgressUpdate() 方法中显示对话框。你不需要运行 ui 线程。当异步任务已经有一种方法可以在 ui 上显示一些输出时。

【讨论】:

  • 谢谢,这对我有用。我会在 onProgressUpdate() 中尝试你的建议。
【解决方案2】:

最好在调用AsyncTask 之前调用create AlertDialog。 你可以在打电话取东西之前检查网络连接。

alertDialog = new AlertDialog.Builder(YourAcitvity.this, android.R.style.Theme_Material_Light_Dialog_Alert).create();

此外,您可以为警报对话框提供主题(android.R.style.Theme_Material_Light_Dialog_Alert).

【讨论】:

    【解决方案3】:

    尝试在应用标签中添加这个

    android:theme="@style/Theme.AppCompat.Light"
    

    在您的清单中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-27
      • 2023-03-25
      • 2013-01-17
      • 2021-10-11
      • 2013-09-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多