我正在测试按下按钮时下载的应用。但应用程序崩溃了。 logcat说你必须使用them.appcompat。但我使用theme.material.light。如何解决这个问题?
主要活动
public class MainActivity extends AppCompatActivity {
Button btnShowProgress;
// Progress Dialog
private ProgressDialog pDialog;
ImageView my_image;
// Progress dialog type (0 - for Horizontal progress bar)
public static final int progress_bar_type = 0;
// File url to download
private static String file_url = "http://api.androidhive.info/progressdialog/hive.jpg";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// show progress bar button
btnShowProgress = (Button) findViewById(R.id.btnProgressBar);
// Image view to show image after downloading
my_image = (ImageView) findViewById(R.id.my_image);
/**
* Show Progress bar click event
* */
btnShowProgress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// starting new Async Task
new DownloadFileFromURL().execute(file_url);
}
});
}
/**
* Showing Dialog
* */
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case progress_bar_type: // we set this to 0
pDialog = new ProgressDialog(this);
pDialog.setMessage("Downloading file. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(true);
pDialog.show();
return pDialog;
default:
return null;
}
}
/**
* Background Async Task to download file
* */
class DownloadFileFromURL extends AsyncTask<String, String, String> {
/**
* Before starting background thread
* Show Progress Bar Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}
/**
* Downloading file in background thread
* */
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
// this will be useful so that you can show a tipical 0-100% progress bar
int lenghtOfFile = conection.getContentLength();
// download the file
InputStream input = new BufferedInputStream(url.openStream(), 8192);
// Output stream
OutputStream output = new FileOutputStream("/sdcard/downloadedfile.jpg");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress(""+(int)((total*100)/lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
/**
* Updating progress bar
* */
protected void onProgressUpdate(String... progress) {
// setting progress percentage
pDialog.setProgress(Integer.parseInt(progress[0]));
}
/**
* After completing background task
* Dismiss the progress dialog
* **/
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after the file was downloaded
dismissDialog(progress_bar_type);
// Displaying downloaded image into image view
// Reading image path from sdcard
String imagePath = Environment.getExternalStorageDirectory().toString() + "/downloadedfile.jpg";
// setting downloaded into image view
my_image.setImageDrawable(Drawable.createFromPath(imagePath));
}
}
}
android清单
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.myapp2" >
<uses-permission android:name="android.permission.INTERNET" />
<!-- Permission: Writing to SDCard -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
logcat的
12-04 15:03:51.086 10939 10939 E AndroidRuntime FATAL EXCEPTION: main
12-04 15:03:51.086 10939 10939 E AndroidRuntime Process: com.mycompany.myapp, PID: 10939
12-04 15:03:51.086 10939 10939 E AndroidRuntime java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mycompany.myapp/com.mycompany.myapp.MainActivity}:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
12-04 15:03:51.086 10939 10939 E AndroidRuntime at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
12-04 15:03:51.086 10939 10939 E AndroidRuntime at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
12-04 15:03:51.086 10939 10939 E AndroidRuntime at android.app.ActivityThread.access$800(ActivityThread.java:151)
12-04 15:03:51.086 10939 10939 E AndroidRuntime at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
12-04 15:03:51.086 10939 10939 E AndroidRuntime at android.os.Handler.dispatchMessage(Handler.java:102)
12-04 15:03:51.086 10939 10939 E AndroidRuntime at android.os.Looper.loop(Looper.java:135)
12-04 15:03:51.086 10939 10939 E AndroidRuntime at android.app.ActivityThread.main(ActivityThread.java:5254)
12-04 15:03:51.086 10939 10939 E AndroidRuntime at java.lang.reflect.Method.invoke(Native Method)
12-04 15:03:51.086 10939 10939 E AndroidRuntime at java.lang.reflect.Method.invoke(Method.java:372)
12-04 15:03:51.086 10939 10939 E AndroidRuntime at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
12-04 15:03:51.086 10939 10939 E AndroidRuntime at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
12-04 15:03:51.086 10939 10939 E AndroidRuntime Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
12-04 15:03:51.086 10939 10939 E AndroidRuntime at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:309)
12-04 15:03:51.086 10939 10939 E AndroidRuntime at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:278)
12-04 15:03:51.086 10939 10939 E AndroidRuntime at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:252)
12-04 15:03:51.086 10939 10939 E AndroidRuntime at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
12-04 15:03:51.086 10939 10939 E AndroidRuntime at com.mycompany.myapp.MainActivity.onCreate(MainActivity.java:29)
12-04 15:03:51.086 10939 10939 E AndroidRuntime at android.app.Activity.performCreate(Activity.java:5990)
12-04 15:03:51.086 10939 10939 E AndroidRuntime at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
12-04 15:03:51.086 10939 10939 E AndroidRuntime at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
答案 0 :(得分:1)
您必须使用 AppCompat主题。
您的 style.xml 应如下所示:
lib1
sample
tests
setup.py
lib2
sample
tests
setup.py
希望这会对你有所帮助。
答案 1 :(得分:1)
您使用的当前主题是活动类
因为您已将其更改为AppCompatActivity,您需要使用
AppCompatActivity支持的主题。
只需从 style.xml 自定义 AppTheme 即可使用