android - 在 fragment 的微调器中使用 setOnItemSelectedListener 时出现错误?

标签 android xml android-layout android-fragments android-spinner

我想在我的 android fragment 中使用微调器。为此,我编写了以下代码:

package com.example.shiza.dailyquranverses;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;


/**
 * A simple {@link Fragment} subclass.
 */
public class completeQuran extends Fragment implements AdapterView.OnItemSelectedListener {
    Spinner spinner;


    public completeQuran() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {

        View view = inflater.inflate(R.layout.fragment_complete_quran, container, false);

        spinner = (Spinner)view.findViewById(R.id.selectChapter);
        ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity().getApplicationContext(),R.array.chapters,android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);

        return view;
    }


    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        int quran_id;
        String chapter_verse="";
        TextView textView;
        position++;
        String chapter_array_name = "chapter_" + position;

        quran_id = getResources().getIdentifier(chapter_array_name, "array", getActivity().getApplicationContext().getPackageName());
        String[] chapter = getResources().getStringArray(quran_id);

        for ( int item = 0 ; item < chapter.length ; item++ )
        {
//            if ( item > 0 )
//            {
//                chapter_verse += item + ". " ;
//            }
            chapter_verse += chapter[item] + "\n";
        }
        textView = (TextView)view.findViewById(R.id.verse);
        textView.setText(chapter_verse);
        textView.setMovementMethod(new ScrollingMovementMethod());
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent)
    {
        Toast.makeText(getActivity().getApplicationContext(),"Please enter your choice",Toast.LENGTH_LONG).show();
    }
}

我在

处遇到错误

textView.setText(chapter_verse);

在 onItemSelected 方法中。我正在使用它的 View 从 xml 获取值。与该 fragment 对应的 xml 如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <Spinner
        android:id="@+id/selectChapter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/backgroundColor"
        android:orientation="vertical">

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            android:scrollbars="vertical"
            tools:context="com.example.shiza.dailyquranverses.Quran">

            <TextView
                android:id="@+id/verse"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:maxLines="2000"
                android:scrollbars="vertical"
                android:text="Hello from Quran"
                android:textSize="@dimen/verse_font_chapter" />
        </LinearLayout>
    </ScrollView>
</LinearLayout>

请帮我解决这个问题。

最佳答案

onItemSelected 回调会告诉您何时选择 Spinner 中的某个项目。在该回调中作为参数存在的 View 是所选 Spinner 中的 View。因此,我猜测您的 textView 变量将为 null。您尝试访问的 View 并不包含在正在选择的 View 内,它通常包含在您的布局中。您应该在 OnCreateView 内部设置对该 TextView 的引用,如下所示:

public class completeQuran extends Fragment implements AdapterView.OnItemSelectedListener 
{
    Spinner spinner;
    TextView textView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)
    {

        View view = inflater.inflate(R.layout.fragment_complete_quran, container, false);

        spinner = (Spinner)view.findViewById(R.id.selectChapter);
        ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity().getApplicationContext(),R.array.chapters,android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);

        textView = (TextView)view.findViewById(R.id.verse); //notice the view that this is finding your textview within

        return view;
    }

}

然后您可以在 onItemSelected 回调中使用对 textView 的引用。您不需要每次都重新定义引用。

关于android - 在 fragment 的微调器中使用 setOnItemSelectedListener 时出现错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30834843/

相关文章:

Android studio 3.1.4 工具栏卡在角落里

android - 如何为android创建这种布局

java - 获取请求在 Android 设备上不起作用,但它在我的电脑上起作用

xml - Tally 与 XML 和 HTTP 的集成 : Ledger creation failure

java - XSLT 与 javax.xml.transform 的乘法结果不正确 (0.2*0.8*0.8)

java - Wifi 管理器和 WiFi 信息

android - 如何在android中创建一个可点击的不规则形状区域?

android - "big"pouchdb 上的附件与 sqlite 卡住

android - 显示 Activity 并在加载其元素后

android - 你能用我的代码找出错误吗?