1

I'm new in Android and just trying to figure out how to work with method onActivityResult. I want to click button on mainActivity, go to another actvivity, enter name and return to mainActivity. But I have troubles with running app. I always have get this error:

java.lang.NullPointerException: Attempt to invoke virtual method...

09-20 22:14:32.212 30617-30617/com.genaepic.p029_simpleactivityresult E/AndroidRuntime:

FATAL EXCEPTION: main Process: com.genaepic.p029_simpleactivityresult, PID: 30617 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.genaepic.p029_simpleactivityresult/com.genaepic.p029_simpleactivityresult.NameActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at com.genaepic.p029_simpleactivityresult.NameActivity.onCreate(NameActivity.java:22) at android.app.Activity.performCreate(Activity.java:6679) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)  at android.app.ActivityThread.-wrap12(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6119)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

My main activity:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    TextView textView;
    Button button;


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

        textView = (TextView) findViewById(R.id.tv_main);
        button = (Button) findViewById(R.id.btn_iputName);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this, NameActivity.class);
        startActivityForResult(intent, 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(data == null) {
            return;
        }
        String name = data.getStringExtra("name");
        textView.setText("Your name is " + name);
    }
}

My second activity:

public class NameActivity extends AppCompatActivity implements View.OnClickListener {

EditText editText;
Button button;

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

    editText = (EditText) findViewById(R.id.et_name);
    button = (Button) findViewById(R.id.btn_iputName);
    button.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    Intent intent = new Intent();
    intent.putExtra("name", editText.getText().toString());
    setResult(RESULT_OK, intent);
    finish();
}

}

My xml code for mainActivity:

<Button
    android:id="@+id/btn_iputName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="input your name"
    tools:layout_constraintTop_creator="1"
    android:layout_marginStart="34dp"
    android:layout_marginTop="92dp"
    tools:layout_constraintLeft_creator="1"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginLeft="34dp" />

<TextView
    android:id="@+id/tv_main"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Your name is"
    tools:layout_constraintTop_creator="1"
    android:layout_marginStart="34dp"
    android:layout_marginTop="33dp"
    tools:layout_constraintLeft_creator="1"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginLeft="34dp" />

My xml code for NameActivity

 <Button
        android:id="@+id/btn_ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="116dp"
        android:text="ok"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintTop_creator="1" />

    <EditText
        android:id="@+id/et_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="64dp"
        android:layout_marginRight="84dp"
        android:layout_marginTop="31dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintTop_creator="1" />
</android.support.constraint.ConstraintLayout>
0

4 Answers 4

4

You doing setContentView(R.layout.activity_name) in NameActivity and then button = (Button) findViewById(R.id.btn_iputName) which is present in another activity.

Just change

button = (Button) findViewById(R.id.btn_iputName);

to

button = (Button) findViewById(R.id.btn_ok);

it will work for you.

1
  • Thanks for feedback. It was helpful Sep 21, 2017 at 9:42
3

You have

setContentView(R.layout.activity_name)

So change this

button = (Button) findViewById(R.id.btn_iputName)

to

button = (Button) findViewById(R.id.btn_ok);

findViewById looks for views with id in the current view hierarchy. So if you have setContentView(R.layout.activity_name) you should look up id in your activity_name.xml.

0
2

In Your NameActivity there is not button of button = (Button) findViewById(R.id.btn_iputName); instead it is there in MainActivity and you are finding in the NameActivity because of which while starting Name activity it is giving NullPointerException.

0
1

Wrong ID used for Button

In your second activity i.e., NameActivity.java You need to Replace

button = (Button) findViewById(R.id.btn_iputName);

with

button = (Button) findViewById(R.id.btn_ok);

These changes will resolve the NullPointerException and your app will work properly.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.