应用程序在@Override点上不断崩溃

时间:2013-06-12 04:51:24

标签: java android crash override

我正在制作我的第一个Android应用程序。 我目前正在尝试使用自定义适配器。

这是代码:

CustomAdapter类:

class CustomListAdapter extends BaseAdapter {

Context mContext;
//    List <Note> notesR = new ArrayList<Note>();
ArrayList<Note> notesR;

public CustomListAdapter (List notes) {
    // mContext = context;
//       notesR =  = notes;



    notesR = new ArrayList<Note>(3);
    notesR.add(new Note("Hello", "bye", new Date()));
    notesR.add(new Note("Hello", "bye", new Date()));
    notesR.add(new Note("Hello", "bye", new Date()));
}



@Override
public int getCount() {
    return notesR.size();
}

@Override
public Object getItem(int i) {
    return notesR.get(i);
}

@Override
public long getItemId(int i) {
    return i;

}

// This method is called to draw each row of the list
@Override
public View getView(int index, View convertView, final ViewGroup parent) {

    View vi = convertView;

    if (vi == null) {

        LayoutInflater inflater;
        inflater = LayoutInflater.from(parent.getContext());
        vi = inflater.inflate(R.layout.item_list, parent, false);

    }

    final Note noteModel = notesR.get(index);

    TextView date = (TextView) vi.findViewById(R.id.date);
    date.setText("" + noteModel.getDate());
    TextView title = (TextView) vi.findViewById(R.id.title);
    title.setText(noteModel.getTitle());
    TextView content = (TextView) vi.findViewById(R.id.content);
    content.setText(noteModel.getNote());
    // here you inflate the layout you want for the row
    final View view = View.inflate(mContext, R.layout.item_list, null);



    return view;

列出备注类:

public class ListNotesActivity extends Activity {

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info =     (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
    notes.remove(info.position);
    populateList();
    return true;
}


@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo  menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context_menu, menu);
}




@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_CANCELED){
        return;
    }
    Serializable extra = data.getSerializableExtra("Note");
    if (extra != null){

        Note newNote = (Note)extra;
        if (editingNoteId > -1){

            notes.set(editingNoteId, newNote);
            editingNoteId = -1;
        }
        else {

            notes.add(newNote);
        };
        populateList();
    }

}



private List<Note> notes = new ArrayList<Note>();
private ListView notesListView;
private int editingNoteId = -1;

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_notes);
    notesListView = (ListView)findViewById(R.id.notesListView);

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

            Intent editNoteIntent = new Intent(view.getContext(), EditNotesActivity.class);
            editNoteIntent.putExtra("Note", notes.get(itemNumber));
            editingNoteId = itemNumber;
            startActivityForResult(editNoteIntent, 1);


        }
    });

    registerForContextMenu(notesListView);

    notes.add(new Note("1 Note", "blah blah", new Date()));
    notes.add(new Note("2 Note", "blah blah", new Date()));
    notes.add(new Note("3 Note", "blah blah", new Date()));
    notes.add(new Note("4 Note", "blah blah", new Date()));
    notes.add(new Note("5 Note", "blah blah", new Date()));
    notes.add(new Note("6 Note", "blah blah", new Date()));
    notes.add(new Note("7 Note", "blah blah", new Date()));
    notes.add(new Note("8 Note", "blah blah", new Date()));

    populateList();


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.list_notes, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    //notes.add(new Note("Added note", "blah", new Date()));
    //populateList();

    Intent editNoteIntent = new Intent (this, EditNotesActivity.class);
    startActivityForResult(editNoteIntent, 1);


    return true;


}




// Populate Method

// Populate Method
private void populateList() {

    CustomListAdapter customAdapter = new CustomListAdapter(notes);
     notesListView.setAdapter(customAdapter);
    //CustomAdapter customAdapter = new CustomAdapter();
    //notesListView.setAdapter(customAdapter);

我一直试图让虚拟数据开始使用,但它仍然会崩溃。

我一直试图找到断点的问题来源, 如果我在第一个@Overrride之前放置一个吱吱声点,它会一直运行到那里,但是一旦我让它运行到@Override它就会崩溃。 我尝试删除第一个覆盖,然后代码运行正常,直到下一个覆盖,再次崩溃。

我一直在努力让这一整天工作,我甚至把它发送到我的C#dev的更远的地方,他也不知道。

如果有人能告诉我到底是什么造成的,我将非常感激。

我有一个我下载的例子,它使用非常相似的代码来运行,它运行正常。这是示例中的代码:

public class CustomAdapter extends BaseAdapter {

private static final String TAG = CustomAdapter.class.getSimpleName();
ArrayList<DataModel> listArray;

public CustomAdapter() {
listArray = new ArrayList<DataModel>(5);
listArray.add(new DataModel("Title1", "Java", new Date()));
listArray.add(new DataModel("name2",  "Python", new Date()));
listArray.add(new DataModel("name3",  "Django", new Date()));
listArray.add(new DataModel("name4",  "Groovy", new Date()));
listArray.add(new DataModel("name5", "Maven", new Date()));
}

@Override
public int getCount() {
return listArray.size();    // total number of elements in the list

}

@Override
public Object getItem(int i) {
return listArray.get(i);    // single item in the list
}

@Override
public long getItemId(int i) {
return i;                   // index number
}

@Override
public View getView(int index, View view, final ViewGroup parent) {

if (view == null) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    view = inflater.inflate(R.layout.single_list_item, parent, false);
}

final DataModel dataModel = listArray.get(index);

TextView title = (TextView) view.findViewById(R.id.title);
title.setText(dataModel.getTitle());

TextView content = (TextView) view.findViewById(R.id.content);
content.setText(dataModel.getContent());

TextView date = (TextView) view.findViewById(R.id.date);
date.setText("" + dataModel.getDate());


return view;

编辑1:

  

LogCat(我只为我的应用程序过滤了它):

     

06-12 15:26:46.359 8851-8851 / com.fishingfon.notetakerui
  D / dalvikvm:延迟启用CheckJNI 06-12 15:26:46.399
  8851-8851 / com.fishingfon.notetakerui W / ActivityThread:Application   com.fishingfon.notetakerui正在等待端口8100上的调试器...   06-12 15:26:46.409 8851-8851 / com.fishingfon.notetakerui
  I / System.out:发送WAIT块06-12 15:26:46.419
  8851-8856 / com.fishingfon.notetakerui I / dalvikvm:调试器是   活跃06-12 15:26:46.614 8851-8851 / com.fishingfon.notetakerui
  I / System.out:调试器连接06-12 15:26:46.614
  8851-8851 / com.fishingfon.notetakerui I / System.out:等待   调试器解决... 06-12 15:26:46.814
  8851-8851 / com.fishingfon.notetakerui I / System.out:等待   调试器解决... 06-12 15:26:47.014
  8851-8851 / com.fishingfon.notetakerui I / System.out:等待   调试器解决... 06-12 15:26:47.214
  8851-8851 / com.fishingfon.notetakerui I / System.out:等待   调试员解决... 06-12 15:26:47.289
  2137-2137 / com.google.android.youtube D / YouTube MDX:已收到   intent android.intent.action.SCREEN_OFF 06-12 15:26:47.414
  8851-8851 / com.fishingfon.notetakerui I / System.out:等待   调试员解决... 06-12 15:26:47.614
  8851-8851 / com.fishingfon.notetakerui I / System.out:等待   调试器解决... 06-12 15:26:47.814
  8851-8851 / com.fishingfon.notetakerui I / System.out:调试器有   定居(1315)06-12 15:26:48.149
  8851-8851 / com.fishingfon.notetakerui D / AndroidRuntime:正在关闭   VM 06-12 15:26:48.149 8851-8851 / com.fishingfon.notetakerui
  W / dalvikvm:threadid = 1:线程退出未捕获的异常   (group = 0x40f4d930)06-12 15:26:48.169
  8851-8851 / com.fishingfon.notetakerui E / AndroidRuntime:致命   例外:主要           java.lang.RuntimeException:无法启动活动ComponentInfo {com.fishingfon.notetakerui / com.fishingfon.notetakerui.ListNotesActivity}:   显示java.lang.NullPointerException           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2357)           在android.app.ActivityThread.access $ 600(ActivityThread.java:153)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1247)           在android.os.Handler.dispatchMessage(Handler.java:99)           在android.os.Looper.loop(Looper.java:137)           在android.app.ActivityThread.main(ActivityThread.java:5231)           at java.lang.reflect.Method.invokeNative(Native Method)           在java.lang.reflect.Method.invoke(Method.java:511)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:795)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)           at dalvik.system.NativeStart.main(Native Method)           引起:java.lang.NullPointerException           at com.fishingfon.notetakerui.ListNotesActivity.populateList(ListNotesActivity.java:145)           at com.fishingfon.notetakerui.ListNotesActivity.onCreate(ListNotesActivity.java:110)           在android.app.Activity.performCreate(Activity.java:5110)           在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)           ... 11更多06-12 15:26:48.209 8851-8851 / com.fishingfon.notetakerui I /处理:发送信号。   PID:8851 SIG:9

编辑2:

新适配器代码:

class CustomListAdapter extends BaseAdapter {

Context mContext;
//    List <Note> notesR = new ArrayList<Note>();
ArrayList<Note> notesR;

public CustomListAdapter (Context context, Object notes) {
     mContext = context;
//       notesR =  = notes;



    notesR = new ArrayList<Note>(3);
    notesR.add(new Note("Hello", "bye", new Date()));
    notesR.add(new Note("Hello", "bye", new Date()));
    notesR.add(new Note("Hello", "bye", new Date()));
}



@Override
public int getCount() {
    return notesR.size();
}

@Override
public Object getItem(int position) {
    return notesR.get(position);
}

@Override
public long getItemId(int position) {
    return position;

}

// This method is called to draw each row of the list
@Override
public View getView(int position, View convertView, final ViewGroup parent) {

    View vi = convertView;

    if (vi == null) {

        LayoutInflater inflater;
       // inflater = LayoutInflater.from(parent.getContext());
        vi = View.inflate(mContext, R.layout.item_list, null);

    }

    final Note noteModel = notesR.get(position);

    TextView date = (TextView) vi.findViewById(R.id.date);
    date.setText("" + noteModel.getDate());
    TextView title = (TextView) vi.findViewById(R.id.title);
    title.setText(noteModel.getTitle());
    TextView content = (TextView) vi.findViewById(R.id.content);
    content.setText(noteModel.getNote());


    return vi;

提前致谢 干杯 科里

1 个答案:

答案 0 :(得分:0)

此处不需要此代码

final View view = View.inflate(mContext, R.layout.item_list, null);
return view;

只需添加此项而不是上面的

return vi;

在CustomListAdapter的getView()

修改

public CustomListAdapter (Context context, int id, ArrayList<Note> notes) {
   super(context,id,notes);
   mContext = context;
   notesR =  = notes;
}

在活动中

CustomListAdapter customAdapter = new CustomListAdapter(this, R.layout.listitem, notes);
notesListView.setAdapter(customAdapter);