我知道有很多人問過同樣的事情,但我只是不知道發生了什麼。我試圖做出像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>
什麼線FindMePlace.java 80? – Raghunandan
這是線80 lv.setTextFilterEnabled(真); – riehime23
擴展'ListActivity'。同時向我們展示xml代碼。 – Raghunandan