Java 使用addTextChangedListener会产生NullPointerException
我有一个列表和一个搜索框。我的目标是,一旦搜索框中发生更改,列表将被更新 我正在将addTextChangedListener函数与TextWatcher一起使用。 它给了我一个NullPointerException。我正确初始化了search edittext和list视图,不知道是什么问题 守则: MainActivityFragment.javaJava 使用addTextChangedListener会产生NullPointerException,java,android,Java,Android,我有一个列表和一个搜索框。我的目标是,一旦搜索框中发生更改,列表将被更新 我正在将addTextChangedListener函数与TextWatcher一起使用。 它给了我一个NullPointerException。我正确初始化了search edittext和list视图,不知道是什么问题 守则: MainActivityFragment.java package com.example.amr.spotifystreamer; import android.support.
package com.example.amr.spotifystreamer;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* A placeholder fragment containing a simple view.
*/
public class MainActivityFragment extends Fragment {
public MainActivityFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String[] data = {
"Amr Diab",
"Tamer Hosny",
"Hisham Abbas",
"Amr Youssef",
};
List<String> artist = new ArrayList<String>(Arrays.asList(data));
final ArrayAdapter<String> dataAdabter= new ArrayAdapter<String>(getActivity(),
R.layout.list_item_view,R.id.artisttextView,artist);
View rootview=inflater.inflate(R.layout.fragment_main, container, false);
ListView resultList= (ListView) rootview.findViewById(R.id.resultList);
resultList.setAdapter(dataAdabter);
EditText searchText=((EditText)rootview.findViewById(R.id.search_bar));
try{
searchText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
dataAdabter.getFilter().filter(s);
}
@Override
public void afterTextChanged(Editable s) {
}
});
}catch(Exception e){
Log.e("Error for Listener", e.toString());
}
return rootview;
}
}
下面是日志:
06-16 19:20:04.508: I/dalvikvm(4419): Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
06-16 19:20:04.508: W/dalvikvm(4419): VFY: unable to resolve virtual method 408: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
06-16 19:20:04.508: I/dalvikvm(4419): Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
06-16 19:20:04.508: W/dalvikvm(4419): VFY: unable to resolve virtual method 430: Landroid/content/res/TypedArray;.getType (I)I
06-16 19:20:04.548: I/dalvikvm(4419): Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawable
06-16 19:20:04.548: W/dalvikvm(4419): VFY: unable to resolve virtual method 371: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
06-16 19:20:04.548: I/dalvikvm(4419): Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawableForDensity
06-16 19:20:04.548: W/dalvikvm(4419): VFY: unable to resolve virtual method 373: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
06-16 19:20:04.598: W/dalvikvm(4419): threadid=1: thread exiting with uncaught exception (group=0x41612c80)
主片段XML文件(包含列表视图和搜索编辑器文本)
您的编辑文本ID不正确: EditText searchText=(EditText)rootview.findViewById(R.id.search\u bar) 应该是
EditText searchText=(EditText)rootview.findViewById(R.id.searchBar) 把你的战友发出去
06-16 19:20:04.508: I/dalvikvm(4419): Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
06-16 19:20:04.508: W/dalvikvm(4419): VFY: unable to resolve virtual method 408: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
06-16 19:20:04.508: I/dalvikvm(4419): Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
06-16 19:20:04.508: W/dalvikvm(4419): VFY: unable to resolve virtual method 430: Landroid/content/res/TypedArray;.getType (I)I
06-16 19:20:04.548: I/dalvikvm(4419): Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawable
06-16 19:20:04.548: W/dalvikvm(4419): VFY: unable to resolve virtual method 371: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
06-16 19:20:04.548: I/dalvikvm(4419): Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawableForDensity
06-16 19:20:04.548: W/dalvikvm(4419): VFY: unable to resolve virtual method 373: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
06-16 19:20:04.598: W/dalvikvm(4419): threadid=1: thread exiting with uncaught exception (group=0x41612c80)
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivityFragment">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<EditText
android:layout_width="364dp"
android:layout_height="wrap_content"
android:id="@+id/searchBar"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:singleLine="true"
/>
<ListView
android:layout_width="362dp"
android:layout_height="374dp"
android:id="@+id/resultList"
android:layout_gravity="left|center_vertical" />
</FrameLayout>
</RelativeLayout>