Dr.Rick 2020-12-18 13:33 采纳率: 75%
浏览 195
已采纳

为什么我这里明明声明了textview还报错?

代码如下

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
  • 写回答

3条回答 默认 最新

  • 关外吴三桂 2020-12-18 15:46
    关注

    textView = findViewById(R.id.textview99);

    textView 绑定控件,应该这样使用; 

     

    希望帮助到你;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB代码补全插值
  • ¥15 Typegoose 中如何使用 arrayFilters 筛选并更新深度嵌套的子文档数组信息
  • ¥15 前后端分离的学习疑问?
  • ¥15 stata实证代码答疑
  • ¥50 husky+jaco2实现在gazebo与rviz中联合仿真
  • ¥15 dpabi预处理报错:Error using y_ExtractROISignal (line 251)
  • ¥15 在虚拟机中配置flume,无法将slave1节点的文件采集到master节点中
  • ¥15 husky+kinova jaco2 仿真
  • ¥15 zigbee终端设备入网失败
  • ¥15 金融监管系统怎么对7+4机构进行监管的