0

所以我正在尝试对这个搜索活动进行编程,当我在模拟器中执行搜索时,我的日志猫中出现一个错误,提示该活动无法启动,我无法弄清楚是什么导致了该错误。

搜索活动代码:

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;
} 

}

安卓清单

<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>

4

1 回答 1

0

这里

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(android.R.id.list);  //<<<<

您将 ListView id 添加为ListActivity. 只需setContentView(android.R.id.list); 从 ListActivity 的 onCreate 中删除并为 Activty 添加您自己的布局

并将 onCreate 方法中的数据库实例初始化为

 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

于 2012-12-28T02:05:23.393 回答