【问题标题】:Crash when setText on TextView [duplicate]TextView上的setText时崩溃[重复]
【发布时间】:2016-09-28 20:12:10
【问题描述】:

当我尝试生成此代码时,出现此错误:

原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.TextView.setText(java.lang.CharSequence)”。

这是我的代码:

public class MainActivity extends AppCompatActivity implements   View.OnTouchListener{
private Tablero fondo;
int x;
int y;
private boolean activo = true;
int intento = 10;
String numerointentos;


     @Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    LinearLayout linearLayout1 = (LinearLayout) findViewById(R.id.linearLayout1);
    TextView Tiradas = (TextView) findViewById(R.id.Intentos) ;
    numerointentos = Integer.toString(intento);
    Tiradas.setText(numerointentos);

    fondo = new Tablero(this);
    fondo.setOnTouchListener(this);
    linearLayout1.addView(fondo);
    fondo.casillas = new Casilla[8][8];
    for (int f = 0; f < 8; f++){
        for (int c = 0; c < 8; c++){
            fondo.casillas[f][c] = new Casilla();
        }
    }

这是 .xml 代码:

<?xml version= "1.01" encoding="utf-8"?>

<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width= "fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:weightSum="1">
 <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/reiniciar"
    android:id="@+id/btnReiniciar"
    android:onClick="presionado"
    android:layout_gravity="center_horizontal" />

 <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:textAppearance="?android:attr/textAppearanceMedium"
     android:id="@+id/Intentos"
     android:layout_gravity="center_horizontal" />

显然有更多的代码和代码和类,但那是包含 setText 的唯一性。

还有一个问题。如果我想在另一个公共 void 或方法中修改 TextView 的值,我该怎么办?

感谢您的帮助,我在该网站上搜索了 anwsers,但无法解决。我不知道原因。谢谢!

【问题讨论】:

    标签: android textview settext


    【解决方案1】:

    TextView 的声明移动为类的属性

    public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
        public TextView Tiradas;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            setContentView( ... );
    
            Tiradas = (TextView) findViewById(R.id.Intentos);
            Tiradas.setText(numerointentos);
        }
    }
    

    编辑:

    确保您没有activity_main.xml 文件的其他变体(例如横向或更大的屏幕尺寸)。如果有,请将缺少的视图添加到该布局文件中。

    因为如果在引用的布局中找不到视图,findViewById(int) 将返回 null

    【讨论】:

    • 嗨,我将在@override 之前添加我所拥有的内容,看看吧,因为我没有相同的公共课程。谢谢!
    • 有新代码,谢谢!
    • 一样:) 只要Tiradas变量是类的变量(全局变量)(不是onCreate方法中的局部变量,那么如果你在onCreate 里面做setText。顺便说一句,我已经更新了我的代码,希望这会有所帮助
    • 嗨,我试着像你一样做,但一直在同一点崩溃。谢谢!
    • 来自findViewById documentation,好像没有找到视图。您确定 activity_main xml 布局文件有一个 ID 为 Intentos 的视图吗?或者您可能正在引用另一个布局。还要确保您没有 activity_main xml 文件的其他变体(适用于横向或更大的屏幕尺寸)
    【解决方案2】:

    您为什么不尝试将文本视图设为全局变量?然后您也可以在其他功能中访问该文本字段。

    像这样。

    public class MainActivity extends AppCompatActivity { TextView Tiradas; @Override protected void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManAger.LayoutParams.FLAG_FULLSCREEN); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); LinearLayout linearLayout1 = (LinearLayout) findViewById(R.id.linearLayout1); Tiradas = (TextView) findViewById(R.id.Intentos) ; numerointentos = Integer.toString(intento); Tiradas.setText(numerointentos); fondo = new Tablero(this); } }

    【讨论】:

    • 嗨 Snippy,感谢您的回答,但是,我是 Java 和 Android 的新手,那么您对 ​​Logcat 是什么意思?谢谢和抱歉!
    • Log cat 是 Android Studio 用来显示输出的输出终端。
    • 嗨 Snippy,但我没有相同的公共课程,请查看新代码,谢谢!不知道能不能改公开课。
    猜你喜欢
    • 1970-01-01
    • 2016-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多