首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在uiThread内部使用警报对话?

如何在uiThread内部使用警报对话?
EN

Stack Overflow用户
提问于 2016-03-24 05:54:12
回答 3查看 372关注 0票数 0

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

代码语言:javascript
复制
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表示可运行的,而不是上下文。我该如何纠正这段代码?

Styles.xml

代码语言:javascript
复制
<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

代码语言:javascript
复制
<?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>
EN

Stack Overflow用户

回答已采纳

发布于 2016-03-24 06:06:05

在此传递活动上下文,如

代码语言:javascript
复制
alertDialog = new AlertDialog.Builder(YourActivityName.this).create();

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

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

票数 0
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36194058

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档