使用addTextChangedListener会产生NullPointerException

时间:2015-06-16 18:03:15

标签: java android

我有一个列表和一个搜索框。我的目标是在搜索框中发生更改后更新列表。

我正在使用带TextWatcher的addTextChangedListener函数。 它给了我一个NullPointerException。我正确地初始化了搜索edittext和列表视图,我不知道是什么问题。

代码:

MainActivityFragment.java

      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;

    }
}

MainActitviy.java

    package com.example.amr.spotifystreamer;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }}

这是Logcat:

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文件(包含列表视图和搜索editText)

<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>

1 个答案:

答案 0 :(得分:0)

您的编辑TextID不正确:

EditText searchText =(EditText)rootview.findViewById(R.id.search_bar);

应该是

EditText searchText =(EditText)rootview.findViewById(R.id.searchBar);