应用程序意外停止,请再试一次

时间:2012-03-05 15:44:01

标签: java android exception runtime textwatcher

我希望在用户在我的Android应用程序的屏幕布局顶部的搜索框中键入时过滤列表视图。为此,我使用textwatcher添加了一些代码并运行了我的应用程序,但现在它开始给出错误了它没有过滤列表。请帮我解决这个错误,并为我提供自动列表过滤搜索的完美代码.Thanx

logcat的:

   03-05 20:51:34.560: E/log_tag(897): Error is:java.lang.ClassC
   astException:                  android.widget.TextView
    03-05 20:51:38.009: D/AndroidRuntime(897): Shutting down VM
    03-05 20:51:38.009: W/dalvikvm(897): threadid=1: thread exiting with     uncaught             exception (group=0x4001d800) 
    03-05 20:51:38.029: E/AndroidRuntime(897): FATAL EXCEPTION: main
    03-05 20:51:38.029: E/AndroidRuntime(897): java.lang.RuntimeException: Unable to          instantiate activity                     ComponentInfo{com.example.FirstProject/com.example.FirstProject.Trains}:             java.lang.NullPointerException
        03-05 20:51:38.029: E/AndroidRuntime(897):  at         android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
        03-05 20:51:38.029: E/AndroidRuntime(897):  at         android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
          03-05 20:51:38.029: E/AndroidRuntime(897):    at         android.app.ActivityThread.access$2300(ActivityThread.java:125)
        03-05 20:51:38.029: E/AndroidRuntime(897):  at         android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
        03-05 20:51:38.029: E/AndroidRuntime(897):  at         android.os.Handler.dispatchMessage(Handler.java:99)
        03-05 20:51:38.029: E/AndroidRuntime(897):  at     android.os.Looper.loop(Looper.java:123)
        03-05 20:51:38.029: E/AndroidRuntime(897):  at         android.app.ActivityThread.main(ActivityThread.java:4627)
        03-05 20:51:38.029: E/AndroidRuntime(897):  at         java.lang.reflect.Method.invokeNative(Native Method)
        03-05 20:51:38.029: E/AndroidRuntime(897):  at         java.lang.reflect.Method.invoke(Method.java:521)
    03-05 20:51:38.029: E/AndroidRuntime(897):  at         com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
      03-05 20:51:38.029: E/AndroidRuntime(897):    at         com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
       03-05 20:51:38.029: E/AndroidRuntime(897):   at     dalvik.system.NativeStart.main(Native     Method)
        03-05 20:51:38.029: E/AndroidRuntime(897): Caused by:     java.lang.NullPointerException
          03-05 20:51:38.029: E/AndroidRuntime(897):    at         android.app.Activity.findViewById(Activity.java:1637)
        03-05 20:51:38.029: E/AndroidRuntime(897):  at com.example.FirstProject.Trains.    <init>    (Trains.java:29)
         03-05 20:51:38.029: E/AndroidRuntime(897):     at         java.lang.Class.newInstanceImpl(Native Method)
        03-05 20:51:38.029: E/AndroidRuntime(897):  at         java.lang.Class.newInstance(Class.java:1429)
        03-05 20:51:38.029: E/AndroidRuntime(897):  at         android.app.Instrumentation.newActivity(Instrumentation.java:1021)
        03-05 20:51:38.029: E/AndroidRuntime(897):  at         android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
         03-05 20:51:38.029: E/AndroidRuntime(897):     ... 11 more

java文件的代码:

package com.example.FirstProject;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
 import android.widget.Filterable;
 import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

 public class City extends ListActivity{

protected ArrayAdapter adapter;
ListView lv;
Cursor cursor;
protected String[] cities =                {"Mumbai","Nashik","Pune","Nagpur","Kolhapur","Aurangabad","Amravati"};

 @Override
    public void onCreate(Bundle savedInstanceState)
 {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.screen1);
    try{ 
     filterText = (EditText) findViewById(R.id.textView1);
     filterText.addTextChangedListener(filterTextWatcher);
    }
    catch(Exception e)
    {
        Log.e("log_tag", "Error is:"+e.toString());

    }
      adapter = new ArrayAdapter<String>(this,         android.R.layout.simple_list_item_1, cities);
     //ListView City = (ListView) findViewById(android.R.id.list);
     setListAdapter(adapter);

 }

 private TextWatcher filterTextWatcher = new TextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {
            lv.setFilterText(s.toString());
        }
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }
        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            ((Filterable) adapter).getFilter().filter(s);
        }



    };
private TextView filterText;



 public void onListItemClick(ListView parent, View view, int position, long id) {
        super.onListItemClick(parent, view, position, id);
        switch(position)
        {
        case 0: Intent i = new Intent(this, Mumbai.class);
                startActivity(i);
                break;
        }
    }

 @Override
    protected void onDestroy() {
        super.onDestroy();
        filterText.removeTextChangedListener(filterTextWatcher);
    }

}

xml文件的代码:

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/City"
    android:textAppearance="?android:attr/textAppearanceMedium" />

 <EditText android:id="@+building_list/search_box" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="type to filter"
    android:inputType="text"
    android:maxLines="1"/>



<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <!-- Preview: listitem=@android:layout/simple_list_item_1 -->
</ListView>






</LinearLayout>

2 个答案:

答案 0 :(得分:2)

03-05 20:51:38.029: E/AndroidRuntime(897): Caused by:     java.lang.NullPointerException
03-05 20:51:38.029: E/AndroidRuntime(897):    at         android.app.Activity.findViewById(Activity.java:1637)
03-05 20:51:38.029: E/AndroidRuntime(897):  at com.example.FirstProject.Trains.    <init>    (Trains.java:29)

您正尝试从初始化程序中findViewById()拨打Activity。这不行。在之后 findViewById()之前,您无法致电setContentView()

答案 1 :(得分:0)

TextView不是EditView。 (因此不能施放,因此抛出classcastexception)

在XML中,将textView1声明为EditText。