【问题标题】:text view returning null [duplicate]返回null的文本视图[重复]
【发布时间】:2016-07-03 11:55:15
【问题描述】:

我有 2 个类 BuyCoins 和 CustomOnItemSelectListerner。 BuyCoins 类包含一个微调器,一旦选择了微调器,就会调用 CustomOnItemSelectListerner 类。

public class BuyCoins extends AppCompatActivity {
...
    public void addListenerOnSpinnerItemSelection() {
    spinner1 = (Spinner) findViewById(R.id.spinner1);
    //TextView t=(TextView) findViewById(R.id.conversion);
    spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener(t));
    }
...
}

public class CustomOnItemSelectedListener implements OnItemSelectedListener {
    //private TextView t;

    //CustomerOnItemSelected (TextView V) {t=V;}   

    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

        String selected= parent.getItemAtPosition(pos).toString();
        TextView t=(TextView)parent.findViewById(R.id.conversion);

        switch(selected) {
            case "A":
                Toast.makeText(parent.getContext(),
                "OnItemSelectedListener : " + selected,
                Toast.LENGTH_SHORT).show();
                t.setText("$ala$");
                break;
        }
   ...
}

我的问题是 t.setText 返回 null,但是 case 语句的其余部分工作正常。请帮忙。

带有微调器的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="50dp"
android:paddingTop="50dp"
android:paddingRight="50dp">

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


    <ImageView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:id="@+id/coinpic"
        android:src="@drawable/coin"
        android:scaleX="0.8"
        android:scaleY="0.8"
        android:layout_gravity="center_horizontal" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/coin_array"
        android:prompt="@string/coin_prompt" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text=""
        android:id="@+id/conversion"
        android:layout_gravity="right" />

    </LinearLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Purchase Coins"
        android:id="@+id/Pur_button"
        android:layout_gravity="center_horizontal"
        android:onClick="toast"/>

</LinearLayout>

【问题讨论】:

  • 感谢您将其标记为重复...您可能知道为什么我在这种情况下会出现错误。
  • 请分享您的 xml 布局。
  • 您能否将xml粘贴到您定义ID“转换”视图的位置。那是自定义微调器项目吗?
  • 你试试这个:TextView t=(TextView)view.findViewById(R.id.conversion);
  • 嗨试过它甚至不允许我选择微调器而不轰炸

标签: java android


【解决方案1】:

您尝试访问的 TextView 属于您的整体布局,而不是微调器,因此您需要使用以下方法从 Activity 访问它:

TextView t=(TextView) findViewById(R.id.conversion);

您可以通过在您的 CustomOnItemSelectListener 中创建一个新的构造函数将它传递给您的侦听器,该构造函数接受一个 TextView,您可以将其设置为一个成员变量,然后在下面调用 setText。

然后在你的 Acticity 中创建监听器时,你调用新的构造函数来传递 TextView:

    new CustomOnItemSelectListener(t);

【讨论】:

  • 谢谢,但没有用.. 它甚至不允许我选择微调器而不用同样的错误轰炸
  • 我已经根据您发布的 xml 更改了我的答案,对不起,我误解并认为转换视图是微调器项目布局的一部分。
  • 非常感谢它的工作,你是一个绝对的明星......我已经用我所做的更改更新了上面的代码 - 请参阅原始代码中的 cmets......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-04
  • 2011-11-01
  • 2017-05-09
  • 2012-02-01
  • 2015-09-11
相关资源
最近更新 更多