Java 应用程序意外停止,请重试

Java 应用程序意外停止,请重试,java,android,exception,runtime,textwatcher,Java,Android,Exception,Runtime,Textwatcher,当用户在我的android应用程序屏幕布局顶部的搜索框中键入内容时,我想过滤列表视图。为此,我使用textwatcher添加了一些代码,并运行了我的应用程序,但现在它开始出现错误,并且没有过滤列表。请帮助我解决此错误,并为我提供完美的列表代码自动列表筛选搜索.Thanx 日志: java文件的代码: xml文件的代码: 您正试图从初始值设定项对活动调用findViewById。这是行不通的。在调用setContentView之前,不能调用findViewById。文本视图不是EditView。因

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

日志:

java文件的代码:

xml文件的代码:


您正试图从初始值设定项对活动调用findViewById。这是行不通的。在调用setContentView之前,不能调用findViewById。

文本视图不是EditView。因此不可浇铸,因此抛出classcastexception


在XML中,将textView1声明为EditText。

这个问题措词不当,提供了太多无用的代码,而且看起来好像你没有试图自己解决这个问题。它是Trains.java中的NullPointerException,你是否将Trains.java定义为activity?代码与stacktrace中提到的内容无关!是的,我已经将Trains.java定义为manifest文件中的activity。我可以理解,u人正在考虑Trains.java中的NullPointerException,但是这个问题现在已经解决了。现在我在列表筛选方面遇到了问题。在搜索框中输入内容时,列表不会进行筛选。请帮助我完成此操作。谢谢。您可以提供声明此操作的代码吗?在您的XML文件中,将第一次提到的TextView替换为EditText…
   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
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 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>
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)