2013-07-02 132 views
2

我知道有很多人問過同樣的事情,但我只是不知道發生了什麼。我試圖做出像this 這樣的功能,它由2個EditText s,2個ListView s和1個按鈕組成。對於我的應用程序,用戶將輸入內EditText(1爲用戶位置,另一個爲目的地)我的目的包括ListView是當用戶輸入1或2個字母,listView顯示有關字母的地方的建議。但是,ListView是隱藏的 - 只有當用戶將輸入放入EditText時,它纔會顯示列表。獲取java.lang.RuntimeException:無法啓動活動ComponentInfo

我的代碼沒有顯示任何錯誤,但是當我運行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> 
+1

什麼線FindMePlace.java 80? – Raghunandan

+0

這是線80 lv.setTextFilterEnabled(真); – riehime23

+0

擴展'ListActivity'。同時向我們展示xml代碼。 – Raghunandan

回答

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。

 <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 

} 
+0

嚴更新的問題。我知道,我從很多網站閱讀,當我們做了多個listview時,我應該使用Activity而不是ListActivity。 – riehime23

+0

多個listview?我不明白你想告訴你什麼。 setListAdapter是'ListActivity' – Raghunandan

+0

我的意思是方法我有兩個列表視圖1個活動 – riehime23

0

您的getListView()方法返回硬編碼爲空。通向NPE下面一行setTextFilterEnabled

+0

感謝回答 – riehime23

0

您還沒有擴展ListActivity,因此不能使用getListView() ..

你應該id找到它,當你不延長ListActivity。 如果您的列表中有id平等list然後順便問

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

它可以有任何id ..

我覺得ListActivity種讓你僅限於ID list ..我不不喜歡那種行爲。

然而ListActivity有一些很好的方法與清單的工作,但我喜歡Activity更好......

+0

感謝您的幫助:) – riehime23

相關問題