获取java.lang.RuntimeException:无法启动活动ComponentInfo

时间:2013-07-02 16:26:19

标签: android

我知道有很多人问过同样的事情,但我不知道发生了什么。我正在尝试制作像this这样的函数 它由2 EditText s,2 ListView和1个按钮组成。对于我的应用程序,用户将输入放在EditText内(1表示用户位置,另1表示目的地)我包含ListView的目的是当用户输入输入1或2个字母时,listView显示建议有关字母的地方。但是,ListView被隐藏 - 只有当用户将输入放入EditText时,它才会显示列表。

我的代码没有显示任何错误,但是当我运行logCat时给我这个:

07-02 15:57:39.160: E/AndroidRuntime(5545): FATAL EXCEPTION: main
07-02 15:57:39.160: E/AndroidRuntime(5545): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.series3/com.example.series3.FindMePlace}: java.lang.NullPointerException
07-02 15:57:39.160: E/AndroidRuntime(5545):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
07-02 15:57:39.160: E/AndroidRuntime(5545):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-02 15:57:39.160: E/AndroidRuntime(5545):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-02 15:57:39.160: E/AndroidRuntime(5545):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-02 15:57:39.160: E/AndroidRuntime(5545):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-02 15:57:39.160: E/AndroidRuntime(5545):     at android.os.Looper.loop(Looper.java:123)
07-02 15:57:39.160: E/AndroidRuntime(5545):     at android.app.ActivityThread.main(ActivityThread.java:3683)
07-02 15:57:39.160: E/AndroidRuntime(5545):     at java.lang.reflect.Method.invokeNative(Native Method)
07-02 15:57:39.160: E/AndroidRuntime(5545):     at java.lang.reflect.Method.invoke(Method.java:507)
07-02 15:57:39.160: E/AndroidRuntime(5545):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-02 15:57:39.160: E/AndroidRuntime(5545):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-02 15:57:39.160: E/AndroidRuntime(5545):     at dalvik.system.NativeStart.main(Native Method)
07-02 15:57:39.160: E/AndroidRuntime(5545): Caused by: java.lang.NullPointerException
07-02 15:57:39.160: E/AndroidRuntime(5545):     at com.example.series3.FindMePlace.onCreate(FindMePlace.java:80)
07-02 15:57:39.160: E/AndroidRuntime(5545):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-02 15:57:39.160: E/AndroidRuntime(5545):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-02 15:57:39.160: E/AndroidRuntime(5545):     ... 11 more

认真地说我真的没有想法我错了。请帮我解决这个问题 这是我的代码:

FindMePlace.java

public class FindMePlace extends Activity {

    // ------------------------------------------------------------------
    // Declaration
    public static UkmRoute selectedPath = null;
    final DatabaseHandler db = new DatabaseHandler(this);
    private EditText filterText = null;
    private EditText filterText2 = null;
    ArrayAdapter<String> adapter = null;
    final ArrayList<String> results = new ArrayList<String>();
    final ArrayList<String> results_id = new ArrayList<String>();
    Button search;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.findmeplace);
        // final Intent c = new Intent(FindMePlace.this,
        // QSLocationDetail.class);

        // ------------------------------------------------------------------
        // Link editText to layout item
        filterText = (EditText) findViewById(R.id.search_Location);
        filterText.addTextChangedListener(filterTextWatcher);
        filterText2 = (EditText) findViewById(R.id.search_Destination);
        filterText2.addTextChangedListener(filterTextWatcher);

        /*populateFromLocation();
        populateToDestination();*/

            // Reading location
            Log.d("Reading", "Reading all location ..");
            List<UkmRoute> location = db.getAllUKMRoute();

            for (UkmRoute k : location) {
                results.add(k.getFromLocation());
                results_id.add(k.getID());
            }
            // Set list arrayAdapter to adapter
            if (!filterText.getText().toString().equals(""))
                adapter = new ArrayAdapter<String>(FindMePlace.this,
                        R.layout.list_item, R.id.textView1, results);
            else {
                ArrayList<String> r = new ArrayList<String>();
                adapter = new ArrayAdapter<String>(FindMePlace.this,
                        R.layout.list_item, R.id.textView1, r);
            }

            // adapter = new ArrayAdapter<String>(this, R.layout.list_item,
            // R.id.textView1, results);
            setListAdapter(adapter);

            // Set ListView from ListActivity
            ListView lv = getListView();
            lv.setTextFilterEnabled(true);

            lv.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    // When clicked, show a toast with the TextView text
                    Toast.makeText(getApplicationContext(),
                            ((TextView) view).getText(), Toast.LENGTH_SHORT).show();

                    Log.d("test", "position:" + position);

                    Log.d("test",
                            "actualname:"
                                    + db.getUkmRouteByFrom(
                                            adapter.getItem(position))
                                            .getFromLocation());

                    // String poiID = results_id.get(position);
                    String poiID = db.getUkmRouteByFrom(adapter.getItem(position))
                            .getID();
                    setSelectedPoi(poiID);
                    // startActivity(c);

                }
            });
//-------------------------------------------------------------------------------

                List<UkmRoute> destination = db.getAllUKMRoute();
                for (UkmRoute k : destination) {
                    results.add(k.getToDestination());
                    results_id.add(k.getID());
                }
                if (!filterText2.getText().toString().equals(""))
                    adapter = new ArrayAdapter<String>(FindMePlace.this,
                            R.layout.list_item, R.id.textView1, results);
                else {
                    ArrayList<String> r = new ArrayList<String>();
                    adapter = new ArrayAdapter<String>(FindMePlace.this,
                            R.layout.list_item, R.id.textView1, r);
                }
                setListAdapter(adapter);

                // Set ListView from ListActivity
                ListView lv2 = getListView();
                lv2.setTextFilterEnabled(true);

                // Set click event from listView
                lv2.setOnItemClickListener(new OnItemClickListener() {
                    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
                        // When clicked, show a toast with the TextView text
                        Toast.makeText(getApplicationContext(),
                                ((TextView) view).getText(), Toast.LENGTH_SHORT).show();

                        Log.d("test", "position:" + position);

                        Log.d("test",
                                "actualname:"
                                        + db.getUkmRouteByTo(adapter.getItem(position))
                                                .getFromLocation());

                        // String poiID = results_id.get(position);
                        String poiID = db.getUkmRouteByTo(adapter.getItem(position))
                                .getID();
                        setSelectedPoi(poiID);
                        // startActivity(c);

                    }
                });

    }

    // Initiate database data
        public void initiateDb() {
            DatabaseHandler myDbHandler = new DatabaseHandler(this);

            try {
                myDbHandler.createDataBase();
            } catch (IOException ioe) {
                throw new Error("Unable to create database");
            }

            try {
                myDbHandler.openDataBase();
            } catch (SQLException sqle) {
                throw sqle;
            }
            Log.d("Initiate",
                    "UKM Route Count: " + myDbHandler.getUkmRouteCount());
            myDbHandler.close();
        }

    // ------------------------------------------------------------------
    private ListView getListView() {
        // TODO Auto-generated method stub
        return null;
    }

    private void setListAdapter(ArrayAdapter<String> adapter2) {
        // TODO Auto-generated method stub

    }

    private TextWatcher filterTextWatcher = new TextWatcher() {

        public void afterTextChanged(Editable s) {
        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            if (!filterText.getText().toString().equals(""))
                adapter = new ArrayAdapter<String>(FindMePlace.this,
                        R.layout.list_item, R.id.textView1, results);
            else {
                ArrayList<String> r = new ArrayList<String>();
                adapter = new ArrayAdapter<String>(FindMePlace.this,
                        R.layout.list_item, R.id.textView1, r);
            }
            setListAdapter(adapter);
            adapter.getFilter().filter(s);
        }
    };

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

    public UkmRoute getSelectedPoi() {
        return selectedPath;
    }

    public void setSelectedPoi(String ID_route) {
        selectedPath = db.getUkmRoute(ID_route);
        Log.d("test2", "ID_route:" + db.getUkmRoute(ID_route).getID());
        Log.d("test2", "FromLocation:" + db.getUkmRoute(ID_route).getFromLocation());
        // kene buat if else ke kalau nk tmbah part Destination?
        // Closing db
        db.close();
    }


}

findmeplace.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".FindMePlace" >

    <EditText
        android:id="@+id/search_Destination"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/search_box"
        android:layout_centerVertical="true"
        android:ems="10"
        android:hint="@string/edittext_hint1"
        android:inputType="text"
        android:maxLines="1" />

    <EditText
        android:id="@+id/search_Location"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="138dp"
        android:ems="10"
        android:hint="@string/edittext_hint"
        android:inputType="text"
        android:maxLines="1" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/search_box"
        android:layout_marginBottom="71dp"
        android:text="@string/searchbutton" />

    <ListView
        android:id="@+id/inputLocation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/search_Location"
        android:layout_below="@+id/search_Location"
        android:layout_marginLeft="32dp"
        android:layout_marginTop="20dp" >
    </ListView>

    <ListView
        android:id="@+id/inputDestination"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/search_Destination"
        android:layout_below="@+id/search_Destination"
        android:layout_marginTop="15dp" >
    </ListView>

</RelativeLayout>

LIST_ITEM

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp" >
</TextView>

3 个答案:

答案 0 :(得分:1)

您的列表视图未初始化。

      ListView lv = getListView(); // lv is null

http://developer.android.com/reference/android/app/ListActivity.html

ListActivity的默认布局包含屏幕中央的单个全屏列表。但是,如果需要,可以通过在onCreate()中使用setContentView()设置自己的视图布局来自定义屏幕布局。 为此,您自己的视图必须包含一个ID为“@android:id / list”的ListView对象(如果它在代码中则列出)

如果扩展ListActivity,您可以拥有单个列表视图。

     <ListView
    android:id="@android:id/list" 

您的课程不会延长ListActivtiy。确保延长ListActivity

如果您延长Activity,则应执行以下操作

    ListView lv = (ListView) findViewById(R.id.inputLocation); 
    lv.setAdapter(adapter);

你也有以下方法,什么都不做,应该删除。

private ListView getListView() {
    // TODO Auto-generated method stub
    return null;
}

private void setListAdapter(ArrayAdapter<String> adapter2) {
    // TODO Auto-generated method stub

}

答案 1 :(得分:0)

您的getListView()方法返回硬编码的null。在setTextFilterEnabled

下面的NPE一行

答案 2 :(得分:0)

您尚未展开ListActivity,因此无法使用getListView() ..

如果您不延长id,则应ListActivity找到它。 如果您的列表的id等于list,则:

ListView lv = (ListView) findViewById(R.id.list);

顺便说一下,它可以有id ..

我认为ListActivity只限制了list ..我不喜欢这种行为。

ListActivity有一些很好的方法可以使用列表,但我更喜欢Activity ...