0

我正在尝试创建一个 android 应用记事本,但由于某种原因,应用程序在使用 ListView 进入页面时崩溃。我认为问题在于该类在 ListActivity 中扩展。有人可以帮助我吗?

NotesList 类代码:

package com.tim.csproject2;

import android.app.Activity;  
import android.app.Dialog; 
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class NotesList extends ListActivity implements OnClickListener{

    private static final int ACTIVITY_CREATE = 0;
    private static final int ACTIVITY_EDIT = 1;

    private static final int INSERT_ID = Menu.FIRST;
    private static final int DELETE_ID = Menu.FIRST+1;
    private static final int EDIT_ID = Menu.FIRST+2;

    private Button add, back, advanced;

    private ListView listV;

    private NotesDbAdapter myHelper;
    private Cursor myCursor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notes_menu);
        add = (Button) findViewById(R.id.add1);
        back = (Button) findViewById(R.id.back1);
        advanced = (Button) findViewById(R.id.advanced1);
        listV = (ListView) findViewById(R.id.listsList);

        add.setOnClickListener(this);
        back.setOnClickListener(this);
        advanced.setOnClickListener(this);
        listV.getAdapter();

        myHelper = new NotesDbAdapter(this);
        myHelper.open();
        fillData();
        registerForContextMenu(getListView());
    }

    private void fillData() {
         // Get all of the rows from the database and create the item list
        myCursor = myHelper.fetchAllNotes();
        startManagingCursor(myCursor);

        // Create an array to specify the fields we want to display in the list (only TITLE)
        String[] from = new String[]{NotesDbAdapter.KEY_TITLE};

        // and an array of the fields we want to bind those fields to (in this case just text1)
        int[] to = new int[]{R.id.text1};

        // Now create a simple cursor adapter and set it to display
        SimpleCursorAdapter notes = 
            new SimpleCursorAdapter(this, R.layout.notes_row, myCursor, from, to);
        setListAdapter(notes);
    }

    private void createNote() {
        Intent i = new Intent(this, NoteEdit.class);
        startActivityForResult(i, ACTIVITY_CREATE);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

}

note_menu xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:paddingLeft="8dp"
         android:paddingRight="8dp">

         <Button
            android:id="@+id/advanced1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:gravity="right"
            android:text="Advanced Menu"
            android:textSize="20dp"
            />

        <ListView 
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />         
         <TextView 
             android:id="@android:id/empty"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:text="@string/no_notes"
            />  

         <Button
            android:id="@+id/add1"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="Add Note"
            android:textSize="20dp"
            />

         <Button
            android:id="@+id/back1"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="Back"
            android:textSize="20dp"
             />

</LinearLayout>

nores_row xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<TextView android:id= "@+id/text1" xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

安卓清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tim.csproject2"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name="com.tim.csproject2.IntroClass"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity> 
        <activity
            android:name=".MainMenu" 
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="com.tim.csproject2.MAINMENU" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>  
        <activity
            android:name=".NotesList" 
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="com.tim.csproject2.NOTELIST" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ListsList" 
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="com.tim.csproject2.LISTSLIST" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>    
        <activity android:name=".NoteEdit" />
  </application>
</manifest>
4

2 回答 2

0

我能看到的一个问题是

listV = (ListView) findViewById(R.id.listsList);

应该

listV = getListView();

由于您的布局 xml 具有 ListView android:id="@android:id/list",您还可以执行以下操作:

listV = (ListView) findViewById(android.R.id.list);
于 2013-03-01T14:06:50.170 回答
0

对于 SimpleCursorAdapter,minSdkVersion 可能是个问题。代替

  import android.widget.SimpleCursorAdapter;

尝试

  import android.support.v4.widget.SimpleCursorAdapter;
于 2013-03-21T09:39:53.100 回答