内容必须具有ListView

时间:2014-05-05 01:22:59

标签: android android-listview

Newbee在Android上工作。我有一个ListView。不确定,我哪里出错了。我在Logcat中看到此错误消息 05-04 21:00:47.410:E / AndroidRuntime(854):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.addressbookapp / com.example.addressbookapp.MainActivity}:java.lang.RuntimeException:Your content必须有一个ListView,其id属性为' android.R.id.list'

05-04 21:00:47.410: E/AndroidRuntime(854): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
05-04 21:00:47.410: E/AndroidRuntime(854):  at android.app.ListActivity.onContentChanged(ListActivity.java:243)
05-04 21:00:47.410: E/AndroidRuntime(854):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:293)
05-04 21:00:47.410: E/AndroidRuntime(854):  at android.app.Activity.setContentView(Activity.java:1929)
05-04 21:00:47.410: E/AndroidRuntime(854):  at com.example.addressbookapp.MainActivity.onCreate(MainActivity.java:39)
05-04 21:00:47.410: E/AndroidRuntime(854):  at android.app.Activity.performCreate(Activity.java:5231)
05-04 21:00:47.410: E/AndroidRuntime(854):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-04 21:00:47.410: E/AndroidRuntime(854):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-04 21:00:47.410: E/AndroidRuntime(854):  ... 11 more
05-04 21:00:48.670: E/NetdConnector(385): NDC Command {44 interface fwmark exempt add 74.125.239.4/32} took too long (7648ms)
05-04 21:00:49.970: E/ConnectivityService(385): Exception trying to add a route: java.lang.IllegalStateException: command '44 interface fwmark exempt add 74.125.239.4/32' failed with '400 44 Failed to add exemption rule (File exists)'
05-04 21:00:50.900: E/BluetoothAdapter(822): Bluetooth binder is null
05-04 21:00:51.330: E/BluetoothAdapter(822): Bluetooth binder is null
05-04 21:00:51.370: E/JavaBinder(705): *** Uncaught remote exception!  (Exceptions are not yet supported across processes.)
05-04 21:00:51.370: E/JavaBinder(705): java.lang.RuntimeException: android.os.DeadObjectException
05-04 21:00:51.370: E/JavaBinder(705):  at android.os.Parcel.writeException(Parcel.java:1366)
05-04 21:00:51.370: E/JavaBinder(705):  at android.os.Binder.execTransact(Binder.java:410)
05-04 21:00:51.370: E/JavaBinder(705):  at dalvik.system.NativeStart.run(Native Method)
05-04 21:00:51.370: E/JavaBinder(705): Caused by: android.os.DeadObjectException
05-04 21:00:51.370: E/JavaBinder(705):  at android.os.BinderProxy.transact(Native Method)
05-04 21:00:51.370: E/JavaBinder(705):  at android.content.IIntentReceiver$Stub$Proxy.performReceive(IIntentReceiver.java:124)
05-04 21:00:51.370: E/JavaBinder(705):  at android.app.ActivityThread$ApplicationThread.scheduleRegisteredReceiver(ActivityThread.java:816)
05-04 21:00:51.370: E/JavaBinder(705):  at android.app.ApplicationThreadNative.onTransact(ApplicationThreadNative.java:394)
05-04 21:00:51.370: E/JavaBinder(705):  at android.os.Binder.execTransact(Binder.java:404)
05-04 21:00:51.370: E/JavaBinder(705):  ... 1 more
05-04 21:00:53.580: E/logwrapper(884): executing /system/bin/ip failed: No such file or directory
05-04 21:00:53.590: E/SecondaryTablController(48): ip route del failed: /system/bin/ip route del 10.0.2.2/32 via 0.0.0.0 dev eth0 table 60
05-04 21:00:53.630: E/logwrapper(885): executing /system/bin/ip failed: No such file or directory
05-04 21:00:53.640: E/SecondaryTablController(48): ip route del failed: /system/bin/ip route del 0.0.0.0/0 via 10.0.2.2 dev eth0 table 60
05-04 21:00:54.440: E/NetdConnector(385): NDC Command {51 bandwidth removeiquota eth0} took too long (580ms)
05-04 21:00:54.580: E/NetdConnector(385): NDC Command {52 bandwidth gettetherstats} took too long (538ms)
05-04 21:00:55.450: E/WindowManager(385): Starting window AppWindowToken{b4112da8 token=Token{b407b900 ActivityRecord{b4046fd0 u0 com.example.addressbookapp/.MainActivity t2}}} timed out
05-04 21:00:55.840: E/NetdConnector(385): NDC Command {53 bandwidth setiquota eth0 9223372036854775807} took too long (1396ms)
05-04 21:00:56.550: E/NetdConnector(385): NDC Command {54 bandwidth removeiquota eth0} took too long (665ms)
05-04 21:00:57.910: E/NetdConnector(385): NDC Command {55 bandwidth setiquota eth0 9223372036854775807} took too long (1349ms)
05-04 21:01:52.110: E/BluetoothAdapter(385): Bluetooth binder is null
05-04 21:01:52.110: E/BluetoothAdapter(385): Bluetooth binder is null
05-04 21:01:52.290: E/SoundPool(385): error loading /system/media/audio/ui/Effect_Tick.ogg
05-04 21:01:52.290: E/SoundPool(385): error loading /system/media/audio/ui/Effect_Tick.ogg
05-04 21:01:52.300: E/SoundPool(385): error loading /system/media/audio/ui/Effect_Tick.ogg
05-04 21:01:52.300: E/SoundPool(385): error loading /system/media/audio/ui/Effect_Tick.ogg
05-04 21:01:52.300: E/SoundPool(385): error loading /system/media/audio/ui/Effect_Tick.ogg
05-04 21:01:52.300: E/SoundPool(385): error loading /system/media/audio/ui/KeypressStandard.ogg
05-04 21:01:52.320: E/SoundPool(385): error loading /system/media/audio/ui/KeypressSpacebar.ogg
05-04 21:01:52.330: E/SoundPool(385): error loading /system/media/audio/ui/KeypressDelete.ogg
05-04 21:01:52.330: E/SoundPool(385): error loading /system/media/audio/ui/KeypressReturn.ogg
05-04 21:01:52.330: E/SoundPool(385): error loading /system/media/audio/ui/KeypressInvalid.ogg
05-04 21:02:18.010: E/BluetoothAdapter(822): Bluetooth binder is null
05-04 21:02:19.442: E/BluetoothAdapter(822): Bluetooth binder is null
05-04 21:02:19.482: E/StrictMode(976): A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
05-04 21:02:19.482: E/StrictMode(976): java.lang.Throwable: Explicit termination method 'release' not called
05-04 21:02:19.482: E/StrictMode(976):  at dalvik.system.CloseGuard.open(CloseGuard.java:184)
05-04 21:02:19.482: E/StrictMode(976):  at android.drm.DrmManagerClient.<init>(DrmManagerClient.java:258)
05-04 21:02:19.482: E/StrictMode(976):  at com.google.android.mms.pdu.PduPersister.<init>(PduPersister.java:288)
05-04 21:02:19.482: E/StrictMode(976):  at com.google.android.mms.pdu.PduPersister.getPduPersister(PduPersister.java:296)
05-04 21:02:19.482: E/StrictMode(976):  at com.android.mms.transaction.TransactionService.onNewIntent(TransactionService.java:224)
05-04 21:02:19.482: E/StrictMode(976):  at com.android.mms.transaction.TransactionService$ServiceHandler.handleMessage(TransactionService.java:621)
05-04 21:02:19.482: E/StrictMode(976):  at android.os.Handler.dispatchMessage(Handler.java:102)
05-04 21:02:19.482: E/StrictMode(976):  at android.os.Looper.loop(Looper.java:136)
05-04 21:02:19.482: E/StrictMode(976):  at android.os.HandlerThread.run(HandlerThread.java:61)

这是我的代码: 1)fragment_main.xml

<TableLayout 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"
    tools:context="com.example.addressbookapp.MainActivity$PlaceholderFragment" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:background="#000">

        <TextView
            android:id="@+id/contactsTitleTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/padding_5dp"
            android:text="@string/contacts_title" 
            android:textColor="#FFF"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:layout_weight="1"/>

        <Button
            android:id="@+id/button1"
            android:background="#C4BABA"
            android:onClick="showAddContact"
            android:text="@string/add_button"
            android:textColor="#FFF" 
            android:textSize="20sp"/>

    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:layout_weight="1">
        </ListView>

    </TableRow>
</TableLayout>

contact_entry.xml

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/contactId"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="gone" />

    <TextView
        android:id="@+id/lastName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="@dimen/padding_5dp" 
        android:textColor="#444444"
        android:textSize="20sp"
        android:textStyle="bold"/>

    <TextView
        android:id="@+id/firstName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="@dimen/padding_5dp" 
        android:textColor="#444444"
        android:textSize="20sp"
        android:textStyle="bold"/>

</TableRow>

add_new_contact.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:background="#000">

        <TextView
            android:id="@+id/editContactTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="@dimen/padding_5dp"
            android:text="@string/add_button" 
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#FFF"
            />

    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/fistNameTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:padding="@dimen/padding_5dp"
            android:text="@string/first_name" />

        <EditText
            android:id="@+id/firstName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="textPersonName"
            android:padding="@dimen/padding_5dp" >

            <requestFocus />
        </EditText>

    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/lastNameTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:padding="@dimen/padding_5dp"
            android:text="@string/last_name" />

        <EditText
            android:id="@+id/lastName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="textPersonName"
            android:padding="@dimen/padding_5dp" >

        </EditText>

    </TableRow>

    <TableRow
        android:id="@+id/tableRow4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/phoneNumberTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:padding="@dimen/padding_5dp"
            android:text="@string/phone_number" />

        <EditText
            android:id="@+id/phoneNumber"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="phone"
            android:padding="@dimen/padding_5dp" >

        </EditText>

    </TableRow>

    <TableRow
        android:id="@+id/tableRow5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/emailTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:padding="@dimen/padding_5dp"
            android:text="@string/email_address" />

        <EditText
            android:id="@+id/emailAddress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="textEmailAddress"
            android:padding="@dimen/padding_5dp" >
        </EditText>

    </TableRow>

    <TableRow
        android:id="@+id/tableRow6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/addressTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:padding="@dimen/padding_5dp"
            android:text="@string/home_address" />

        <EditText
            android:id="@+id/homeAddress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="textPostalAddress"
            android:padding="@dimen/padding_5dp" >
        </EditText>

    </TableRow>

    <TableRow
        android:id="@+id/tableRow7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/addButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/save_data" 
            android:onClick="addNewContact"
            android:layout_weight="1"
            />

    </TableRow>

</TableLayout>

MainActivity.java

package com.example.addressbookapp;

import java.util.ArrayList;
import java.util.HashMap;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.os.Build;

public class MainActivity extends ListActivity {

    // The Intent is used to perform an operation
    Intent      intent;
    TextView    contactId;

    DBTools dbTools = new DBTools(this);

    // Method invoked first, when the Activity is first called
    protected void onCreate(Bundle savedInstanceState) {

        //Get saved data, if any 
        super.onCreate(savedInstanceState);
        //Set the main view
        setContentView(R.layout.fragment_main);
        //Get all data from database and store it in an array
        ArrayList<HashMap<String, String>> contactList = dbTools.getAllContacts();

        if (contactList.size() != 0) {
            ListView listView = getListView();

            listView.setOnItemClickListener(new OnItemClickListener(){

                @Override
                public void onItemClick(AdapterView<?> arg0, View view,
                        int arg2, long arg3) {

                    contactId = (TextView) view.findViewById(R.id.contactId);
                    String contactIdValue = contactId.getText().toString();

                    // Intent to get application that owns this Activity
                    Intent theIntent = new Intent(getApplication(), edit_contact.class);
                    // Put additional key value pair to use in Edit Contact
                    theIntent.putExtra("contactId", contactIdValue);
                    startActivity(theIntent);

                }

            });


            // List Adapter is a bridge between ListView and ListView data
                // SimpleAdapter connects data in an Array to XML
                    // SimpleAdapter parameters are: 
                        // 1. Context to provide information needed about the application
                        // 2. The ArrayList of data is next 
                        // 3. Followed by the xml resource
                        // 4. Then we have the names of the data in String format
                        // 5. And their specific resource ids
            ListAdapter adapter = new SimpleAdapter(
                    MainActivity.this, contactList, R.layout.contact_entry, 
                    new String[] {"contactId", "lastName", "firstName"},
                    new int[] {R.id.contactId, R.id.lastName, R.id.firstName});

            // setListAdapter provides cursor for the list view
            // The cursor provides access to the  data
            setListAdapter(adapter);
        }

    }

    // Call the below function showAddContact when New Contact is called
    private void showAddContact(View view) {
        Intent theIntent = new Intent(getApplication(), NewContact.class);
        startActivity(theIntent);
    }
}

DBTools.java

package com.example.addressbookapp;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.*;

public class DBTools extends SQLiteOpenHelper {

    public DBTools(Context applicationContext){
        super(applicationContext, "contactbook.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase database) {

        String dmlSQL = "CREATE TABLE contacts(contactId INTEGER PRIMARY KEY, firstName TEXT, lastName Text," +
                        " phoneNumber TEXT, emailAddress TEXT, homeAddress Text)";
        database.execSQL(dmlSQL);

    }

    @Override
    public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        String dmlSQL = "DROP TABLE IF EXISTS contacts";
        database.execSQL(dmlSQL);
        onCreate(database);
    }

    public void insertContact(HashMap<String, String> queryValues) {
        SQLiteDatabase database = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("firstName", queryValues.get("firstName"));
        values.put("lastName", queryValues.get("lastName"));
        values.put("phoneNumber", queryValues.get("phoneNumber"));
        values.put("emailAddress", queryValues.get("emailAddress"));
        values.put("homeAddress", queryValues.get("homeAddress"));
        database.insert("contacts", null, values);
        database.close();
    }

    public int updateContact(HashMap<String, String> queryValues) {
        SQLiteDatabase database = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("firstName", queryValues.get("firstName"));
        values.put("lastName", queryValues.get("lastName"));
        values.put("phoneNumber", queryValues.get("phoneNumber"));
        values.put("emailAddress", queryValues.get("emailAddress"));
        values.put("homeAddress", queryValues.get("homeAddress"));
        return database.update("contacts", values, "contactId" + "=?", new String[] {queryValues.get("contactId") });
    }

    public void deleteContact(String id) {
        SQLiteDatabase database = this.getWritableDatabase();
        String theSQL = "DELETE FROM contacts WHERE contactId = '" + id + "'";
        database.execSQL(theSQL);
        //database.close();
    }

    public ArrayList<HashMap<String, String>> getAllContacts(){

        ArrayList<HashMap<String, String>> contactArrayList = new ArrayList<HashMap<String, String>>();
        String theSQL = "SELECT * FROM contacts";
        SQLiteDatabase database = this.getWritableDatabase();
        Cursor cursor = database.rawQuery(theSQL, null);
        if (cursor.moveToFirst()){
            do {
                HashMap<String, String> contactMap = new HashMap<String, String>();
                contactMap.put("contactId", cursor.getString(0));
                contactMap.put("firstName", cursor.getString(1));
                contactMap.put("lastName", cursor.getString(2));
                contactMap.put("phoneNumber", cursor.getString(3));
                contactMap.put("emailAddress", cursor.getString(4));
                contactMap.put("homeAddress", cursor.getString(5));
                contactArrayList.add(contactMap);
            } while (cursor.moveToNext());
        }
        return contactArrayList;
    }

    public HashMap<String, String> getContactInfo(String id){

        HashMap<String, String> contactMap = new HashMap<String, String>();
        SQLiteDatabase database = this.getReadableDatabase();
        String theSQL = "SELECT * FROM contacts where contactId = '" + id + "'";
        Cursor cursor = database.rawQuery(theSQL, null);
        if (cursor.moveToFirst()){
            do {
                contactMap.put("contactId", cursor.getString(0));
                contactMap.put("firstName", cursor.getString(1));
                contactMap.put("lastName", cursor.getString(2));
                contactMap.put("phoneNumber", cursor.getString(3));
                contactMap.put("emailAddress", cursor.getString(4));
                contactMap.put("homeAddress", cursor.getString(5));
            } while (cursor.moveToNext());
        }
        return contactMap;
    }
}

0 个答案:

没有答案