SimpleCursorAdapter不起作用

时间:2015-07-19 18:37:43

标签: android

我一直关注谷歌开发人员之一tutorials关于创建一个应用程序,将已添加的项目存储到数据库并将其重新发送到listView,但是他们正在使用ListActivity扩展其MainActivity并使用我的BaseActivity扩展MainActivity。我将适配器传递给List时遇到错误。我的simpleCursorAdapter和startManagingCursor也被删除了。

这是我的代码:

import com.dusandimitrijevic.data.GroceryDbAdapter;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.support.v4.widget.SimpleCursorAdapter;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class MainActivity extends BaseActivity {
    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 GroceryDbAdapter mDbHelper;

    Button addItem;
    private Toolbar toolbar;
    private ListView listView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        FrameLayout frameLayout = (FrameLayout)findViewById(R.id.frame_container);

         // inflate the custom activity layout
        LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View activityView = layoutInflater.inflate(R.layout.activity_main, null,false);

        frameLayout.addView(activityView);

        // Setting toolbar
        toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        mDbHelper = new GroceryDbAdapter(this);
        mDbHelper.open();


        // Locate ListView
        listView = (ListView) findViewById(R.id.list);

        fillData();

        addItem = (Button) findViewById(R.id.button1);
        addItem.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, AddActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.slide_in, R.anim.slide_out);

            }
        });

    }

    @SuppressWarnings("deprecation")
    private void fillData() {
        Cursor itemsCursor = mDbHelper.fetchAllItems();
        startManagingCursor(itemsCursor);

        // Create an array to specify the fields we want to display in the list (only TITLE)
        String[] from = new String[]{GroceryDbAdapter.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.title};

        // Now create a simple cursor adapter and set it to display
        SimpleCursorAdapter items = 
            new SimpleCursorAdapter(this, R.layout.list_item_row, itemsCursor, from, to);
        listView.setAdapter(items);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, INSERT_ID, 0, "Add Item");
        return true;
    }

    public boolean onOptionsItemSelected(int featureId, MenuItem item) {
        switch(item.getItemId()) {
            case INSERT_ID:
                createItem();
                return true;
        }

        return super.onMenuItemSelected(featureId, item);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, DELETE_ID, 0, "Delete Item");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch(item.getItemId()) {
            case DELETE_ID:
                AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
                mDbHelper.deleteItem(info.id);
                fillData();
                return true;
        }
        return super.onContextItemSelected(item);
    }

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

    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Intent i = new Intent(this, AddActivity.class);
        i.putExtra(GroceryDbAdapter.KEY_ROWID, id);
        startActivityForResult(i, ACTIVITY_EDIT);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        fillData();
    }
}

这是他们的代码:

/*
 * Copyright (C) 2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.demo.notepad3;

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.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class Notepadv3 extends ListActivity {
    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 NotesDbAdapter mDbHelper;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notes_list);
        mDbHelper = new NotesDbAdapter(this);
        mDbHelper.open();
        fillData();
        registerForContextMenu(getListView());
    }

    private void fillData() {
        Cursor notesCursor = mDbHelper.fetchAllNotes();
        startManagingCursor(notesCursor);

        // 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, notesCursor, from, to);
        setListAdapter(notes);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, INSERT_ID, 0, R.string.menu_insert);
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch(item.getItemId()) {
            case INSERT_ID:
                createNote();
                return true;
        }

        return super.onMenuItemSelected(featureId, item);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, DELETE_ID, 0, R.string.menu_delete);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch(item.getItemId()) {
            case DELETE_ID:
                AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
                mDbHelper.deleteNote(info.id);
                fillData();
                return true;
        }
        return super.onContextItemSelected(item);
    }

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

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Intent i = new Intent(this, NoteEdit.class);
        i.putExtra(NotesDbAdapter.KEY_ROWID, id);
        startActivityForResult(i, ACTIVITY_EDIT);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        fillData();
    }
}

修改

AddActivity.java:

    package com.dusandimitrijevic.grocerylist;


import com.dusandimitrijevic.data.GroceryDbAdapter;

import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class AddActivity extends BaseActivity {

    private Long mRowId;
    private GroceryDbAdapter mDbHelper;
    private EditText title_edit;
    private EditText price_edit;
    private Button saveButton;

    private Toolbar toolbar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        mDbHelper = new GroceryDbAdapter(this);
        mDbHelper.open();
        setContentView(R.layout.add_activity_layout);

        toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);

        // Locate the EditText in add_note.xml
        title_edit = (EditText) findViewById(R.id.titleEdit);
        price_edit = (EditText) findViewById(R.id.priceEdit);

        mRowId = (savedInstanceState == null) ? null :
            (Long) savedInstanceState.getSerializable(GroceryDbAdapter.KEY_ROWID);
        if (mRowId == null) {
            Bundle extras = getIntent().getExtras();
            mRowId = extras != null ? extras.getLong(GroceryDbAdapter.KEY_ROWID)
                                    : null;
        }

        populateFields();

        saveButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                setResult(RESULT_OK);
                finish();
            }

        });

    }

    private void populateFields() {
        if (mRowId != null) {
            Cursor item = mDbHelper.fetchItem(mRowId);
            startManagingCursor(item);
            title_edit.setText(item.getString(
                    item.getColumnIndexOrThrow(GroceryDbAdapter.KEY_TITLE)));
            price_edit.setText(item.getString(
                    item.getColumnIndexOrThrow(GroceryDbAdapter.KEY_PRICE)));
        }

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        saveState();
        outState.putSerializable(GroceryDbAdapter.KEY_ROWID, mRowId);
    }

    @Override
    protected void onPause() {
        super.onPause();
        saveState();
    }

    @Override
    protected void onResume() {
        super.onResume();
        populateFields();
    }

    private void saveState() {
        String title = title_edit.getText().toString();
        String price = price_edit.getText().toString();

        if (mRowId == null) {
            long id = mDbHelper.createItem(title, price);
            if (id > 0) {
                mRowId = id;
            }
        } else {
            mDbHelper.updateItem(mRowId, title, price);
        }
    }

}

错误I' M GETTING:

这是我点击addItem按钮时出现的错误,但是当我点击菜单选项'添加项目'''我在onCreateOption

中声明
    07-20 00:01:45.760: E/AndroidRuntime(27613): FATAL EXCEPTION: main
07-20 00:01:45.760: E/AndroidRuntime(27613): Process: com.dusandimitrijevic.grocerylist, PID: 27613
07-20 00:01:45.760: E/AndroidRuntime(27613): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dusandimitrijevic.grocerylist/com.dusandimitrijevic.grocerylist.AddActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
07-20 00:01:45.760: E/AndroidRuntime(27613):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
07-20 00:01:45.760: E/AndroidRuntime(27613):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
07-20 00:01:45.760: E/AndroidRuntime(27613):    at android.app.ActivityThread.access$900(ActivityThread.java:177)
07-20 00:01:45.760: E/AndroidRuntime(27613):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
07-20 00:01:45.760: E/AndroidRuntime(27613):    at android.os.Handler.dispatchMessage(Handler.java:102)
07-20 00:01:45.760: E/AndroidRuntime(27613):    at android.os.Looper.loop(Looper.java:145)
07-20 00:01:45.760: E/AndroidRuntime(27613):    at android.app.ActivityThread.main(ActivityThread.java:5942)
07-20 00:01:45.760: E/AndroidRuntime(27613):    at java.lang.reflect.Method.invoke(Native Method)
07-20 00:01:45.760: E/AndroidRuntime(27613):    at java.lang.reflect.Method.invoke(Method.java:372)
07-20 00:01:45.760: E/AndroidRuntime(27613):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
07-20 00:01:45.760: E/AndroidRuntime(27613):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
07-20 00:01:45.760: E/AndroidRuntime(27613): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
07-20 00:01:45.760: E/AndroidRuntime(27613):    at com.dusandimitrijevic.grocerylist.AddActivity.onCreate(AddActivity.java:48)
07-20 00:01:45.760: E/AndroidRuntime(27613):    at android.app.Activity.performCreate(Activity.java:6289)
07-20 00:01:45.760: E/AndroidRuntime(27613):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
07-20 00:01:45.760: E/AndroidRuntime(27613):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
07-20 00:01:45.760: E/AndroidRuntime(27613):    ... 10 more

2 个答案:

答案 0 :(得分:1)

您获得NullPointerException的原因是因为您在致电listView.setAdapter(items);之前正在调用listView = (ListView) findViewById(R.id.list);

只需将通话移至fillData()初始化listView下方的 mDbHelper = new GroceryDbAdapter(this); mDbHelper.open(); //fillData(); //don't call it here // Locate ListView listView = (ListView) findViewById(R.id.list); fillData(); //call it here

saveButton

至于下一个NullPointerException,您需要在致电saveButton.setOnClickListener()之前初始化saveButton = (Button) findViewById(R.id.yourButtonIdHere); saveButton.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { setResult(RESULT_OK); finish(); } });

这样的事情:

cart()

答案 1 :(得分:1)

你需要调用方法:

fillData()

分配后

listView =(ListView)findViewById(R.id.list)。