2016-08-14 191 views
1

嗨,大家即時進行自動更新後,在線教程,然後編輯代碼,以滿足我的需要..但我得到這個錯誤在我的logcat爲什麼我得到th java.lang.RuntimeException:無法啓動活動ComponentInfo

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

​​是Service。它不是Activity。要啓動服務,請撥打startService(),而不是startActivity()

+0

謝謝@CommonWare –

0

UpdateService不是一個活動,它是一個擴展服務的類。

我認爲你是一個非常初學者。 請在Undacity使用android之前先了解一下概念,否則,你會浪費大量時間來尋找複製粘貼解決方案。

+0

因爲我得到了一行代碼錯誤。我是初學者嗎?哈哈哈你甚至不能拼寫單詞..感謝您的幫助 –

+0

:|不要讓你的自我:| – codepeaker

1

由於​​是一項服務,您必須從startService()開始,像上面提到的@CommonsWare。另外不要忘記在Manifest文件中添加你的服務。

相關問題