listview中的空指针

时间:2015-03-21 19:07:26

标签: java android listview

我有一个listview,它将从数据库中检索详细信息和日期。现在我要在listview中添加一个文本字段。文本字段供我比较数据库中的日期并计算剩余日期。首先,我尝试获取当前日期以测试是否可以在列表视图或nt中显示日期,但我无法显示它。

           package my.com.chyi.schedulev2;

         import android.app.Activity;
         import android.app.Activity;
            import android.content.Intent;
          import android.database.Cursor;
         import android.os.Bundle;
         import android.view.View;
          import android.view.View.OnClickListener;
           import android.widget.AdapterView;
              import android.widget.AdapterView.OnItemClickListener;
             import android.widget.Button;
           import android.widget.ListView;
           import android.widget.SimpleCursorAdapter;
        import android.widget.TextView;

        import java.text.SimpleDateFormat;
          import java.util.Calendar;

          import my.com.chyi.schedulev2.SQLController;

          /**
        * Created by chyi1_000 on 3/19/2015.
          */
   public class assigmentActivity extends Activity {

Button addass_bt;
ListView lv;
SQLController dbcon;
TextView assID_tv,assName_tv,assAtime_tv,assRemain;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_assign);

    dbcon = new SQLController(this);
    dbcon.open();
    //assign module
    addass_bt = (Button)findViewById(R.id.addAss_bt_id);
    lv =(ListView)findViewById(R.id.assList_id);

    //assig module
    addass_bt.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent add_ass = new Intent(assigmentActivity.this,Add_Assignment.class);
            startActivity(add_ass);
        }
    });

    //assig module
    Cursor cursor = dbcon.readDataA();
    //calculate days different
  // list -> loop
    // each row different days


    String[] fromA = new String[] { DBhelper.ASSIGNMENT_ID,DBhelper.ASSIGNMENT_NAME,DBhelper.ASSIGNMENT_mTime, };
    int[] toA = new int[] { R.id.assigment_id, R.id.assigment_name, R.id.assigment_ATime};

    SimpleCursorAdapter adapterA = new SimpleCursorAdapter(
           assigmentActivity.this, R.layout.view_assignment_entry, cursor, fromA, toA);
    adapterA.notifyDataSetChanged();
    lv.setAdapter(adapterA);

    // assig module
    lv.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {

            assID_tv = (TextView) view.findViewById(R.id.assigment_id);
            assName_tv = (TextView) view.findViewById(R.id.assigment_name);
            assAtime_tv = (TextView)view.findViewById(R.id.assigment_ATime);


            Calendar d = Calendar.getInstance();
            SimpleDateFormat dfd = new SimpleDateFormat("yyyy-MM-dd");
            String formattedDateD = dfd.format(d.getTime().toString());
            String test = formattedDateD;
            String assID_val = assID_tv.getText().toString();
            String assName_val = assName_tv.getText().toString();
            String assATime_val =  assAtime_tv .getText().toString();

            Intent modify_intent = new Intent(getApplicationContext(),
                    Modify_assignment.class);
            modify_intent.putExtra("assName", assName_val);
            modify_intent.putExtra("assID", assID_val);
            modify_intent.putExtra("assAtime",assATime_val);
            startActivity(modify_intent);
        }
    });

  TextView babu = (TextView) findViewById(R.id.assigment_remain);
   Calendar d = Calendar.getInstance();
   SimpleDateFormat dfd = new SimpleDateFormat("yyyy-MM-dd");
    String formattedDateD = dfd.format(d.getTime().toString());
    babu.setText(formattedDateD);
 }

}

我在bubu.setText(formattedDateD)的java.lang.NullPointerException收到错误; 我可以知道问题是什么吗?谢谢你的帮助。我的listview将自动从我的SQLController获取所有数据,因此我无法在列表视图中添加额外的文本字段。

      public Cursor readDataA(){
         String[] allcolumnsA = new String[] {                                  DBhelper.ASSIGNMENT_ID,DBhelper.ASSIGNMENT_NAME,DBhelper.ASSIGNMENT_mTime};
    Cursor e = database.query(DBhelper.TABLE_ASSIGNMENT, allcolumnsA,null, null, null, null, null);
    if (e != null ) {
        e.moveToFirst();

    }
    return e;
}

4 个答案:

答案 0 :(得分:0)

尝试使用&#34; yyyy-MM-dd&#34;作为模式。

答案 1 :(得分:0)

添加一个&#39; M&#39;到你的日期格式

答案 2 :(得分:0)

String formattedDateD = dfd.format(d.getTime()).toString();

答案 3 :(得分:0)

  

我在bubu.setText(formattedDateD)的java.lang.NullPointerException收到错误;

这表示bubu(或代码中的babu)为空。

我会看看这一行:

TextView babu = (TextView) findViewById(R.id.assigment_remain);

这可能找不到视图,因此返回null。

您的代码需要处理这种情况,您还需要跟踪视图中ID错误的原因。