为什么我得到java.lang.RuntimeException:无法启动活动ComponentInfo

时间:2016-08-14 19:03:38

标签: java android android-volley auto-update

大家好我在网上关注教程之后进行自动更新,然后根据我的需要编辑代码..但是我的logcat中出现了这个错误

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.harrops.h20droidapp/com.example.harrops.h20droidapp.Homescreen}: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.harrops.h20droidapp/com.example.harrops.h20droidapp.UpdateService}; have you declared this activity in your AndroidManifest.xml?
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)

有人可以帮助我吗

这是我的服务类

public class UpdateService extends Service {
 public UpdateService() {

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
    String url = "<MY Link for version>";
    StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

            if (response != null) {
                boolean resp = response.contains("<div class='post-body entry-content' id='post-body-6791062644900393367' itemprop='description articleBody'>\n" +
                        "1.1.8\n" +
                        "<div style='clear: both;'></div>");
                if (!resp) {
                    //Dialog to show update
                    Intent intent1 = new Intent(UpdateService.this, UpdateDialog.class);
                    intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent1);

                } else {
                    Toast.makeText(UpdateService.this, "No New Update Found..", Toast.LENGTH_LONG).show();
                }
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    });
    queue.add(stringRequest);

    return Service.START_NOT_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();
}

@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}
 }

然后这是我的对话类

 public static Button btn;
public static String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Update";
public static File Dir = new File(path);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AndroidAuthSession session = buildSession();
    dropboxAPI = new DropboxAPI<AndroidAuthSession>(session);

    Dir.mkdir();

    final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setIcon(R.mipmap.ic_launcher);
    alertDialog.setTitle("update");
    alertDialog.setMessage("New update Available...");
    alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "UPDATE", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            DownloadFromDropboxFromPath(path + "downloadFileFromDropbox", "Update/Myupdate.apk");


        }
    });
    alertDialog.show();
}




static DropboxAPI<AndroidAuthSession> dropboxAPI;
private static final String APP_KEY = "********* **** ** **";
private static final String APP_SECRET = "XXX XX X X XX X";
private static final String ACCESSTOKEN = "xx x x xxx x x x x";
private DropboxAPI.UploadRequest request;

private AndroidAuthSession buildSession() {
    AppKeyPair appKeyPair = new AppKeyPair(APP_KEY, APP_SECRET);
    AndroidAuthSession session = new AndroidAuthSession(appKeyPair);
    session.setOAuth2AccessToken(ACCESSTOKEN);
    return session;
}

static final int UploadFromSelectApp = 9501;
static final int UploadFromFilemanager = 9502;
public static String DropboxUploadPathFrom = "";
public static String DropboxUploadName = "";
public static String DropboxDownloadPathFrom = "";
public static String DropboxDownloadPathTo = "";

private void UploadToDropboxFromPath(String uploadPathFrom, String uploadPathTo) {
    Toast.makeText(getApplicationContext(), "Upload file ...", Toast.LENGTH_SHORT).show();
    final String uploadPathF = uploadPathFrom;
    final String uploadPathT = uploadPathTo;
    Thread th = new Thread(new Runnable() {
        public void run() {
            File tmpFile = null;
            try {
                tmpFile = new File(uploadPathF);
            } catch (Exception e) {
                e.printStackTrace();
            }
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(tmpFile);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            try {
                dropboxAPI.putFileOverwrite(uploadPathT, fis, tmpFile.length(), null);
            } catch (Exception e) {
            }
            getMain().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(), "File successfully uploaded.", Toast.LENGTH_SHORT).show();
                }
            });
        }
    });
    th.start();
}

private void UploadToDropboxFromSelectedApp(String uploadName) {
    DropboxUploadName = uploadName;
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("*/*");
    startActivityForResult(Intent.createChooser(intent, "Upload from ..."), UploadFromSelectApp);
}

private void UploadToDropboxFromFilemanager(String uploadName) {
    DropboxUploadName = uploadName;
    Intent intent = new Intent("com.sec.android.app.myfiles.PICK_DATA");
    intent.putExtra("CONTENT_TYPE", "*/*");
    intent.addCategory(Intent.CATEGORY_DEFAULT);
    startActivityForResult(intent, UploadFromFilemanager);
}

private void DownloadFromDropboxFromPath(String downloadPathTo, String downloadPathFrom) {
    DropboxDownloadPathTo = downloadPathTo;
    DropboxDownloadPathFrom = downloadPathFrom;
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(getApplicationContext(), "Download file ...", Toast.LENGTH_SHORT).show();
            Thread th = new Thread(new Runnable() {
                public void run() {
                    File file = new File(DropboxDownloadPathTo + DropboxDownloadPathFrom.substring(DropboxDownloadPathFrom.lastIndexOf('.')));
                    if (file.exists()) file.delete();
                    try {
                        FileOutputStream outputStream = new FileOutputStream(file);
                        UpdateDialog.dropboxAPI.getFile(DropboxDownloadPathFrom, null, outputStream, null);
                        getMain().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(getApplicationContext(), "File successfully downloaded.", Toast.LENGTH_SHORT).show();
                            }
                        });
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            th.start();
        }
    });
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == UploadFromFilemanager) {
        final Uri currFileURI = intent.getData();
        final String pathFrom = currFileURI.getPath();
        Toast.makeText(getApplicationContext(), "Upload file ...", Toast.LENGTH_SHORT).show();
        Thread th = new Thread(new Runnable() {
            public void run() {
                getMain().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        UploadToDropboxFromPath(pathFrom, "/db-test/" + DropboxUploadName + pathFrom.substring(pathFrom.lastIndexOf('.')));
                        Toast.makeText(getApplicationContext(), "File successfully uploaded.", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
        th.start();
    }
    if (requestCode == UploadFromSelectApp) {
        Toast.makeText(getApplicationContext(), "Upload file ...", Toast.LENGTH_SHORT).show();
        final Uri uri = intent.getData();

        DropboxUploadPathFrom = getPath(getApplicationContext(), uri);
        if (DropboxUploadPathFrom == null) {
            DropboxUploadPathFrom = uri.getPath();
        }
        Thread th = new Thread(new Runnable() {
            public void run() {
                try {
                    final File file = new File(DropboxUploadPathFrom);
                    InputStream inputStream = getContentResolver().openInputStream(uri);

                    dropboxAPI.putFile("/db-test/" + DropboxUploadName + file.getName().substring(file.getName().lastIndexOf("."),
                            file.getName().length()), inputStream, file.length(), null, new ProgressListener() {
                        @Override
                        public long progressInterval() {
                            return 100;
                        }

                        @Override
                        public void onProgress(long arg0, long arg1) {
                        }
                    });
                    getMain().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(), "File successfully uploaded.", Toast.LENGTH_SHORT).show();
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        th.start();
    }
    super.onActivityResult(requestCode, resultCode, intent);
}

public String getPath(Context context, Uri contentUri) {
    Cursor cursor = null;
    try {
        String[] proj = {MediaStore.Images.Media.DATA, MediaStore.Video.Media.DATA, MediaStore.Audio.Media.DATA};
        cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String s = cursor.getString(column_index);
        if (s != null) {
            cursor.close();
            return s;
        }
    } catch (Exception e) {
    }
    try {
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
        cursor.moveToFirst();
        String s = cursor.getString(column_index);
        if (s != null) {
            cursor.close();
            return s;
        }
    } catch (Exception e) {
    }
    try {
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
        cursor.moveToFirst();
        String s = cursor.getString(column_index);
        cursor.close();
        return s;
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

public UpdateDialog getMain() {
    return this;
}

} 最后我在我的主屏幕课上调用它。有意图

        Intent intent = new Intent(Homescreen.this, UpdateService.class);
        startActivity(intent);

3 个答案:

答案 0 :(得分:3)

UpdateServiceService。它不是Activity。要启动服务,请致电startService(),而不是startActivity()

答案 1 :(得分:1)

由于UpdateService是一项服务,您必须通过startService()方法启动它,如上面提到的@CommonsWare。另外,不要忘记在Manifest文件中添加您的服务。

答案 2 :(得分:0)

UpdateService不是一项活动,它是一个扩展服务的类。

我认为你是一个非常初学者。 请在使用Android之前先去Udacity学习概念,否则,你将浪费大量时间寻找复制粘贴解决方案。