下面是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表示可运行的,而不是上下文。我该如何纠正这段代码?
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>发布于 2016-03-24 06:06:05
在此传递活动上下文,如
alertDialog = new AlertDialog.Builder(YourActivityName.this).create();如果您想传递应用程序上下文,那么为您的应用程序使用app comapact主题。它明确地在styles.xml文件中定义。
但是您需要在onProgressUpdate()方法中显示对话框。您不需要运行ui线程。当异步任务已经有了在ui上显示某些输出的方法时。
https://stackoverflow.com/questions/36194058
复制相似问题