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<Model> adapter = new MyCustomArrayAdapter(this, list);

adapter = new MyCustomArrayAdapter(this, list);

アダプターのタイプをに変更しArrayList<Model>ます。それ以外の場合、インスタンス変数アダプターは初期化されません。

于 2013-01-08T07:39:49.393 に答える
1

アクティビティで を初期化していませんArrayAdapter<String> adapter;onCreate()likeで初期化するだけadapter=new ArrayAdapter<String>();です。

それが唯一の問題だと思います。

于 2013-01-08T07:39:28.913 に答える
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 に答える