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

为什么我这里明明声明了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 绑定控件,应该这样使用; 

     

    希望帮助到你;

    打赏 评论
  • 何去处 2020-12-19 15:43

    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);

    }});}}

     

    这样写,没毛病

    打赏 评论
  • qq_1113502097 2020-12-22 11:31

    你这是对java代码的理解有误会吧,你声明了对象TextView textView99是没错,但是java是面对对象的,你声明对象的时候并没有对对象赋值,你就直接调用他的方法肯定报错。因此声明对象之后应该进行赋值。也就是=;textView99 = findViewById(R.id.textview99);这才是正确的赋值方式。而你的textView99.findViewById(R.id.textview99);实际上是调用额textview的findviewByid的方法,但对象只声明未赋值,就会出现空指针或者越界

    打赏 评论

相关推荐 更多相似问题