Reputation: 63
Ok, I've been searching but I can't find anything similar to this thing that is happening with my app. I've got an app that uses an IntentService to import and export files, everything seemingly work, but when I rotate the screen after launching the app through an intent from selecting a compatible file from other app, my app crashes. It doesn't crash if it was already running before selecting the file, and I'm completely out of ideas of what could be going on and how to solve it. Since the error is so bizarre I don't know what info can be relevant, but here is the error:
java.lang.RuntimeException:
Unable to instantiate activity ComponentInfo{com.txintxe.dev.xxx/com.txintxe.dev.xxx.services.ExportImportService}: java.lang.ClassCastException: com.txintxe.dev.xxx.services.ExportImportService cannot be cast to android.app.Activity
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2586)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2751)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4556)
at android.app.ActivityThread.-wrap19(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1502)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6186)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
Caused by: java.lang.ClassCastException: com.txintxe.dev.saltea.services.ExportImportService cannot be cast to android.app.Activity
at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2576)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2751)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4556)
at android.app.ActivityThread.-wrap19(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1502)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6186)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
The (I think) relevant part of the manifest:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".ListActivity"
android:label="@string/app_name"
android:launchMode="singleTop"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<intent-filter>
<data
android:scheme="content"
android:mimeType="*/*"
android:host="*"
/>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.EDIT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<data
android:scheme="file"
android:mimeType="*/*"
android:pathPattern=".*\\.xxx"
android:host="*"/>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.EDIT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
The method inside the activity that I use to call the IntentService:
private void doImportRecipe() {
Intent intent = getIntent();
intent.setAction(ExportImportService.ACTION_IMPORT);
intent.setClass(getApplicationContext(), ExportImportService.class);
startService(intent);
}
And finally the method that handles the intents in the IntentService:
protected void onHandleIntent(Intent intent) {
if (intent != null) {
final String action = intent.getAction();
final ArrayList<String> xxxKeys = intent.getStringArrayListExtra(EXTRA_XXXKEYS);
switch(action) {
case ACTION_IMPORT:
final Uri xxxPath = intent.getData();
handleActionImport(xxxPath);
break;
case ACTION_EXPORT:
handleActionExport(xxxKeys);
break;
case ACTION_SHARE:
handleActionShare(xxxKeys);
break;
}
}
}
If there's something more needed to solve it I have no problem in posting it, but as I said everything seemingly works, unless the app is started from the import action and then rotated.
Upvotes: 1
Views: 85
Reputation: 2551
in your manifest define your intent service class
<service
android:name=".MyIntentService"
android:exported="false" />
And in your main class to start the service
Intent intent = new Intent(context, MyIntentService.class);
intent.setAction("your_action");
startService(intent);
Upvotes: 1
Reputation: 63
Ok. It seems that it works now, and the problem was in reusing the same intent instead of creating a new one here:
private void doImportRecipe(String data) {
Intent intent = new Intent();
intent.setAction(ExportImportService.ACTION_IMPORT);
intent.setClass(getApplicationContext(), ExportImportService.class);
intent.setData(Uri.parse(data));
startService(intent);
}
I'll keep testing it to make sure that it really works now, thank you everyone that took the time to try to answer :)
Upvotes: 0