java.lang.RuntimeException : 활동 ComponentInfo를 인스턴스화 할 수 없습니다
샘플 코드를 실행하려고했습니다 .Android 1.5 에뮬레이터에서 응용 프로그램을 시작하는 동안 이러한 오류가 발생했습니다 .... 힌트가 하나 있습니다 ..?
LogCat의 오류 :
01-13 02:28:08.392: ERROR/AndroidRuntime(2888): FATAL EXCEPTION: main
01-13 02:28:08.392: ERROR/AndroidRuntime(2888): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.s.android.test/com.s.android.test.MainActivity}: java.lang.ClassNotFoundException: com.s.android.test.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.s.android.test-2.apk]
01-13 02:28:08.392: ERROR/AndroidRuntime(2888): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1544)
01-13 02:28:08.392: ERROR/AndroidRuntime(2888): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
01-13 02:28:08.392: ERROR/AndroidRuntime(2888): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-13 02:28:08.392: ERROR/AndroidRuntime(2888): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
01-13 02:28:08.392: ERROR/AndroidRuntime(2888): at android.os.Handler.dispatchMessage(Handler.java:99)
01-13 02:28:08.392: ERROR/AndroidRuntime(2888): at android.os.Looper.loop(Looper.java:123)
01-13 02:28:08.392: ERROR/AndroidRuntime(2888): at android.app.ActivityThread.main(ActivityThread.java:3647)
01-13 02:28:08.392: ERROR/AndroidRuntime(2888): at java.lang.reflect.Method.invokeNative(Native Method)
01-13 02:28:08.392: ERROR/AndroidRuntime(2888): at java.lang.reflect.Method.invoke(Method.java:507)
01-13 02:28:08.392: ERROR/AndroidRuntime(2888): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-13 02:28:08.392: ERROR/AndroidRuntime(2888): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-13 02:28:08.392: ERROR/AndroidRuntime(2888): at dalvik.system.NativeStart.main(Native Method)
01-13 02:28:08.392: ERROR/AndroidRuntime(2888): Caused by: java.lang.ClassNotFoundException: com.s.android.test.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.s.android.test-2.apk]
01-13 02:28:08.392: ERROR/AndroidRuntime(2888): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
01-13 02:28:08.392: ERROR/AndroidRuntime(2888): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
01-13 02:28:08.392: ERROR/AndroidRuntime(2888): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
01-13 02:28:08.392: ERROR/AndroidRuntime(2888): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
01-13 02:28:08.392: ERROR/AndroidRuntime(2888): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1536)
01-13 02:28:08.392: ERROR/AndroidRuntime(2888): ... 11 more
01-13 02:28:08.407: WARN/ActivityManager(112): Force finishing activity com.s.android.test/.MainActivity
편집 이 오류는 대부분의 초보자에게 발생합니다. 모든 활동을 매니페스트 파일에 추가해야한다는 것입니다.
그것은 당신의 의도의 문제입니다.
당신을 추가하십시오 Activity
당신에 AndroidManifest.xml
.
새로운 활동을하고 싶을 때에는에 등록해야합니다 AndroidManifest.xml
.
내 경우에는 Google지도 라이브러리를 추가하는 것을 잊었습니다.
<application>
....
<uses-library android:name="com.google.android.maps" />
</application>
또한 활동 경로 전에 이전 점이 누락되지 않았는지 확인하십시오.
<activity android:name=".activities.MainActivity"/>
보기 onCreate()
가 잘못된 보기를 찾으려고 시도했을 수 있습니다 .
public class MainActivity extends Activity {
ImageView mainImage = (ImageView) findViewById(R.id.imageViewMain); //incorrect
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
...
}
java.lang.RuntimeException 을 얻는 다른 방법이 있습니다 . 활동 ComponentInfo 예외 를 인스턴스화 할 수 없으며 시작하려는 활동은 abstract 입니다. 나는이 어리석은 실수를 한 번했고 간과하기가 매우 쉽다.
이 문제로 인해 발생합니다. apk로 내 보내야 할 항아리를 확인하지 않았으며 이와 동일한 일이 발생했습니다. 앱을 실행하는 데 필요한 항아리를 선택하십시오.
이 문제도 발생했지만 약간 다른 방식으로 발생합니다. 내 시나리오는 다음과 같습니다.
앱 세부 정보 :
- 라이브러리로 ActionBarSherlock 사용
- ActionBarSherlock 라이브러리에서 android-support-v4-r7-googlemaps.jar를 사용하여 기본 프로젝트에서 "맵 조각"을 사용할 수 있습니다.
- 기본 프로젝트의 빌드 경로에 항아리를 추가했습니다.
- Included the
<uses-library android:name="com.google.android.maps" />
in the manifests of both the library project and my primary project (this may not be necessary in the library?) - The manifest in the primary project had the proper activity definition and all of the appropriate properties
- I didn't have an abstract activity or any of the other gotchas I've seen on Stack Overflow pertaining to this problem.
However, I still encountered the error described in the original post and could not get it to go away. The problem though, was slightly different in one regard:
- It only affected a fresh install of the application to my device. Any time the app installed for the first time, I would get the error preceded by several "warnings" of: Unable to resolve superclass of FragmentActivity
- Those warnings traced back to the ActionBarSherlock library
- The app would force close and crash.
- If I immediately rebuilt and redeployed the app, it worked fine.
- The only time it crashed was on a totally fresh install. If I uninstalled the app, built and deployed again from Eclipse, it would crash. Build/deploy a second time, it would work fine.
How I fixed it:
- In the ActionBarSherlock library project, I added the android-support-v4-r7-googlemaps.jar to the build path
This step alone did not fix the problem
Once the jar was added to the build path, I had change the order on the Java Build Path > Order and Export tab - I set the JAR to the first item in the list (it was the last after the /src and /gen items).
I then rebuilt and redeployed the app to my device - it worked as expected on a fresh install. Just to be certain, I uninstalled it again 2-3 times and reinstalled - still worked as expected.
This may be a total rookie mistake, but I spent quite a while digging through posts and my code to figure it out, so hopefully it can be helpful to someone else. May not fix all situations, but in this particular case, that ended up being the solution to my problem.
This might not be relevant to the actual question, but in my instance, I tried to implement Kotlin and left out apply plugin: 'kotlin-android'
. The error happened because it could not recognize the MainActivity in as a .kt file.
Hope it helps someone.
This error can also be the ultimate sign of a dumb mistake (like when I - I mean, cough, like when a friend of mine who showed me their code once) where they try to execute code outside of a method like trying to do this:
SQLiteDatabase db = openOrCreateDatabase("DB", MODE_PRIVATE, null); //trying to perform function where you can only set up objects, primitives, etc
@Override
public void onCreate(Bundle savedInstanceState) {
....
}
rather than this:
SQLiteDatabase db;
@Override
public void onCreate(Bundle savedInstanceState) {
db = openOrCreateDatabase("DB", MODE_PRIVATE, null);
....
}
I got rid of this problem by deleting the Bin
and Gen
folder from project(which automatically come back when the project will build) and then cleaning the project from ->Menu -> Project -> clean.
Thanks.
In my case I haven't set the setContentView(R.layout.main);
If you create a new class do not foget to set this in on onCreate(Bundle savedInstanceState)
method.
I have done this stupid mistake several times.
Simply Clean your working project or restart eclipse. Then run your project. it will work.
For me, my package string in AndroidManifest.xml
was incorrect (copied from a tutorial). Make sure the package string in this file is the same as where your main activity is, e.g.
package="com.example.app"
An easy way to do this is to open the AndroidManifest.xml file in the "Manifest" tab, and type it in the text box next to Package, or use the Browse button.
Also, the package string for my activity was wrong, e.g.
<activity android:name="com.example.app.MainActivity" android:label="@string/app_name">
I (stupidly) got the same error weeks later when I renamed my package name. If you do this, make sure you update the AndroidManifest.xml
file too.
Ok, I am fairly experienced on the iPhone but new to android. I got this issue when I had:
Button infoButton = (Button)findViewById(R.id.InfoButton);
this line of code above:
@Override
public void onCreate (Bundle savedInstanceState) {
}
May be I am sleep deprived :P
I recently encountered this with fresh re-install of Eclipse. Turns out my Compiler compliance level was set to Java 1.7 and project required 1.6.
In Eclipse: Project -> Properties -> Java Compiler -> JDK Compliance
Got this problem, and fixed it by setting the "launch mode" property of the activity.
Another reason of this problem may be a missing library.
Go to Properties -> Android and check that you add the libraries correctly
I had the same problem, but I had my activity declared in the Manifest file, with the correct name.
My problem was that I didn't have to imported a third party libraries in a "libs" folder, and I needed reference them in my proyect (Right-click, properties, Java Build Path, Libraries, Add Jar...).
This can happen if your activity class is inside a default package. I fixed it by moving the activity class to a new package. and changing the manifest.xml
before
activity android:name=".MainActivity"
after
activity android:name="new_package.MainActivity"
This happens to me fairly frequently when using the NDK. I found that it is necessary for me to do a "Clean" in Eclipse after every time I do a ndk-build
. Hope it helps anyone :)
This error also occurs when you use of ActionBarActivity
but assigned a non AppCompat
style.
To fix this just set your apps style parent to an Theme.AppCompat
one like this.:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
</style>
Right click on project > properties > android > and try with different version of the android earlier i was doing with android 4.4 then i changed to android 4.3 and it worked !
I had the same issue (Unable to instantiate Activity) :
FIRST reason :
I was accessing
Camera mCamera;
Camera.Parameters params = mCamera.getParameters();
before
mCamera = Camera.open();
So right way of doing is, open the camera first and then access parameters.
SECOND reason : Declare your activity in the manifest file
<activity android:name=".activities.MainActivity"/>
THIRD reason : Declare Camera permission in your manifest file.
<uses-feature android:name="android.hardware.Camera"></uses-feature>
<uses-permission android:name="android.permission.CAMERA" />
Hope this helps
In my case, I was trying to embed the Facebook SDK and I was having the wrong Application ID; thus the error was popping up. In your manifest file, you should have the proper meta data:
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id" />
If you have Android Studio 2.3.3 and Android Studio 3.0.0 installed, then switching between the two programs for development will cause this error. This is because there exist situations where classes supported in one program is not supported by the other and vice versa. It is important to maintain consistency in which version of Android Studio is being used to develop a project.
As suggested by djjeck
in comment in this answer I missed to put public
modifier for my class.
It should be
public class MyActivity extends AppCompatActivity {
It may help some like me.
I have tried all above solution but nothing work for me. after I have just add extend
activity instead of AppCompatActivity
and working fine.
used
public class MainActivity extends Activity
instead of
public class MainActivity extends AppCompatActivity
i dont know what real issue was that.
In my case, I was trying to initialize the components(UI) even before the onCreate
is called for the Activity.
Make sure your UI components are initialized/linked in the onCreate
method after setContentView
NB: This is my first mistake while learning Android programming.
This happened to me when I tried to run an Activity
on 2.2 that used imports from Honeycomb not available in older versions of Android and not included in the v4 support package either.
Your New Activity add AndroidManifest.xml
like ".NewActivity"
</activity>
<activity android:name=".NewActivity" />
You should make sure that all the activities in your application are properly defined in the AndroidManifest.xml file.
Also, if they start with a path make sure this path exists.
E.g. this will not work:
<activity android:name=".com.example.android.networkusage.NetworkActivity" android:label="@string/app_name" >
If your application's name is com.example.networkusage
(note the missing .android.)
또는 매니페스트 내에서 활동을 정의 할 때 경로를 포함하지 마십시오. 이름 앞에만 점을 넣으십시오.
<activity android:name=".NetworkActivity" android:label="@string/app_name" >
'Programing' 카테고리의 다른 글
const 포인터의 요점은 무엇입니까? (0) | 2020.06.15 |
---|---|
iOS 앱 아이콘 설정 및 이미지 실행 방법 (0) | 2020.06.15 |
AJAX 성공의 JSON 결과에 대한 jQuery 루프? (0) | 2020.06.15 |
BAT 파일이 실행 된 후 CMD를 열린 상태로 유지 (0) | 2020.06.15 |
Node.js와 함께 밑줄 모듈 사용 (0) | 2020.06.15 |