在尝试执行从另一个类调用的Asynctask备份例程时,我正在收到消息 “无法实例化活动无法转换为android.app.Activity。” 编译正常,但执行失败。 我对Intent做了一个研究,但是找不到我的出路。 因为我打算做其他课程,所以知道发生了什么事对我很重要 在应用程序的生命周期中调用Asynctasks。 如果需要更多数据,请告诉我。源代码相当大,我认为没有意义 在倾销最终无用的信息。我宁愿发送与此问题相关的内容 谢谢
第一个电话:
public class MainActivity extends Activity implements OnClickListener {
} else if (v == backupDbButton){
startActivity (new Intent(this, CheckSDDB.class));
第二个电话:
public class CheckSDDB extends Activity {
Intent intent = new Intent(CheckSDDB.this,RunBackup.class);
startActivity(intent);
最后一个组件:
public class RunBackup extends AsyncTask<Void, Void, Integer> {
日志:
02-21 15:14:15.067: E/AndroidRuntime(18123): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.peter.databasetest/com.peter.databasetest.RunBackup}: java.lang.ClassCastException: com.peter.databasetest.RunBackup cannot be cast to android.app.Activity 02-21 15:14:15.067: E/AndroidRuntime(18123): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
02-21 15:14:15.067: E/AndroidRuntime(18123): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
02-21 15:14:15.067: E/AndroidRuntime(18123): at android.app.ActivityThread.access$600(ActivityThread.java:130)
02-21 15:14:15.067: E/AndroidRuntime(18123): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
02-21 15:14:15.067: E/AndroidRuntime(18123): at android.os.Handler.dispatchMessage(Handler.java:99)
02-21 15:14:15.067: E/AndroidRuntime(18123): at android.os.Looper.loop(Looper.java:137)
02-21 15:14:15.067: E/AndroidRuntime(18123): at android.app.ActivityThread.main(ActivityThread.java:4745)
02-21 15:14:15.067: E/AndroidRuntime(18123): at java.lang.reflect.Method.invokeNative(Native Method)
02-21 15:14:15.067: E/AndroidRuntime(18123): at java.lang.reflect.Method.invoke(Method.java:511)
02-21 15:14:15.067: E/AndroidRuntime(18123): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-21 15:14:15.067: E/AndroidRuntime(18123): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-21 15:14:15.067: E/AndroidRuntime(18123): at dalvik.system.NativeStart.main(Native Method)
02-21 15:14:15.067: E/AndroidRuntime(18123): Caused by: java.lang.ClassCastException: com.peter.databasetest.RunBackup cannot be cast to android.app.Activity
02-21 15:14:15.067: E/AndroidRuntime(18123): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
02-21 15:14:15.067: E/AndroidRuntime(18123): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
02-21 15:14:15.067: E/AndroidRuntime(18123): ... 11 more
Manifest (partial)
<activity
android:name="com.peter.databasetest.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>
<activity
android:name=".RunBackup"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.peter.databasetest.RUNBACKUP" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".CheckSDDB"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.CHECKSDDB" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
@mike yaworski:我从(this,CheckSDDB.class)改变了这一行。我没有看到执行中的任何变化。无论如何,谢谢你的建议。
@DuZi通过从Manifest中删除Runbackup,我得到了这个。请参阅下面的日志。
02-24 07:39:43.745: E/AndroidRuntime(5069): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.peter.databasetest/com.peter.databasetest.CheckSDDB}: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.peter.databasetest/com.peter.databasetest.RunBackup}; have you declared this activity in your AndroidManifest.xml?
02-24 07:39:43.745: E/AndroidRuntime(5069): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
02-24 07:39:43.745: E/AndroidRuntime(5069): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
02-24 07:39:43.745: E/AndroidRuntime(5069): at android.app.ActivityThread.access$600(ActivityThread.java:130)
02-24 07:39:43.745: E/AndroidRuntime(5069): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
02-24 07:39:43.745: E/AndroidRuntime(5069): at android.os.Handler.dispatchMessage(Handler.java:99)
02-24 07:39:43.745: E/AndroidRuntime(5069): at android.os.Looper.loop(Looper.java:137)
02-24 07:39:43.745: E/AndroidRuntime(5069): at android.app.ActivityThread.main(ActivityThread.java:4745)
02-24 07:39:43.745: E/AndroidRuntime(5069): at java.lang.reflect.Method.invokeNative(Native Method)
02-24 07:39:43.745: E/AndroidRuntime(5069): at java.lang.reflect.Method.invoke(Method.java:511)
02-24 07:39:43.745: E/AndroidRuntime(5069): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-24 07:39:43.745: E/AndroidRuntime(5069): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-24 07:39:43.745: E/AndroidRuntime(5069): at dalvik.system.NativeStart.main(Native Method)
02-24 07:39:43.745: E/AndroidRuntime(5069): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.peter.databasetest/com.peter.databasetest.RunBackup}; have you declared this activity in your AndroidManifest.xml?
02-24 07:39:43.745: E/AndroidRuntime(5069): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1541)
02-24 07:39:43.745: E/AndroidRuntime(5069): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1416)
02-24 07:39:43.745: E/AndroidRuntime(5069): at android.app.Activity.startActivityForResult(Activity.java:3351)
02-24 07:39:43.745: E/AndroidRuntime(5069): at android.app.Activity.startActivityForResult(Activity.java:3312)
02-24 07:39:43.745: E/AndroidRuntime(5069): at android.app.Activity.startActivity(Activity.java:3522)
02-24 07:39:43.745: E/AndroidRuntime(5069): at android.app.Activity.startActivity(Activity.java:3490)
02-24 07:39:43.745: E/AndroidRuntime(5069): at com.peter.databasetest.CheckSDDB.onCreate(CheckSDDB.java:134)
02-24 07:39:43.745: E/AndroidRuntime(5069): at android.app.Activity.performCreate(Activity.java:5008)
02-24 07:39:43.745: E/AndroidRuntime(5069): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
02-24 07:39:43.745: E/AndroidRuntime(5069): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2 023)
02-24 07:39:43.745: E/AndroidRuntime(5069): ... 11 more
新货单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.peter.databasetest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<supports-screens
android:xlargeScreens="true"
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="false" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.peter.databasetest.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>
<activity
android:name="com.peter.databasetest.Insert"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.peter.databasetest.INSERT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".ListDr"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.peter.databasetest.LISTDR" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".BackupDB"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.peter.databasetest.BACKUPDB" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".RestoreDB"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.RESTOREDB" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".InputValidation"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.INPUTVALIDATION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".CheckSDDB"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.CHECKSDDB" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
@Atul O Holic:RunBackup只是将数据库(数据/数据/ ...)复制到SD卡。 CheckSDDB检查是否存在SD卡是否可写,测试是否存在要复制的主数据库,并检查数据库是否为空(表为空)。
在CheckSDDB中满足OK条件后,将调用runAackup的startActyivity。
RunBackup只是将数据库复制到SDCard(这就是我需要Asynctask的原因),然后返回到MainActivity。
由于我还在学习Java,我不确定在哪个Activity onCreate我应该放置Asynctask
这是RunBackup的代码:
package com.peter.databasetest;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;
public class RunBackup extends AsyncTask<Void, Void, Integer> {
int bkcode;
String message;
public Context context;
protected Dialog alert;
public static String PACKAGE_NAME ;
public static String FOLDER_NAME = "DBfolder";
public static final String DATABASE_NAME = "UserDB.db";
public static final String DATABASE_BACKUP= "UserDB.db";
public static final String TABLE_NAME = "tableone";
public static final int DB_BACKUP_VERSION= 1;
//CHECKING DATABASE RELATED MESSAGES dbcode
public static final String NO_DB ="Database does not exist. Backup is not possible";
public static final String EMPTY_DB ="Database is empty.\nNo backup was done.";
public static final String UNKN_ERR ="Unknown Error.\nNo backup was done.";
//CHECKING SDCARD RELATED MESSAGES sdcode
//////////////////////////////////////////////////////////////////////////////////////////////
public static final String UNWR_SDCARD ="Your SDCard must be set to writable. Check the card lock";
public static final String NO_SDCARD ="No SDcard detected. No backup is possible";
///////////////////////////////////////////////////////////////////////////////////////////////
// BACKUP RELATED MESSAGES bkcode
//////////////////////////////////////////////////////////////////////////////////////////////
public static final String BKP_OK = "Backup was SUCCESSFUL.- CheckSDDB"; // 00
public static final String FROM_FILE_NFOUND = "Input File not found. Backup not done"; //-15
public static final String TO_FILE_NFOUND = "Output File not found. Backup not done"; //-14
public static final String TO_FROM_COPY = "Problems Copying Input File to Output. Backup not done"; //-13
public static final String FROM_INPUT = "Problems Closing the Input File. Backup not done"; //-12
public static final String TO_OUTPUT = "Problems closing the Output File. Backup not done"; //-11
public static final String BKP_NOK = "Backup FAILED. ";
public static final String TAG = "runBackup";
protected void onPreExecute() {
PACKAGE_NAME = context.getApplicationContext().getPackageName() ;
}
//////////////////////////////////////////////////////
// START BACKUP TO SDCARD
//////////////////////////////////////////////////////
@Override
protected Integer doInBackground(Void... params) {
bkcode = 0;
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
///////////////////////////////////////////////////////////////////////////////////////////
//CREATE A FOLDER /mnt/sdcard<packagename>FOLDER_NAME if it does not exist
///////////////////////////////////////////////////////////////////////////////////////////
File folder = new File(Environment.getExternalStorageDirectory()
+ "/"
+ PACKAGE_NAME
+ "/"
+ FOLDER_NAME);
if(!folder.exists()) {
if (folder.mkdirs()) ;
}
//////////////////////////////////////////////////////////////////////////
// GET THE PATH OF THE BACKUP ON THE SDCARD
//////////////////////////////////////////////////////////////////////////
File fileBackupDir = new File(Environment.getExternalStorageDirectory()
+ "/"
+PACKAGE_NAME
+ "/"
+ FOLDER_NAME
+"/"
+ DATABASE_BACKUP) ;
///////////////////////////////////////////////////////////////////////////////////////////////
// IF WE HAVE A BACKUP ON SDCARD, DELETE IT TO MAKE ROOM FOR THE NEW BACKUP
//////////////////////////////////////////////////////////////////////////////////////////////
if (fileBackupDir.exists()) {
fileBackupDir.delete();
}else {
/* DO NOTHING. THIS LOGIC KILLS ME... */
}
/////////////////////////////////////////////////////////////
// GET CURRENT DB PATH FOR THE COPY
/////////////////////////////////////////////////////////////
String currentDBPath = "/data/" + PACKAGE_NAME + "/databases/"+ DATABASE_NAME;
// GET CURRENT DB PATH FOR THE BACKUP
String backupDBPath = "/" + PACKAGE_NAME + "/" +FOLDER_NAME + "/" + DATABASE_BACKUP;
File currDB = new File(data, currentDBPath) ;
File bkpDB = new File(sd, backupDBPath);
FileChannel from = null;
try {
from = new FileInputStream(currDB).getChannel();
} catch (FileNotFoundException e) {
bkcode = -15;
message = FROM_FILE_NFOUND ;
SendMessageDialog(message);
}
FileChannel to = null;
try {
to = new FileOutputStream(bkpDB).getChannel();
} catch (FileNotFoundException e) {
bkcode = -14;
message = TO_FILE_NFOUND;
SendMessageDialog(message);
}
try {
to.transferFrom(from, 0, from.size());
} catch (IOException e) {
bkcode = -13;
message = TO_FROM_COPY ;
SendMessageDialog(message);
}
try {
from.close();
} catch (IOException e) {
bkcode = -12;
message = FROM_INPUT;
SendMessageDialog(message);
}
try {
to.close();
} catch (IOException e) {
bkcode = -11;
message = TO_OUTPUT;
SendMessageDialog(message);
}
return bkcode;
}
protected void onPostExecute(Integer retcode, String message) {
if(retcode == 0) {
message = BKP_OK;
SendMessageDialog(message);
}else {
message = BKP_NOK;
SendMessageDialog(message);
}
}
///////////////////////////////////////////////////////////////////
// DISPLAY MESSAGES
//////////////////////////////////////////////////////////////////
public void SendMessageDialog(String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
if ( bkcode == 0) {
builder.setIcon(R.drawable.ecg); /* SET OK ICON */
}else {
builder.setIcon(R.drawable.bad);
}
builder.setTitle("My MESSAGE")
.setMessage(message) // Title of the dialog
.setCancelable(true) // allow the use of Back Button on the hardware
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
returnMain();
}
);
alert = builder.create();
alert.show();
}
///////////////////////////////////////////////////////////////////
// BACK TO THE CALLING ACTIVITY
///////////////////////////////////////////////////////////////////
public void returnMain() {
if (alert != null && alert.isShowing() ) {
alert.dismiss();
}
Context ctx = context.getApplicationContext();
Intent intent = new Intent(ctx, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
ctx.startActivity(intent);
}
}
@NPE:感谢您的链接。我已经多次完成了解释,但我仍然无法看到为什么根据链接上给出的解释,RunBackup无法按照上面的日志运行。
答案 0 :(得分:1)
这不是整个解决方案(不确定RunBackup
的问题是什么),但这可能值得注意。
} else if (v == backupDbButton){
startActivity (new Intent(this, CheckSDDB.class));
我假设在onClick
或v
为View
的其他方法中调用此方法。问题在于this
是指View
(据我所知)。
尝试更改此内容:
startActivity (new Intent(this, CheckSDDB.class));
对此:
startActivity (new Intent(MainActivity.this, CheckSDDB.class));
答案 1 :(得分:0)
intent是要执行的操作的抽象描述。它可以与startActivity一起用于启动Activity,broadcastIntent用于将其发送到任何感兴趣的BroadcastReceiver组件,startService(Intent)或bindService(Intent,ServiceConnection,int)用于与后台服务进行通信。
如果您想使用RunBackup
启动Intent
,则需要扩展Activity
类或其任何子类。
更多信息here。
答案 2 :(得分:0)
问题在于以下行,
Intent intent = new Intent(CheckSDDB.this,RunBackup.class);
RunBackup是Asynctask的子类,您的意图是期望一个Activity。因此例外。因此,正如@NPE所说,创建一个你通过执行调用的新Activity,
Intent intent = new Intent(CheckSDDB.this,NewActivty.class);
startActivity(intent);
并在其onCreate()方法中放置Asynctask调用。因此,只要创建了Activity,就会触发RunBackUp。
如果您还可以解释您对Asynctask的看法,那么我们将很容易为您提供帮助。