代码如下
package com.example.gps3;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
Button button1,buttonn;
TextView textView99;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView99.findViewById(R.id.textview99);
button1=findViewById(R.id.button1);
buttonn=findViewById(R.id.buttonn);
buttonn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView99.setText("132456");
/**
Intent intent =getIntent();
String stringvalue = intent.getStringExtra("tings");
textView1.setText(stringvalue);
**/
}
});
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent2=new Intent(MainActivity.this,MainActivity2.class);
startActivity(intent2);
}
});
}
}
下面是错误报告
12-18 13:24:30.864 18118-18118/com.example.gps3 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.gps3, PID: 18118
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gps3/com.example.gps3.MainActivity}: 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:2431)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
at android.app.ActivityThread.access$800(ActivityThread.java:164)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1362)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:147)
at android.app.ActivityThread.main(ActivityThread.java:5531)
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:969)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764)
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.gps3.MainActivity.onCreate(MainActivity.java:18)
at android.app.Activity.performCreate(Activity.java:6033)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2384)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
at android.app.ActivityThread.access$800(ActivityThread.java:164)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1362)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:147)
at android.app.ActivityThread.main(ActivityThread.java:5531)
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:969)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764)
12-18 13:24:30.884 18118-18118/com.example.gps3 I/Process: Sending signal. PID: 18118 SIG: 9