java.lang.RuntimeException: Unable to Instantiate activity ComponentInfo

淺唱寂寞╮ 提交于 2019-12-08 02:43:21

问题


I am creating a widget but when I run it I get this error in the console:

[2013-10-05 22:02:56 - AwesomeFileBuilderWidget] ------------------------------
[2013-10-05 22:02:56 - AwesomeFileBuilderWidget] Android Launch!
[2013-10-05 22:02:56 - AwesomeFileBuilderWidget] adb is running normally.
[2013-10-05 22:02:56 - AwesomeFileBuilderWidget] No Launcher activity found!
[2013-10-05 22:02:56 - AwesomeFileBuilderWidget] The launch will only sync the application package on the device!
[2013-10-05 22:02:56 - AwesomeFileBuilderWidget] Performing sync
[2013-10-05 22:02:56 - AwesomeFileBuilderWidget] Automatic Target Mode: Unable to detect device compatibility. Please select a target device.
[2013-10-05 22:03:00 - AwesomeFileBuilderWidget] Uploading AwesomeFileBuilderWidget.apk onto device 'HT18YMA05067'
[2013-10-05 22:03:00 - AwesomeFileBuilderWidget] Installing AwesomeFileBuilderWidget.apk...
[2013-10-05 22:03:04 - AwesomeFileBuilderWidget] Success!
[2013-10-05 22:03:04 - AwesomeFileBuilderWidget] \AwesomeFileBuilderWidget\bin\AwesomeFileBuilderWidget.apk installed on device
[2013-10-05 22:03:04 - AwesomeFileBuilderWidget] Done!

But the widget installs fine on my device and shows up in the Widget selection screen.

The second error occurs when I try to put my widget on the homescreen. I get this error in LogCat:

10-05 21:51:45.485: D/AndroidRuntime(3557): Shutting down VM
10-05 21:51:45.485: W/dalvikvm(3557): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)
10-05 21:51:45.505: E/AndroidRuntime(3557): FATAL EXCEPTION: main
10-05 21:51:45.505: E/AndroidRuntime(3557): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.awesomefilebuilderwidget/com.example.awesomefilebuilderwidget.WidgetConfig}: java.lang.ClassNotFoundException: com.example.awesomefilebuilderwidget.WidgetConfig in loader dalvik.system.PathClassLoader[/data/app/com.example.awesomefilebuilderwidget-1.apk]
10-05 21:51:45.505: E/AndroidRuntime(3557):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1746)
10-05 21:51:45.505: E/AndroidRuntime(3557):     at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:1854)
10-05 21:51:45.505: E/AndroidRuntime(3557):     at android.app.ActivityThread.access$1500(ActivityThread.java:135)
10-05 21:51:45.505: E/AndroidRuntime(3557):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1041)
10-05 21:51:45.505: E/AndroidRuntime(3557):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-05 21:51:45.505: E/AndroidRuntime(3557):     at android.os.Looper.loop(Looper.java:150)
10-05 21:51:45.505: E/AndroidRuntime(3557):     at android.app.ActivityThread.main(ActivityThread.java:4333)
10-05 21:51:45.505: E/AndroidRuntime(3557):     at java.lang.reflect.Method.invokeNative(Native Method)
10-05 21:51:45.505: E/AndroidRuntime(3557):     at java.lang.reflect.Method.invoke(Method.java:507)
10-05 21:51:45.505: E/AndroidRuntime(3557):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-05 21:51:45.505: E/AndroidRuntime(3557):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-05 21:51:45.505: E/AndroidRuntime(3557):     at dalvik.system.NativeStart.main(Native Method)
10-05 21:51:45.505: E/AndroidRuntime(3557): Caused by: java.lang.ClassNotFoundException: com.example.awesomefilebuilderwidget.WidgetConfig in loader dalvik.system.PathClassLoader[/data/app/com.example.awesomefilebuilderwidget-1.apk]
10-05 21:51:45.505: E/AndroidRuntime(3557):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
10-05 21:51:45.505: E/AndroidRuntime(3557):     at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
10-05 21:51:45.505: E/AndroidRuntime(3557):     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
10-05 21:51:45.505: E/AndroidRuntime(3557):     at android.app.Instrumentation.newActivity(Instrumentation.java:1040)
10-05 21:51:45.505: E/AndroidRuntime(3557):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1738)
10-05 21:51:45.505: E/AndroidRuntime(3557):     ... 11 more

Here is my Manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.awesomefilebuilderwidget"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

<receiver android:name=".AFBWidget" android:label="@string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>

<meta-data android:name="android.appwidget.provider"
android:resource="@xml/widget_stuff"/>

</receiver>

<activity android:name=".WidgetConfig" android:label="@string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
<category android:name="android.intent.category.LAUNCHER" />     
</intent-filter> 

</activity>   

<activity android:name=".AFBWidget" android:label="@string/app_name"/>


</application>

</manifest>

I have made sure I have checked all the boxes in the Order and Export section. I have also made sure that my AFBWidget and WidgetConfig classes are in src. (Please note that the actual widget configuration is in the correct xml location, the WidgetConfig.java is something else)

What is the issue?

Updated LogCat:

10-05 22:47:40.628: D/AndroidRuntime(4113): Shutting down VM
10-05 22:47:40.628: W/dalvikvm(4113): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)
10-05 22:47:40.638: E/AndroidRuntime(4113): FATAL EXCEPTION: main
10-05 22:47:40.638: E/AndroidRuntime(4113): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.awesomefilebuilderwidget/com.example.awesomefilebuilderwidget.WidgetConfig}: java.lang.ClassNotFoundException: com.example.awesomefilebuilderwidget.WidgetConfig in loader dalvik.system.PathClassLoader[/data/app/com.example.awesomefilebuilderwidget-1.apk]
10-05 22:47:40.638: E/AndroidRuntime(4113):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1746)
10-05 22:47:40.638: E/AndroidRuntime(4113):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1854)
10-05 22:47:40.638: E/AndroidRuntime(4113):     at android.app.ActivityThread.access$1500(ActivityThread.java:135)
10-05 22:47:40.638: E/AndroidRuntime(4113):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1041)
10-05 22:47:40.638: E/AndroidRuntime(4113):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-05 22:47:40.638: E/AndroidRuntime(4113):     at android.os.Looper.loop(Looper.java:150)
10-05 22:47:40.638: E/AndroidRuntime(4113):     at android.app.ActivityThread.main(ActivityThread.java:4333)
10-05 22:47:40.638: E/AndroidRuntime(4113):     at java.lang.reflect.Method.invokeNative(Native Method)
10-05 22:47:40.638: E/AndroidRuntime(4113):     at java.lang.reflect.Method.invoke(Method.java:507)
10-05 22:47:40.638: E/AndroidRuntime(4113):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-05 22:47:40.638: E/AndroidRuntime(4113):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-05 22:47:40.638: E/AndroidRuntime(4113):     at dalvik.system.NativeStart.main(Native Method)
10-05 22:47:40.638: E/AndroidRuntime(4113): Caused by: java.lang.ClassNotFoundException: com.example.awesomefilebuilderwidget.WidgetConfig in loader dalvik.system.PathClassLoader[/data/app/com.example.awesomefilebuilderwidget-1.apk]
10-05 22:47:40.638: E/AndroidRuntime(4113):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
10-05 22:47:40.638: E/AndroidRuntime(4113):     at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
10-05 22:47:40.638: E/AndroidRuntime(4113):     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
10-05 22:47:40.638: E/AndroidRuntime(4113):     at android.app.Instrumentation.newActivity(Instrumentation.java:1040)
10-05 22:47:40.638: E/AndroidRuntime(4113):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1738)
10-05 22:47:40.638: E/AndroidRuntime(4113):     ... 11 more

ADDED CLASSES AND XML FILES: AFBWidget.java:

import java.util.Random;

import com.example.awesomefilebuilderwidget.R;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.widget.RemoteViews;
import android.widget.Toast;


public class AFBWidget extends AppWidgetProvider{

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    // TODO Auto-generated method stub
    super.onUpdate(context, appWidgetManager, appWidgetIds);
    Random r = new Random();
    int randomInt = r.nextInt(1000000000);
    String rand = String.valueOf(randomInt);

    final int N = appWidgetIds.length;

    for (int i = 0; i < N; i++){
        int awID = appWidgetIds[i];
        RemoteViews v = new RemoteViews(context.getPackageName(), R.layout.widget);
        v.setTextViewText(R.id.tvwidgetUpdate, rand);
        appWidgetManager.updateAppWidget(awID, v);
    }

}

WidgetConfig.java:

import android.app.Activity;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RemoteViews;

import com.example.awesomefilebuilderwidget.R;


public class WidgetConfig extends Activity implements OnClickListener{

EditText info;
AppWidgetManager awm;
Context c;
int awID;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.widgetconfig);
    Button b = (Button)findViewById(R.id.bwidgetconfig);
    b.setOnClickListener(this);
    c = WidgetConfig.this;
    info = (EditText)findViewById(R.id.etwidgetconfig);
    //Getting info about the widget that launched this Activity
    Intent i = getIntent();
    Bundle extras = i.getExtras();
    if (extras != null){
        awID = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID
                );
    }else{
        finish();
    }
    awm = AppWidgetManager.getInstance(c);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    String e = info.getText().toString();

    RemoteViews views = new RemoteViews(c.getPackageName(), R.layout.widget);
    views.setTextViewText(R.id.tvConfigInput, e);

    Intent in = new Intent(c, WidgetConfig.class);
    PendingIntent pi = PendingIntent.getActivity(c, 0, in, 0);
    views.setOnClickPendingIntent(R.id.bwidgetOpen, pi);

    awm.updateAppWidget(awID, views);

    Intent result = new Intent();
    result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, awID);
    setResult(RESULT_OK, result);

    finish();
}


}

Widget.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/tvConfigInput"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<Button
    android:id="@+id/bwidgetOpen"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

<TextView
    android:id="@+id/tvwidgetUpdate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

</LinearLayout>

WidgetConfig.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<EditText
    android:id="@+id/etwidgetconfig"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" 
    android:hint="@string/user_input_application_name">

    <requestFocus />
</EditText>

<Button
    android:id="@+id/bwidgetconfig"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/ok" />

</LinearLayout>

App Widget Provider (app_stuff.xml):


回答1:


I FINALLY FIXED IT AFTER 3 DAYS! Ok, so it was indeed because my two java classes were in a default package. So to fix that, since my classes were being accessed via a different package, I created a new package that was my app (com.example.awesomefilebuilderwidget) and then moved the classes into there and now it works with no problemos! :DD Thanks for all you've done I really do apperciate it! – user1628978




回答2:


try to clean your workspace. Goto Project->Clean and select your project. For some reason eclipse doesn't build manifest automatically.

and also try to clean you server by doing right click and then clean




回答3:


add all activities in src to AndroidManifest.xml.clean and run project then



来源:https://stackoverflow.com/questions/19205341/java-lang-runtimeexception-unable-to-instantiate-activity-componentinfo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!