搜索查看空引用android

时间:2017-11-30 05:09:46

标签: java android xml

我看到这篇与我的问题有关的帖子。 link

我读完了问题和答案,我尝试了所有这些但是没有一个能为我工作。 所以最后我尝试进行一些日志聊天,看看问题的哪一部分。

这是我的menu_search.xml:

<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:title="Search"
    android:id="@+id/action_search"
    android:icon="@drawable/magnifier24"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="always|collapseActionView"/>
</menu>

这是我的MainActivity.java:

public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {
private static final String LogTAG = MainActivity.class.getSimpleName();

String [] cNames = {
        "Error - 128",
        "Upload - 128",
        "Loading - 128",
        "Warning - 128",
        "Checked - 128",
        "Magnifier - 24",
        "Heart - 32",
        "Save - 16",
        "Save - 64"
};

int[] cIcon = {
        R.drawable.error128,
        R.drawable.upload128,
        R.drawable.loading128,
        R.drawable.warning128,
        R.drawable.checked128,
        R.drawable.magnifier24,
        R.drawable.heart32,
        R.drawable.heart64,
        R.drawable.save16
};

Toolbar toolbar;
RecyclerView recyclerView;
RecyclerAdapter recyclerAdapter;
RecyclerView.LayoutManager layoutManager;
ArrayList<Icon> arrayList = new ArrayList<>();

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

    toolbar = (Toolbar) findViewById(R.id.srchTool);
    setSupportActionBar(toolbar);

    recyclerView = (RecyclerView) findViewById(R.id.rvIcon);
    layoutManager = new GridLayoutManager(MainActivity.this, 3);
    //layoutManager = new LinearLayoutManager(this);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(layoutManager);

    int count = 0;

    for(String name : cNames){
        arrayList.add(new Icon(name, cIcon[count]));
        count++;
    }

    RecyclerAdapter myAdapter = new RecyclerAdapter(arrayList);
    recyclerView.setAdapter(myAdapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_search,menu);
    MenuItem menuItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
    searchView.setOnQueryTextListener(this);
    return true;
}

@Override
public boolean onQueryTextSubmit(String query) {
    return false;
}

@Override
public boolean onQueryTextChange(String newText) {
    Log.e(LogTAG, "newText : " + newText);

    if (!newText.isEmpty()){
        Log.e(LogTAG, "newText 1 : " + newText);
        newText = newText.toLowerCase();
        Log.e(LogTAG, "newText 2 : " + newText);
        ArrayList<Icon> newList = new ArrayList<>();
        Log.e(LogTAG, "newText 3 : " + newText);

        for (Icon icon : arrayList){
            Log.e(LogTAG, "newText 4 : " + icon.getName().toLowerCase());
            String name = icon.getName().toLowerCase();
            Log.e(LogTAG, "newText 5 : " + icon.getName().toLowerCase());
            if(name.contains(newText)) {
                Log.e(LogTAG, "newText 6 : " + icon.getName().toLowerCase());
                newList.add(icon);
                Log.e(LogTAG, "newText 7 : " + icon.getName().toLowerCase());
            }
        }
        Log.e(LogTAG, "newText 8 : " + newText);
        recyclerAdapter.setFilter(newList);

        Log.e(LogTAG, "newText 9 : " + newText);
        return true;
    } else {
        Log.e(LogTAG, "newText 999 : " + newText);
        return true;
    }
}
}

这是RecyclerAdapter.java:

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder>{
private static final String LogTAG = MainActivity.class.getSimpleName();

ArrayList<Icon> arrayList = new ArrayList<>();
RecyclerAdapter(ArrayList<Icon> arrayList){
    this.arrayList = arrayList;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout,parent,false);
    return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    holder.c_Name.setText(arrayList.get(position).getName());
    holder.c_Icon.setImageResource(arrayList.get(position).getIcon());
}

@Override
public int getItemCount() {
    return arrayList.size();
}

public static  class MyViewHolder extends RecyclerView.ViewHolder{
    ImageView c_Icon;
    TextView c_Name;


    public MyViewHolder(View itemView) {
        super(itemView);
        c_Icon = (ImageView) itemView.findViewById(R.id.imageView);
        c_Name = (TextView) itemView.findViewById(R.id.textView);
    }
}

public void setFilter(ArrayList<Icon> newList){

    Log.e(LogTAG, "newText 8.1");
    arrayList = new ArrayList<>();

    Log.e(LogTAG, "newText 8.2");
    arrayList.addAll(newList );

    Log.e(LogTAG, "newText 8.3");
    notifyDataSetChanged();
}
}

这是我的搜索toolbar.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
app:actionViewClass="android.support.v7.widget.SearchView"
android:id="@+id/srchTool">

</android.support.v7.widget.Toolbar>

这是格斗:

apply plugin: 'com.android.application'

android {
compileSdkVersion 27
defaultConfig {
    applicationId "com.afe.maja.searchview"
    minSdkVersion 16
    targetSdkVersion 27
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation 'com.android.support:recyclerview-v7:27.0.1'
implementation 'com.android.support:cardview-v7:27.0.1'
implementation 'com.android.support:design:27.0.1'
implementation 'com.android.support:appcompat-v7:27.0.1'
}

LogCat停在 E / MainActivity:newText 8:a 所以我猜问题出在 recyclerAdapter.setFilter(newList);

我是Android编程的新手,所以我会感激任何建议。

编辑了

,我忘了包含RecyclerAdapter.java

更新

我收到类似这样的错误消息:

11-30 13:56:25.274 12928-12928/com.afe.maja.searchview E/MainActivity: newText 8 : a
11-30 13:56:25.275 12928-12928/com.afe.maja.searchview E/InputEventSender: Exception dispatching finished signal.
11-30 13:56:25.275 12928-12928/com.afe.maja.searchview E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
11-30 13:56:25.276 12928-12928/com.afe.maja.searchview E/MessageQueue-JNI: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.afe.maja.searchview.RecyclerAdapter.setFilter(java.util.ArrayList)' on a null object reference
                                                                           at com.afe.maja.searchview.MainActivity.onQueryTextChange(MainActivity.java:119)
                                                                           at android.support.v7.widget.SearchView.onTextChanged(SearchView.java:1180)
                                                                           at android.support.v7.widget.SearchView$10.onTextChanged(SearchView.java:1714)
                                                                           at android.widget.TextView.sendOnTextChanged(TextView.java:8187)
                                                                           at android.widget.TextView.handleTextChanged(TextView.java:8249)
                                                                           at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:10371)
                                                                           at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:1208)
                                                                           at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:578)
                                                                           at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:509)
                                                                           at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:508)
                                                                           at android.text.method.QwertyKeyListener.onKeyDown(QwertyKeyListener.java:223)
                                                                           at android.text.method.TextKeyListener.onKeyDown(TextKeyListener.java:136)
                                                                           at android.widget.TextView.doKeyDown(TextView.java:6285)
                                                                           at android.widget.TextView.onKeyDown(TextView.java:6075)
                                                                           at android.widget.AutoCompleteTextView.onKeyDown(AutoCompleteTextView.java:829)
                                                                           at android.view.KeyEvent.dispatch(KeyEvent.java:2688)
                                                                           at android.view.View.dispatchKeyEvent(View.java:9960)
                                                                           at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1630)
                                                                           at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1630)
                                                                           at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1630)
                                                                           at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1630)
                                                                           at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1630)
                                                                           at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1630)
                                                                           at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1630)
                                                                           at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1630)
                                                                           at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1630)
                                                                           at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1630)
                                                                           at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1630)
                                                                           at com.android.internal.policy.DecorView.superDispatchKeyEvent(DecorView.java:405)
                                                                           at com.android.internal.policy.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1798)
                                                                           at android.app.Activity.dispatchKeyEvent(Activity.java:3021)
                                                                           at android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:534)
                                                                           at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:58)
                                                                           at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:316)
                                                                           at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:58)
                                                                           at com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:319)
                                                                           at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4331)
                                                                           at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4302)
                                                                           at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853)
                                                                           at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3906)
                                                                           at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3872)
                                                                           at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3999)
                                                                           at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3880)
                                                                           at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4056)
                                                                           at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853)
                                                                           at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3906)
                                                                           at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3872)
                                                                           at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3880)
                                                                           at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853)
                                                                           at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3906)
                                                                           at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3872)
                                                                        at android.view.ViewRootImpl$A

2 个答案:

答案 0 :(得分:1)

更改此

RecyclerAdapter myAdapter = new RecyclerAdapter(arrayList);

recyclerAdapter = new RecyclerAdapter(arrayList);

答案 1 :(得分:0)

将您的setFilter更改为

public void setFilter(ArrayList<Icon> newList){


    Log.e(LogTAG, "newText 8.1");

    if(arrayList != null){
       arrayList.clear();
    }

    Log.e(LogTAG, "newText 8.2");
    arrayList.addAll(newList );

    Log.e(LogTAG, "newText 8.3");
    notifyDataSetChanged();

}