简体   繁体   中英

Unable to instantiate activity ComponentInfo - AndroidStudio

I am a complete beginner to android/Java development and I massively appreciate any pointers anyone is able to give me on the issue I'm having. Here is the MainActivity.java.

package com.example.harris.enterappman;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}






EditText enterName;
TextView usersName;
String str = enterName.getText().toString();
public void getName(View view){

    enterName = (EditText) findViewById(R.id.enterName);
    usersName = (TextView) findViewById(R.id.helloNameView);
    usersName.setText(str);

}

public void printUsersName(){



}

}

Everytime I try and run the program, it fails with the error:

java.lang.RuntimeException: Unable to instantiate activity             ComponentInfo{com.example.harris.enterappman/com.example.harris.enterappman.Main    Activity}: java.lang.NullPointerException
        at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5021)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.example.harris.enterappman.MainActivity.<init>(MainActivity.java:48)
        at java.lang.Class.newInstanceImpl(Native Method)
        at java.lang.Class.newInstance(Class.java:1208)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1064)
        at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
            at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5021)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at       com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
            at dalvik.system.NativeStart.main(Native Method)

From others posts online I have come to the conclusion that the problem is related to abstract methods? I was unable to work out how my code was wrong.

Cheers, Harris

String str = enterName.getText().toString();

enterName is null at this point, as you are not setting it ever. You are welcome to declare String str; as a field, but you cannot initialize it until after you set a value on enterName .

You have code to initialize enterName in a somewhat strange getName() method, but you do not appear to be calling that ever.

how possible you are setting your str using enterName , as enterName is not initialized and hence it is null causing Null pointer exception

you must have to initilize enterName before using it, as you are doing initialization in your getName() method, and then use it to get its text

your code must be like this and you have to call this method from onCreate()

public void getName(View view){

    enterName = (EditText) findViewById(R.id.enterName);
    usersName = (TextView) findViewById(R.id.helloNameView);
    usersName.setText(enterName.getText().toString());

}

you have not initialised entername properly

either use:

....
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    enterName = (EditText) findViewById(R.id.enterName);
    usersName = (TextView) findViewById(R.id.helloNameView);
    usersName.setText(str);
}  
...

or use it like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getName();
}

public void getName(){
    enterName = (EditText) findViewById(R.id.enterName);
    usersName = (TextView) findViewById(R.id.helloNameView);
    usersName.setText(str);
}

You call enterName.getText() when enterName is null, which leads to NullPointerException.

Few things I suggest:

  • If you want to own references to the layout components, call these in the onCreate method:
 enterName = (EditText) findViewById(R.id.enterName); usersName = (TextView) findViewById(R.id.helloNameView); 
  • Don't call methods in the class itself (except some constructors), call them in a method (I refer to the str variable).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM