Android 在片段的微调器中使用setOnItemSelectedListener时出错?
我想在我的android片段中使用微调器。为此,我编写了以下代码:Android 在片段的微调器中使用setOnItemSelectedListener时出错?,android,xml,android-layout,android-fragments,android-spinner,Android,Xml,Android Layout,Android Fragments,Android Spinner,我想在我的android片段中使用微调器。为此,我编写了以下代码: 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.v
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();
}
}
package com.example.shiza.dailyquranverses;
导入android.os.Bundle;
导入android.support.v4.app.Fragment;
导入android.text.method.ScrollingMovementMethod;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.AdapterView;
导入android.widget.ArrayAdapter;
导入android.widget.Spinner;
导入android.widget.TextView;
导入android.widget.Toast;
/**
*一个简单的{@link Fragment}子类。
*/
公共类completeQuran扩展片段实现AdapterView.OnItemSelectedListener{
纺纱机;
公共完整古兰经(){
//必需的空公共构造函数
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState)
{
视图=充气机。充气(R.layout.fragment\u complete\u quran,container,false);
微调器=(微调器)视图。findViewById(R.id.selectChapter);
ArrayAdapter=ArrayAdapter.createFromResource(getActivity().getApplicationContext(),R.array.chapters,android.R.layout.simple\u微调器\u下拉菜单\u项);
旋转器。设置适配器(适配器);
spinner.setOnItemSelectedListener(此);
返回视图;
}
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
国际古兰经;
弦章_韵文=”;
文本视图文本视图;
位置++;
字符串chapter\u array\u name=“chapter\u”+位置;
古兰经id=getResources().getIdentifier(第章数组名称,“数组”,getActivity().getApplicationContext().getPackageName());
String[]chapter=getResources().getStringArray(古兰经id);
对于(int item=0;item0)
// {
//第章第+=项+“;
// }
章节+=章节[项目]+“\n”;
}
textView=(textView)view.findViewById(R.id.verse);
textView.setText(第四章);
setMovementMethod(新的ScrollingMovementMethod());
}
@凌驾
未选择公共无效(AdapterView父级)
{
Toast.makeText(getActivity().getApplicationContext(),“请输入您的选择”,Toast.LENGTH\u LONG.show();
}
}
我犯了一个错误
textView.setText(第四章)
在选定的方法中。我使用它的视图从xml中获取值。与片段对应的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>
请帮我解决这个问题。这是因为setText吃了一个字符序列,而你给了一个字符串 你需要使用
textView.setText(String.valueOf(chapter_verse))
问题在于:
textView=(textView)view.findViewById(R.id.verse)
我需要使用getView()方法而不是view方法。当选择了
微调器中的某个项目时,onItemSelected
回调会告诉您。作为参数存在于该回调中的视图
是选中的微调器中的视图
。因此,我猜测您的textView
变量将是null
。您试图访问的视图
未包含在所选的视图
中,它通常包含在您的布局中。您应该在OnCreateView
中设置对该TextView
的引用,如下所示:
公共类completeQuran扩展片段实现AdapterView.OnItemSelectedListener
{
纺纱机;
文本视图文本视图;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState)
{
视图=充气机。充气(R.layout.fragment\u complete\u quran,container,false);
微调器=(微调器)视图。findViewById(R.id.selectChapter);
ArrayAdapter=ArrayAdapter.createFromResource(getActivity().getApplicationContext(),R.array.chapters,android.R.layout.simple\u微调器\u下拉菜单\u项);
旋转器。设置适配器(适配器);
spinner.setOnItemSelectedListener(此);
textView=(textView)view.findViewById(R.id.verse);//注意这个视图,它在
返回视图;
}
}
然后,您可以在onItemSelected
回调中使用该引用来查看textView
。您不需要每次都重新定义引用。我认为这不会有任何区别。