I am trying to build a multiscreen app in android studio but unfortunately, it crashes during its run . The app work perfectly fine on the main screen and it doesn't crashes on MainActivity
but when it comes to the MainActivity2
it crashes.
This is my Java code of MainActivity:
package com.example.scrollapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText editText;
public static final String EXTRA_NAME ="com.example.scrollapp.extra.NAME";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=findViewById(R.id.editTextTextPersonName3);
String nametext = editText.getText().toString();
}
public void openActivity(View v){
Toast.makeText(MainActivity.this, "opening second activity", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this,MainActivity2.class);
editText=findViewById(R.id.editTextTextPersonName3);
String nametext = editText.getText().toString();
intent.putExtra(EXTRA_NAME,nametext);
startActivity(intent);
}
}
This is the Java Code of MainActivity2 :
package com.example.scrollapp;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity2 extends AppCompatActivity {
TextView textView;
@SuppressLint("SetTextI18n")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
textView.findViewById(R.id.textView);
Intent intent = getIntent();
String name = intent.getStringExtra(MainActivity.EXTRA_NAME);
textView.setText("your name is " name);
}
}
Error as shown in logcat:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.scrollapp, PID: 7978
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.scrollapp/com.example.scrollapp.MainActivity2}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.TextView.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.TextView.findViewById(int)' on a null object reference
at com.example.scrollapp.MainActivity2.onCreate(MainActivity2.java:18)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
I/Process: Sending signal. PID: 7978 SIG: 9
CodePudding user response:
The TextView textview in MainActivity2 class is not initialized. Use textView = findViewById(R.id.textView);
instead of textView.findViewById(R.id.textView);
in MainActivity2 class.
CodePudding user response:
The initialization of TextView
is incorrect. The correct way of initialising is :
textView = findViewById(R.id.textView);
You have to instantiate the variable after the setcontentView
method was called, so you have to do the following in MainActivity2
:
public class MainActivity2 extends AppCompatActivity {
TextView textView;
@SuppressLint("SetTextI18n")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
textView = findViewById(R.id.textView);// Change made here in this line
Intent intent = getIntent();
String name = intent.getStringExtra(MainActivity.EXTRA_NAME);
textView.setText("your name is " name)
}
}