0
所以我試圖編程這個搜索活動,當我在我的模擬器中執行搜索時,我得到一個錯誤在我的日誌貓說,活動不能開始,我可以不知道是什麼原因導致了這個錯誤。搜索數據時無法啓動搜索活動android
搜索活動代碼:
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 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>
第一次移動'db = new DatabaseTable(this);'SearchResultsActivity的onCreate方法內部和應用程序崩潰時發佈日誌 –
你說你在日誌中有錯誤貓......我們應該猜猜它是什麼?發佈它,有人可能會提供幫助。 – Squonk
oh srry,忘記發帖 – user1930452