邓富贵儿1996 2016-05-14 16:38 采纳率: 0%
浏览 1581

android adapter空指针异常

 public class MainActivity extends AppCompatActivity
{
    DatabaseHelper databaseHelper;
    private static Toolbar mToolbar;
    private static TextView mMaintitle,mMaincontent,mMaintime;
    public static EditText dlx_Input1,dlx_Input2;
    public static String datetime;
    public static View mLine;
    MyAdapter myAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findName();
        setSupportActionBar(mToolbar);
        databaseHelper = new DatabaseHelper(this, databaseHelper.DATABASE_NAME,
                null, databaseHelper.DATABASE_VERSION);
        //myAdapter.getData2();
        myAdapter = new MyAdapter(this);
        myAdapter.lv = (ListView)findViewById(R.id.lv);
        myAdapter.data = myAdapter.getData();
        MyAdapter adapter = new MyAdapter(this);
        myAdapter.lv.setAdapter(adapter);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId())
        {
            case R.id.action_add:
                showDialog_Layout(MainActivity.this);
        }
        return super.onOptionsItemSelected(item);
    }
    public void findName()
    {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mMaintitle = (TextView)findViewById(R.id.dlx_maintitle);
        mMaincontent = (TextView)findViewById(R.id.dlx_maincontent);
        mMaintime = (TextView)findViewById(R.id.dlx_maintime);
        mLine = (View)findViewById(R.id.dlx_line);
    }

    private void showDialog_Layout(Context context) {
        dlx_Input1 = (EditText)findViewById(R.id.dlx_Input1);
        dlx_Input2 = (EditText)findViewById(R.id.dlx_Input2);
        final AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setCancelable(false);
        builder.setTitle(R.string.dlx_add);
        builder.setPositiveButton("CANCEL",
                new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dialog, int whichButton)
                    {
                        setTitle("");
                    }
                });
        builder.setNegativeButton("OK",
                new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dialog, int whichButton)
                    {
                        SQLiteDatabase db = databaseHelper.getWritableDatabase();
                        ContentValues values = new ContentValues();
                        SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                        datetime = s.format(new java.util.Date());
                        values.put("title", String.valueOf(dlx_Input1.getText()));
                        values.put("content", String.valueOf(dlx_Input2.getText()));
                        values.put("time", datetime);
                        db.insert(databaseHelper.TABLE_NAME, null, values);
                        myAdapter.getData();
                    }
                });
        builder.show();
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        menu.findItem(R.id.action_add).setChecked(true);
        return super.onCreateOptionsMenu(menu);
    }

}

public class MyAdapter extends BaseAdapter
{
    public static ListView lv;
    public static List<Map<String, Object>> data;
    public LayoutInflater mInflater = null;
    MainActivity mainActivity;
    DatabaseHelper databaseHelper;
    public MyAdapter(Context context)
    {
        this.mInflater = LayoutInflater.from(context);
    }

    public List<Map<String, Object>> getData()
    {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map;
        for(int i=0;i<10;i++)
        {
            map = new HashMap<String, Object>();
            map.put("title",String.valueOf(mainActivity.dlx_Input1.getText()));
            map.put("content", String.valueOf(mainActivity.dlx_Input2.getText()));
            map.put("time",mainActivity.datetime);
            list.add(map);
        }
        return list;
    }
        static class ViewHolder
    {
        public TextView title;
        public TextView content;
        public TextView time;
        public View line;
    }
    public int getCount() {
        //How many items are in the data set represented by this Adapter.
        //在此适配器中所代表的数据集中的条目数
        return data.size();
    }

    public Object getItem(int position) {
        // Get the data item associated with the specified position in the data set.
        //获取数据集中与指定索引对应的数据项
        return position;
    }

    public long getItemId(int position) {
        //Get the row id associated with the specified position in the list.
        //获取在列表中与指定索引对应的行id
        return position;
    }

    //Get a View that displays the data at the specified position in the data set.
    //获取一个在数据集中指定索引的视图来显示数据
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        //如果缓存convertView为空,则需要创建View
        if(convertView == null)
        {
            holder = new ViewHolder();
            //根据自定义的Item布局加载布局
            convertView = mInflater.inflate(R.layout.commentlayout, null);
            holder.title = (TextView)convertView.findViewById(R.id.dlx_maintitle);
            holder.content = (TextView)convertView.findViewById(R.id.dlx_maincontent);
            holder.time = (TextView)convertView.findViewById(R.id.dlx_maintime);
            holder.line = (View)convertView.findViewById(R.id.dlx_line);
            //将设置好的布局保存到缓存中,并将其设置在Tag里,以便后面方便取出Tag
            convertView.setTag(holder);
        }else
        {
            holder = (ViewHolder)convertView.getTag();
        }
        holder.title.setText((String)data.get(position).get("title"));
        holder.content.setText((String)data.get(position).get("content"));
        holder.time.setText((String)data.get(position).get("time"));
        holder.line.setBackgroundColor(mainActivity.getResources().getColor(R.color.colorPrimaryDark));
        return convertView;
    }
    在map.put("title",String.valueOf(mainActivity.dlx_Input1.getText()));和myAdapter.data = myAdapter.getData();报空指针异常
      Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference,求大神指点!!!
  • 写回答

4条回答 默认 最新

  • devmiao 2016-05-14 23:53
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿