所以我正在尝试对此搜索活动进行编程,当我在我的模拟器中执行搜索时,我的日志cat中出现错误,说明活动无法启动,我无法弄清楚导致该错误的原因
搜索活动代码:
package com.example.myfirstapp;
import android.os.Bundle;
import android.app.SearchManager;
import android.app.ListActivity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.content.Intent;
import android.database.Cursor;
public class SearchResultsActivity extends ListActivity {
private ListView list;
DatabaseTable db = new DatabaseTable(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(android.R.id.list);
handleIntent(getIntent());
}
public void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_VIEW.equals(intent.getAction())) {
Intent contactIntent = new Intent(this, ContactActivity.class);
contactIntent.setData(intent.getData());
startActivity(contactIntent);
finish();
} else if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
showResults(query);
}
}
private void showResults(String query) {
int name = 0;
String c = "";
Cursor cursor = db.getContactMatches(query, null);
int cursor_length = db.cursorLength(cursor);
String[] display = new String[cursor_length];
list = (ListView)findViewById(android.R.id.list);
int x = 0;
//Loading data through Cursor into String array
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext())
{
name = cursor.getColumnIndex(DatabaseTable.COL_NAME);
c = (cursor.getString(name));
display[x] = c;
c = "";
x = x + 1;
name = 0;
}
ArrayAdapter<String> contactAdapter = new ArrayAdapter<String>(this,
R.layout.activity_search_results, android.R.id.list, display);
list.setAdapter(contactAdapter);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent contactIntent = new Intent(getApplicationContext(),ContactActivity.class);
contactIntent.setData(getIntent().getData());
startActivity(contactIntent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_search_results, menu);
return true;
}
}
Android Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchResultsActivity" />
</activity>
<activity
android:name=".SearchResultsActivity"
android:label="@string/title_activity_search_results"
android:launchMode="singleTop" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
<activity
android:name=".AddContacts"
android:label="@string/title_activity_add_contacts" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ContactActivity"
android:label="@string/title_activity_contact" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
答案 0 :(得分:0)
这里
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(android.R.id.list); //<<<<
您要将ListView ID添加为ListActivity
的内容视图。只需从ListActivity的onCreate中删除setContentView(android.R.id.list);
,然后为Activty
将onCreate方法中的数据库实例initulization移动为
DatabaseTable db;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // <<< add activity layout here
db = new DatabaseTable(this);
请参阅本教程,了解如何使用ListView创建布局:
http://www.java2s.com/Code/Android/UI/UsingImageViewwithinListActivity.htm