0

我正在开发一个从我的网络服务器接收图像和字符串的应用程序。这是有效的。但我想向 listView 添加搜索。这是我的 ApplicationActivity.java

package com.fireplace.market.fads;

import java.util.ArrayList; 
import java.util.List; 

import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.MenuItem;
import com.fireplace.market.fads.adapter.MyCustomArrayAdapter;
import com.fireplace.market.fads.database.GetDataFromDB;
import com.fireplace.market.fads.model.Model;

import android.app.ProgressDialog; 
import android.os.Bundle; 
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter; 
import android.widget.EditText;

public class ApplicationsActivity extends SherlockListActivity {

    EditText inputSearch;
    ArrayAdapter<String> adapter;

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

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        inputSearch = (EditText) findViewById(R.id.inputSearchApps);

        final List<Model> list = new ArrayList<Model>();
        /** This block is for getting the image url to download from the server **/ 
        final GetDataFromDB getvalues = new GetDataFromDB(); 
        final ProgressDialog dialog = ProgressDialog.show(ApplicationsActivity.this, "", "Gettting information", true);

        new Thread (new Runnable() { public void run() {
            String response = getvalues.getImageURLAndDesciptionFromDB(); System.out.println("Response : " + response);
            dismissDialog(dialog); 
            if (!response.equalsIgnoreCase("")) {
                if (!response.equalsIgnoreCase("error")) { 
                    dismissDialog(dialog);
                // Got the response, now split it to get the image Urls and description 
                String all[] = response.split("##"); for(int k = 0; k < all.length; k++){
                    String urls_and_desc[] = all[k].split(",");
                    // urls_and_desc[0] contains image url and [1] -> description. 
                    list.add(get(urls_and_desc[1],urls_and_desc[0])); 
                    } 
                } 
                }
            else { dismissDialog(dialog); } } }).start();
        /*************************** GOT data from Server ********************************************/ 
        ArrayAdapter<Model> adapter = new MyCustomArrayAdapter(this, list);
        setListAdapter(adapter); 

        //Making the list searchable
                inputSearch.addTextChangedListener(new TextWatcher() {

                    @Override
                    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                        // When user changed the Text
                        ApplicationsActivity.this.adapter.getFilter().filter(cs);
                    }

                    @Override
                    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                            int arg3) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void afterTextChanged(Editable arg0) {
                        // TODO Auto-generated method stub
                    }
                }); 
        } 

    public void dismissDialog(final ProgressDialog dialog){
        runOnUiThread(new Runnable() {
        public void run() {
            dialog.dismiss(); 
            } 
        }); 
        } 
    private Model get(String s, String url) {
        return new Model(s, url); 
        } 

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            super.finish();
            break;
        default:
            return super.onOptionsItemSelected(item);
        }
        return true;
    }
}

我得到了这个与我自己添加项目的 listView 一起工作。但是现在当我尝试输入时,我的应用程序上会出现强制关闭。问题是什么?

这是我的 LogCat

01-08 08:23:46.397: E/AndroidRuntime(30345): FATAL EXCEPTION: main
01-08 08:23:46.397: E/AndroidRuntime(30345): java.lang.NullPointerException
01-08 08:23:46.397: E/AndroidRuntime(30345):    at com.fireplace.market.fads.ApplicationsActivity$2.onTextChanged(ApplicationsActivity.java:62)
01-08 08:23:46.397: E/AndroidRuntime(30345):    at android.widget.TextView.sendOnTextChanged(TextView.java:7092)
01-08 08:23:46.397: E/AndroidRuntime(30345):    at android.widget.TextView.handleTextChanged(TextView.java:7151)
01-08 08:23:46.397: E/AndroidRuntime(30345):    at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:8794)
01-08 08:23:46.397: E/AndroidRuntime(30345):    at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:962)
01-08 08:23:46.397: E/AndroidRuntime(30345):    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)
01-08 08:23:46.397: E/AndroidRuntime(30345):    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
01-08 08:23:46.397: E/AndroidRuntime(30345):    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
01-08 08:23:46.397: E/AndroidRuntime(30345):    at android.text.method.QwertyKeyListener.onKeyDown(QwertyKeyListener.java:222)
01-08 08:23:46.397: E/AndroidRuntime(30345):    at android.text.method.TextKeyListener.onKeyDown(TextKeyListener.java:136)
01-08 08:23:46.397: E/AndroidRuntime(30345):    at android.widget.TextView.doKeyDown(TextView.java:5246)
01-08 08:23:46.397: E/AndroidRuntime(30345):    at android.widget.TextView.onKeyDown(TextView.java:5065)
01-08 08:23:46.397: E/AndroidRuntime(30345):    at android.view.KeyEvent.dispatch(KeyEvent.java:2609)
01-08 08:23:46.397: E/AndroidRuntime(30345):    at android.view.View.dispatchKeyEvent(View.java:7198)
01-08 08:23:46.397: E/AndroidRuntime(30345):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
01-08 08:23:46.397: E/AndroidRuntime(30345):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
01-08 08:23:46.397: E/AndroidRuntime(30345):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
01-08 08:23:46.397: E/AndroidRuntime(30345):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
01-08 08:23:46.397: E/AndroidRuntime(30345):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1920)
01-08 08:23:46.397: E/AndroidRuntime(30345):    at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1395)
01-08 08:23:46.397: E/AndroidRuntime(30345):    at android.app.Activity.dispatchKeyEvent(Activity.java:2370)
01-08 08:23:46.397: E/AndroidRuntime(30345):    at com.actionbarsherlock.app.SherlockListActivity.dispatchKeyEvent(SherlockListActivity.java:116)
01-08 08:23:46.397: E/AndroidRuntime(30345):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1847)
01-08 08:23:46.397: E/AndroidRuntime(30345):    at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3701)
01-08 08:23:46.397: E/AndroidRuntime(30345):    at android.view.ViewRootImpl.deliverKeyEvent(ViewRootImpl.java:3626)
01-08 08:23:46.397: E/AndroidRuntime(30345):    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3161)
01-08 08:23:46.397: E/AndroidRuntime(30345):    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292)
01-08 08:23:46.397: E/AndroidRuntime(30345):    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271)
01-08 08:23:46.397: E/AndroidRuntime(30345):    at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2976)
01-08 08:23:46.397: E/AndroidRuntime(30345):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-08 08:23:46.397: E/AndroidRuntime(30345):    at android.os.Looper.loop(Looper.java:137)
01-08 08:23:46.397: E/AndroidRuntime(30345):    at android.app.ActivityThread.main(ActivityThread.java:5039)
01-08 08:23:46.397: E/AndroidRuntime(30345):    at java.lang.reflect.Method.invokeNative(Native Method)
01-08 08:23:46.397: E/AndroidRuntime(30345):    at java.lang.reflect.Method.invoke(Method.java:511)
01-08 08:23:46.397: E/AndroidRuntime(30345):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-08 08:23:46.397: E/AndroidRuntime(30345):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-08 08:23:46.397: E/AndroidRuntime(30345):    at dalvik.system.NativeStart.main(Native Method)

因此,正如 LogCat 所说,当我按下一个键时会出现问题。在这种情况下,这是键盘。

4

3 回答 3

1

您尚未ArrayAdapter<String> adapter;在活动中初始化 。只需将其初始化为onCreate()like adapter=new ArrayAdapter<String>();

我认为这是唯一的问题。

于 2013-01-08T07:39:28.913 回答
1

代替

ArrayAdapter<Model> adapter = new MyCustomArrayAdapter(this, list);

adapter = new MyCustomArrayAdapter(this, list);

并将适配器的类型更改为ArrayList<Model>. 否则实例变量适配器不会被初始化。

于 2013-01-08T07:39:49.393 回答
0

如果您从过滤器更改文本值,您将得到一个无限循环:您应该像这样声明一个全局布尔变量:

private boolean canFilter = true;

然后在您的过滤器调用中:

if (canFilter){
    // We don't let any filter calls to pass until this finishes
    canFilter = false;
    ApplicationsActivity.this.adapter.getFilter().filter(cs);
    canFilter = true;
}

无论如何,这只是猜测。该filter(CharSequence sequence)方法有什么作用?

于 2013-01-08T09:14:06.950 回答