- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试启动异步任务
但是当我运行应用程序时, Activity 崩溃了
我的任务:
private class HideTask extends AsyncTask<String, Void, String> {
protected void onPreExecute() {
//loader.show();
}
protected void onPostExecute(String result) {
//loader.dismiss();
}
@Override
protected String doInBackground(String... params) {
//hide();
return "done";
}
}
我怎么调用它:
HideTask task = new HideTask();
task.execute(new String[] { "" });
日志猫:
02-11 19:36:24.799: E/AndroidRuntime(17870): FATAL EXCEPTION: main 02-11 19:36:24.799: E/AndroidRuntime(17870): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{folder.hide.alexander.fuchs/folder.hide.alexander.fuchs.App}: java.lang.IllegalStateException: System services not available to Activities before onCreate() 02-11 19:36:24.799: E/AndroidRuntime(17870): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573) 02-11 19:36:24.799: E/AndroidRuntime(17870): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 02-11 19:36:24.799: E/AndroidRuntime(17870): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 02-11 19:36:24.799: E/AndroidRuntime(17870): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 02-11 19:36:24.799: E/AndroidRuntime(17870): at android.os.Handler.dispatchMessage(Handler.java:99) 02-11 19:36:24.799: E/AndroidRuntime(17870): at android.os.Looper.loop(Looper.java:130) 02-11 19:36:24.799: E/AndroidRuntime(17870): at android.app.ActivityThread.main(ActivityThread.java:3691) 02-11 19:36:24.799: E/AndroidRuntime(17870): at java.lang.reflect.Method.invokeNative(Native Method) 02-11 19:36:24.799: E/AndroidRuntime(17870): at java.lang.reflect.Method.invoke(Method.java:507) 02-11 19:36:24.799: E/AndroidRuntime(17870): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907) 02-11 19:36:24.799: E/AndroidRuntime(17870): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665) 02-11 19:36:24.799: E/AndroidRuntime(17870): at dalvik.system.NativeStart.main(Native Method) 02-11 19:36:24.799: E/AndroidRuntime(17870): Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate() 02-11 19:36:24.799: E/AndroidRuntime(17870): at android.app.Activity.getSystemService(Activity.java:3562) 02-11 19:36:24.799: E/AndroidRuntime(17870): at android.app.Dialog.(Dialog.java:141) 02-11 19:36:24.799: E/AndroidRuntime(17870): at android.app.AlertDialog.(AlertDialog.java:63) 02-11 19:36:24.799: E/AndroidRuntime(17870): at android.app.ProgressDialog.(ProgressDialog.java:80) 02-11 19:36:24.799: E/AndroidRuntime(17870): at android.app.ProgressDialog.(ProgressDialog.java:76) 02-11 19:36:24.799: E/AndroidRuntime(17870): at android.app.ProgressDialog.show(ProgressDialog.java:101) 02-11 19:36:24.799: E/AndroidRuntime(17870): at android.app.ProgressDialog.show(ProgressDialog.java:90) 02-11 19:36:24.799: E/AndroidRuntime(17870): at folder.hide.alexander.fuchs.App.(App.java:58) 02-11 19:36:24.799: E/AndroidRuntime(17870): at java.lang.Class.newInstanceImpl(Native Method) 02-11 19:36:24.799: E/AndroidRuntime(17870): at java.lang.Class.newInstance(Class.java:1409) 02-11 19:36:24.799: E/AndroidRuntime(17870): at android.app.Instrumentation.newActivity(Instrumentation.java:1021) 02-11 19:36:24.799: E/AndroidRuntime(17870): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565) 02-11 19:36:24.799: E/AndroidRuntime(17870): ... 11 more
public class App extends Activity implements OnClickListener {
private Button button;
private Button options;
private EditText edittext;
private TextView folder_to_hide;
Dialog progress;
String hide_or_show;
String password;
String pass;
String directory;
ProgressDialog loader = ProgressDialog.show(App.this, "",
"Loading. Please wait...", true);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Button button;
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(this);
options = (Button) findViewById(R.id.button2);
options.setOnClickListener(this);
// EditText edittext
edittext = (EditText) findViewById(R.id.editText1);
//TextView folder_to_hide;
folder_to_hide = (TextView) findViewById(R.id.textView1);
init();
dialog();
rate();
}
private class HideTask extends AsyncTask<String, Void, String> {
protected void onPreExecute() {
//loader.show();
}
protected void onPostExecute(String result) {
//loader.dismiss();
}
@Override
protected String doInBackground(String... params) {
//hide();
return "done";
}
}
public void init() {
try
{
boolean exists2 = (new File("/data/data/folder.hide.alexander.fuchs/folder.db")).exists();
if (exists2) {
edittext.setText(deserializeString("/data/data/folder.hide.alexander.fuchs/folder.db"));
}
else {
edittext.setText("/sdcard/Hide");
createfolder("/sdcard/Hide");
}
boolean exists = (new File("/data/data/folder.hide.alexander.fuchs/hide.db")).exists();
if (exists) {
edittext.setVisibility(View.INVISIBLE);
folder_to_hide.setVisibility(View.INVISIBLE);
hide_or_show = "show";
button.setText("Show");
} else {
hide_or_show = "hide";
button.setText("Hide");
}
}
catch(Exception x)
{
}
}
public void createfolder(String path) {
try
{
new File(path).mkdirs();
}
catch(Exception x)
{
finish();
}
}
// Deletes all files and subdirectories under dir.
// Returns true if all deletions were successful.
// If a deletion fails, the method stops attempting to delete and returns false.
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
// The directory is now empty so delete it
return dir.delete();
}
/**
public void copyDirectory(File sourceLocation , File targetLocation)
throws IOException {
if (sourceLocation.isDirectory()) {
if (!targetLocation.exists()) {
targetLocation.mkdir();
}
String[] children = sourceLocation.list();
for (int i=0; i<children.length; i++) {
copyDirectory(new File(sourceLocation, children[i]),
new File(targetLocation, children[i]));
}
} else {
InputStream in = new FileInputStream(sourceLocation);
OutputStream out = new FileOutputStream(targetLocation);
// Copy the bits from instream to outstream
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
}
**/
public static void copyFolder(File src, File dest)
throws IOException{
if(src.isDirectory()){
//if directory not exists, create it
if(!dest.exists()){
dest.mkdir();
System.out.println("Directory copied from "
+ src + " to " + dest);
}
//list all the directory contents
String files[] = src.list();
for (String file : files) {
//construct the src and dest file structure
File srcFile = new File(src, file);
File destFile = new File(dest, file);
//recursive copy
copyFolder(srcFile,destFile);
}
}else{
//if file, then copy it
//Use bytes stream to support all file types
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
//copy the file content in bytes
while ((length = in.read(buffer)) > 0){
out.write(buffer, 0, length);
}
in.close();
out.close();
System.out.println("File copied from " + src + " to " + dest);
}
}
@Override
public void onClick(View v) {
if (v == button)
{
try
{
directory = edittext.getText().toString();
FileWriter fstream = new FileWriter("/data/data/folder.hide.alexander.fuchs/folder.db");
BufferedWriter out = new BufferedWriter(fstream);
out.write(directory);
//Close the output stream
out.close();
if(hide_or_show == "hide")
{
HideTask task = new HideTask();
task.execute(new String[] { "http://www.vogella.de" });
edittext.setVisibility(View.INVISIBLE);
folder_to_hide.setVisibility(View.INVISIBLE);
}
else
{
show();
edittext.setVisibility(View.VISIBLE);
folder_to_hide.setVisibility(View.VISIBLE);
}
}
catch(Exception x)
{
String ErrorMessage = x.getMessage();
Toast.makeText(this,"Error"+ErrorMessage, Toast.LENGTH_LONG).show();
finish();
}
}
if (v == options)
{
final CharSequence[] items = {"Change password", "http://www.alexander-fuchs.net/", "Market"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Options");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (items[item] == "Change password")
{
createpass();
}
if (items[item] == "http://www.alexander-fuchs.net/")
{
intentstarter(items[item].toString());
toaster(items[item].toString());
}
if (items[item] == "Market")
{
intentstarter("market://search?q=pub:Alexander Fuchs");
toaster("Please wait...");
}
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
public void hide() {
try
{
FileWriter fstream = new FileWriter("/data/data/folder.hide.alexander.fuchs/hide.db");
BufferedWriter out = new BufferedWriter(fstream);
out.write("hide");
//Close the output stream
out.close();
File copy = new File(directory);
File target = new File("/data/data/folder.hide.alexander.fuchs/Hide");
copyFolder(copy,target);
deleteDir(copy);
hide_or_show = "show";
button.setText("Show");
}
catch(Exception x)
{
edittext.setVisibility(View.VISIBLE);
folder_to_hide.setVisibility(View.VISIBLE);
File copy = new File("/data/data/folder.hide.alexander.fuchs/folder.db");
deleteDir(copy);
copy = new File("/data/data/folder.hide.alexander.fuchs/hide.db");
deleteDir(copy);
String ErrorMessage = x.getMessage();
Toast.makeText(this,"Error", Toast.LENGTH_LONG).show();
finish();
}
}
public void show() {
try
{
File delete = new File("/data/data/folder.hide.alexander.fuchs/hide.db");
deleteDir(delete);
File target = new File(directory);
File copy = new File("/data/data/folder.hide.alexander.fuchs/Hide");
copyFolder(copy,target);
deleteDir(copy);
hide_or_show = "hide";
button.setText("Hide");
}
catch(Exception x)
{
String ErrorMessage = x.getMessage();
Toast.makeText(this,"Error", Toast.LENGTH_LONG).show();
finish();
}
}
public void dialog() {
//set up dialog
final Dialog dialog = new Dialog(App.this);
dialog.setContentView(R.layout.dialoglayout);
dialog.setTitle("Password");
dialog.setCancelable(false);
//there are a lot of settings, for dialog, check them all out!
//set up text
final EditText text = (EditText) dialog.findViewById(R.id.editText1);
text.setText("");
//set up button
Button button = (Button) dialog.findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
password = text.getText().toString();
try
{
pass = deserializeString("/data/data/folder.hide.alexander.fuchs/password.db");
}
catch(Exception x)
{
String ErrorMessage = x.getMessage();
toaster("Error");
finish();
}
if(password.equals(pass))
{
dialog.dismiss();
}
else
{
toaster("Invalid Password");
finish();
}
}
});
//now that the dialog is set up, it's time to show it
boolean exists = (new File("/data/data/folder.hide.alexander.fuchs/password.db")).exists();
if (exists) {
dialog.show();
}
else
{
createpass();
}
}
public static String deserializeString(String file)
throws IOException {
int len;
char[] chr = new char[4096];
final StringBuffer buffer = new StringBuffer();
final FileReader reader = new FileReader(file);
try {
while ((len = reader.read(chr)) > 0) {
buffer.append(chr, 0, len);
}
} finally {
reader.close();
}
return buffer.toString();
}
public void toaster(String text) {
Toast.makeText(this,text, Toast.LENGTH_LONG).show();
}
public void intentstarter(String string) {
String url = string;
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
public void dialogbuilder(String text) {
new AlertDialog.Builder(this).setMessage(
text).setNeutralButton(
"OK",
null).show();
}
public void createpass() {
//set up dialog
final Dialog dialog = new Dialog(App.this);
dialog.setContentView(R.layout.createpass);
dialog.setTitle("Set Password");
dialog.setCancelable(false);
//there are a lot of settings, for dialog, check them all out!
//set up text
final EditText text = (EditText) dialog.findViewById(R.id.editText1);
text.setText("");
//set up text
final EditText text2 = (EditText) dialog.findViewById(R.id.editText2);
text2.setText("");
//set up button
Button button = (Button) dialog.findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String createpass_password = text.getText().toString().trim();
String createpass_password2 = text2.getText().toString().trim();
try
{
if(createpass_password.equals(createpass_password2))
{
FileWriter fstream = new FileWriter("/data/data/folder.hide.alexander.fuchs/password.db");
BufferedWriter out = new BufferedWriter(fstream);
out.write(createpass_password);
//Close the output stream
out.close();
dialog.dismiss();
dialogbuilder("Please notice that if you forget your password there is no availability to restore your data !");
}
else
{
toaster("Passwords are not matching !");
text.setText("");
text2.setText("");
}
}
catch(Exception x)
{
String ErrorMessage = x.getMessage();
toaster("Error");
finish();
}
}
});
//now that the dialog is set up, it's time to show it
dialog.show();
}
public void rate() {
//set up dialog
final Dialog dialog = new Dialog(App.this);
dialog.setContentView(R.layout.vote);
dialog.setTitle("Like this App ?");
dialog.setCancelable(true);
//there are a lot of settings, for dialog, check them all out!
//Rate
Button button = (Button) dialog.findViewById(R.id.button1);
//Later
Button button2 = (Button) dialog.findViewById(R.id.button2);
//No Thanks
Button button3 = (Button) dialog.findViewById(R.id.button3);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try
{
// /data/data/folder.hide.alexander.fuchs/rate.db
FileWriter fstream = new FileWriter("/data/data/folder.hide.alexander.fuchs/rate.db");
BufferedWriter out = new BufferedWriter(fstream);
out.write("no");
//Close the output stream
out.close();
dialog.dismiss();
}
catch (Exception x)
{
}
intentstarter("market://details?id=folder.hide.alexander.fuchs");
toaster("Please wait...");
dialog.dismiss();
}
});
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// /data/data/folder.hide.alexander.fuchs/rate.db
File dta = new File("/data/data/folder.hide.alexander.fuchs/rate.db");
deleteDir(dta);
dialog.dismiss();
}
});
button3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try
{
// /data/data/folder.hide.alexander.fuchs/rate.db
FileWriter fstream = new FileWriter("/data/data/folder.hide.alexander.fuchs/rate.db");
BufferedWriter out = new BufferedWriter(fstream);
out.write("no");
//Close the output stream
out.close();
dialog.dismiss();
}
catch (Exception x)
{
}
}
});
if (rate.rate() == 3)
{
dialog.show();
}
}
}
最佳答案
你唯一要做的就是:
new HideTask().execute();
您没有带参数的构造函数,因此无法像这样在其中传递字符串数组。
当您发布有关应用程序崩溃的 Android 问题时,发布 LogCat 始终是一个好主意:)
关于java - 让 Asynctask 开始工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9242755/
我今天在我的 Android 项目中遇到了一个涉及 aSyncTasks 的问题,经过一些研究找到了答案,并且与我交谈过的人都没有意识到,所以我想我会与 SO 社区分享,以防万一有人发现它有任何用处。
我目前正在 AsyncTask 的 onPostExecute 方法中执行类似的操作,其中 NewTask 不是当前正在执行的任务: private class OlderTask extends A
所以,我正在研究一个条形码解码器,一旦我们有了条形码,它就会通过互联网进入多个 API 来解码刚刚扫描的内容。问题是我必须将一些 XML 解析链接在一起,我不知道我做的是否正确。 因此,扫描条形码后,
我的团队开发了一个新的 Android 应用程序,它广泛使用了 Room。 我不确定我们是否正确使用 AsyncTask。 我们不得不在 AsyncTasks 中包装所有对 insert/update
我查看了其他问题,但未能澄清我对从另一个任务调用任务的疑问,我有以下代码: protected List doInBackground(String... params) try {
我正在开发一个访问 Web 服务的应用程序,并使用从中获取的 JSON 创建一个对象并在我的代码中使用它。尽管我的应用程序正在运行,但我不知道它是否写得很好且完美无缺。 我将解释我的内容,然后放置一些
虽然我还没有尝试过,但从理论上讲,我问这个问题只是为了消除我的疑虑。 我有这样一个场景:1. 向服务器发送请求并接收 JSON 响应。为此,我正在使用 AsyncTask,因为接收响应可能会有延迟。2
我有以下 AsyncTask 的实现,允许多个 AsyncTask 同时运行: public abstract class MyAsyncTask extends AsyncTask { pu
花了很多时间试图解决这个问题,我已经阅读了很多问题、论坛、答案......但它仍然不会更新 UI。 我的最终目标是从用户那里获取一个搜索词,并将一个 httprequest 发送到用 JSON 回复的
我有一个异步任务 private class LoadData extends AsyncTask { private String WEBURL; LoadData(String u
我正在用 android 做一些编码工作。我几乎遇到了一个问题,为了解决这个问题,我需要一个匿名 AsyncTask 类来执行。但我还需要在执行之前传递并反对这个类。我尝试了下面的代码,但它不起作用,
我有两个 AsyncTask:第一个寻找素数,如果成功,我必须调用第二个 AsyncTask 显示单词“Yop!” (将这个词添加到数组列表中,并显示在AsyncTask三)。 如果我从 onProg
我想使用 AsyncTask 将图像加载到 ListView。 private class LoadImageTask extends AsyncTask,Void,Bitmap>{ @Sup
在我的 AsyncTask 的某个时刻,在完成一些验证之后,我需要派生另一个线程来做一些其他工作。所以我现在想要两个后台线程,每个都做自己的事情(每个执行大约 2-3 秒)。我们的想法是最大限度地提高
(这与空指针无关):我在 AsyncTask 中有一个进度条,并且添加了一个取消按钮来取消 asynctask。 我可以从异步任务外部取消异步任务,但我需要在异步任务下实现的progressdialo
我有一个 Activity ,在启动时调用“json”来获取歌曲的数据类别,之后我调用方法“AsyncTask”来获取来自另一个“JSON”的歌曲列表问题是,当我启动 Activity 时,它被锁定,
我想做以下事情。我想显示一个包含信息和图像的列表。这些图像需要一段时间才能加载,所以我想我会采取不同的方式。我会使用两个 AsyncTasks。第一个创建所有布局并用除图像之外的数据填充它。第二个只是
如果我做了这样的事情: public class MyFragment extends Fragment { private GetDataTask mGDT; //onCreat
在 Android Activity 中,我在 onCreate 方法中执行 AsyncTask。我应该在 AsyncTask 的 onPostExecute 中还是在 OnCreate 方法中声明
我对 AsyncTask 有疑问我尝试为 10 个 Json 文件向互联网打开 10 个请求,因此我读取它并将其保存到用户设备_由于数据差异,此文件必须分开,包括。 那么,将每个请求放在单个 Asyn
我是一名优秀的程序员,十分优秀!