【发布时间】:2012-11-15 05:11:43
【问题描述】:
我喜欢将 addTextChangedListener 添加到我的 listView 中,但运行它后,它不会在屏幕上显示任何内容,只是一个空白屏幕。 logcat 也没有给出任何内容,只显示一个选项卡,即“ActivityThread.performLaunchAcitvity(ActivityThread$ActivityClientRecord,Intent)line:2205”,其下方显示“未找到源”和一个按钮(编辑源查找路径)。 我已经尝试了一些方法,但到目前为止还没有找到让它起作用的方法..
这里是 MainActivity:
public class MainActivity extends ListActivity {
ListView lv;
EditText inputSearch;
DefaultHttpClient httpclient;
HttpPost httppost;
HttpResponse response;
InputStream is;
BufferedReader reader;
StringBuilder sb;
String line,result;
String[] people;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getList();
lv= (ListView) findViewById(R.id.list_view);
inputSearch= (EditText) findViewById(R.id.inputSearch);
inputSearch.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if(s.length()==0){
lv.clearTextFilter();
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
lv.setTextFilterEnabled(true);
lv.setFilterText(s.toString());
}
});
}
public void getList(){
new Thread(){
public void run(){
try{
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://www.kryptoquest.com/testing/testList.php");
response = httpclient.execute(httppost);
is = response.getEntity().getContent();
}catch(Exception e){
Log.e("log_tag", "Error:"+e.toString());
}
//convert response to string
try{
reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
sb = new StringBuilder();
line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
Log.d("test",sb.toString());
is.close();
result = sb.toString();
people = result.split("[*]");
runOnUiThread(new Runnable()
{
public void run(){
ArrayAdapter<String> list = new ArrayAdapter<String>(MainActivity.this,R.layout.list_item, R.id.user_name,people);
lv.setAdapter(list);
}
});
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
}
}.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!--Editext for Search -->
<EditText android:id="@+id/inputSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Search.."
android:inputType="textVisiblePassword"/>
<!--List View -->
<ListView
android:id="@+id/list_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
list_item.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" >
<!--Single ListItem -->
<!--User Name -->
<TextView android:id="@+id/user_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold"/>
</LinearLayout>
另外,我在其 Manifest 中添加了 uses-permission android:name="android.permission.INTERNET">。
提前致谢。
【问题讨论】:
-
发布所有 Logcat 错误,以便我们查看发生了什么。
-
11-15 17:03:18.528: E/AndroidRuntime(23630): 致命异常: main 11-15 17:03:18.528: E/AndroidRuntime(23630): java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.testlistview/com.example.testlistview.MainActivity}:java.lang.RuntimeException:您的内容必须有一个 id 属性为 'android.R.id.list' 的 ListView 11-15 17 :03:18.528: E/AndroidRuntime(23630): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205) 11-15 17:03:18.528: E/AndroidRuntime(23630): 在 android.app.ActivityThread。 handleLaunchActivity(ActivityThread.java:2240)
-
11-15 17:03:18.528: E/AndroidRuntime(23630): 在 android.app.ActivityThread.access$600(ActivityThread.java:139) 11-15 17:03:18.528: E /AndroidRuntime(23630): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262) 11-15 17:03:18.528: E/AndroidRuntime(23630): 在 android.os.Handler.dispatchMessage(Handler. java:99) 11-15 17:03:18.528: E/AndroidRuntime(23630): 在 android.os.Looper.loop(Looper.java:156) 11-15 17:03:18.528: E/AndroidRuntime(23630) : 在 android.app.ActivityThread.main(ActivityThread.java:4987)
-
11-15 17:03:18.528: E/AndroidRuntime(23630): at java.lang.reflect.Method.invokeNative(Native Method) 11-15 17:03:18.528: E/AndroidRuntime (23630): 在 java.lang.reflect.Method.invoke(Method.java:511) 11-15 17:03:18.528: E/AndroidRuntime(23630): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller。运行(ZygoteInit.java:784)11-15 17:03:18.528:E/AndroidRuntime(23630):在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)11-15 17:03: 18.528: E/AndroidRuntime(23630): at dalvik.system.NativeStart.main(Native Method)
-
11-15 17:03:18.528: E/AndroidRuntime(23630): Caused by: java.lang.RuntimeException: 您的内容必须有一个 ID 属性为 'android.R.id.列表' 11-15 17:03:18.528: E/AndroidRuntime(23630): 在 android.app.ListActivity.onContentChanged(ListActivity.java:250) 11-15 17:03:18.528: E/AndroidRuntime(23630): 在com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:273) 11-15 17:03:18.528: E/AndroidRuntime(23630): 在 android.app.Activity.setContentView(Activity.java:1892 )
标签: android listview android-edittext