0

我想将列表视图中选定的菜单名称传递给另一个意图类,以便新的子菜单被其父菜单识别。所以我使用 getExtras 在 KatagoriPengeluaran.java 中获取菜单名称,如下所示

 public void displayListView() {



listView.setOnItemClickListener(new OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> listView, View view, 
     int position, long id) {

       Cursor cursor = (Cursor) listView.getItemAtPosition(position);

       String countryCode = 
        cursor.getString(cursor.getColumnIndexOrThrow("katagori_label"));   
    Intent ourIntent = new Intent(KatagoriPengeluaran.this, Pengeluaran.class);
        ourIntent.putExtra("cek", countryCode);

       startActivity(ourIntent);
   }
  });

在 Pengeluaran.java 的新类中

    @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.pengeluaran);

  dbHelper = new Database(this);
  dbHelper.open();

    Bundle extras = this.getIntent().getExtras();
    String sub = extras.getString("cek");

// dbHelper.deleteAllData();
 //dbHelper.insertSomeData();

  //Generate ListView from SQLite Database
  displayListView(sub);

 }


         private void displayListView(final String su) {


  cursor = dbHelper.fetchPengeluaran(su);

  // The desired columns to be bound
  allAdapter = new PengeluaranCursorAdapter(this, cursor);

  ListView listView = (ListView) findViewById(android.R.id.list);
  // Assign adapter to ListView
  listView.setAdapter(allAdapter);

这是我的数据库和 fetch 函数

    public class Database {
// Katagori Pengeluaran
    public static final String RIK = "_id";
    public static final String RLK = "katagori_label";
    public static final String RJK = "katagori_jumlah";

    //Pengeluaran

    public static final String KEY_RPI = "_id";
     public static final String KEY_RLP = "pengeluaran_label";
     public static final String KEY_RSK = "pengeluaran_sub";
     public static final String KEY_RNP = "nominal";
     public static final String KEY_RTP = "tanggal";



        public Cursor fetchPengeluaran(String su) {

  Cursor mCursor = mDb.query(PENGELUARAN_TABLE, new String[] {KEY_RPI,
    KEY_RLP, KEY_RSK, KEY_RNP, KEY_RTP}, "KEY_RLP like " + su
  ,null, null, null, null);

  if (mCursor != null) {
   mCursor.moveToFirst();
  }
  return mCursor;
 }

但是在执行它时说 NullPointerException, Unable to Inisiate Activity Component in log cat。请帮忙。

4

2 回答 2

0

使用此方法getExtra

sub = (String) getIntent().getSerializableExtra("cek");

编辑

只有你Log.i("Activity", countryCode );

让你的countrycode不为空

于 2013-06-02T20:43:32.830 回答
0

为什么从 listView 中返回光标?在我看来,这不是一个好主意。您应该将演示文稿和数据分开。

您可以尝试在第一堂课中使用以下内容。使用putString()代替putExtra()

ourIntent.putString("cek", countryCode);

列表视图和适配器

除此之外,我找不到任何我会做的不同的事情。但就像我一开始所说的那样,回顾cursor一下你的演示文稿中有一个对象(listView)。取而代之的是在一个ArrayAdapter例子中使用 String 对象,这样您就可以String直接获取 countryCode 。

有关 listViews 的更多信息:http ://www.vogella.com/articles/AndroidListView/article.html

调试

无论如何,我会在调试模式下仔细检查从你返回的对象Cursor是你所期望的,最重要的是不是null

于 2013-06-02T20:45:22.930 回答