java.lang.RuntimeException: Unable to instantiate activity ComponentInfo

前端 未结 30 2232
感动是毒
感动是毒 2020-11-22 15:15

I was trying to run a sample code While launching the application in the android 1.5 emulator , I got these errors.... Any one have some hint..?

ERROR from LogCat:<

相关标签:
30条回答
  • 2020-11-22 15:59

    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.

    0 讨论(0)
  • 2020-11-22 15:59

    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

    0 讨论(0)
  • 2020-11-22 16:00

    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.

    0 讨论(0)
  • 2020-11-22 16:01

    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);
    ....
    }
    
    0 讨论(0)
  • 2020-11-22 16:02

    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.

    0 讨论(0)
  • 2020-11-22 16:03

    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"
    
    0 讨论(0)
提交回复
热议问题