Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我能';t从onListItem开始新活动单击ListFragment内部_Java_Android_Android Fragments_Android Activity_Android Listfragment - Fatal编程技术网

Java 我能';t从onListItem开始新活动单击ListFragment内部

Java 我能';t从onListItem开始新活动单击ListFragment内部,java,android,android-fragments,android-activity,android-listfragment,Java,Android,Android Fragments,Android Activity,Android Listfragment,我不确定我做错了什么,到目前为止工作得很好。一旦我想添加新活动,当选择item/note启动新活动时,它将不起作用,也不会给我一个错误 这是ListFragment public class ListFragmentMain extends ListFragment { private ArrayList<Note> notes; private NotesAdapter notesAdapter; @Override public void onActivityCreated

我不确定我做错了什么,到目前为止工作得很好。一旦我想添加新活动,当选择item/note启动新活动时,它将不起作用,也不会给我一个错误

这是ListFragment

public class ListFragmentMain extends ListFragment {

private ArrayList<Note> notes;
private NotesAdapter notesAdapter;


@Override
public void onActivityCreated(Bundle saveInstanceState) {
    super.onActivityCreated(saveInstanceState);
    notes = new ArrayList<>();
    notes.add(new Note("Some Content here","NOTE 1"));
    notes.add(new Note("Some Content here","NOTE 1"));
    notes.add(new Note("Some Content here","NOTE 1"));
    notes.add(new Note("Some Content here","NOTE 1"));
    notes.add(new Note("Some Content here","NOTE 1"));
    notes.add(new Note("Some Content here","NOTE 1"));
    notes.add(new Note("Some Content here","NOTE 1"));
    notes.add(new Note("Some Content here","NOTE 1"));
    notes.add(new Note("Some Content here","NOTE 1"));
    notes.add(new Note("Some Content here","NOTE 1"));

    notesAdapter = new NotesAdapter(getActivity(),notes);
    setListAdapter(notesAdapter);
    getListView().setDivider(ContextCompat.getDrawable(getActivity(),android.R.color.black));
    getListView().setDividerHeight(1);


}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    //position of item passed
    Note note = (Note) getListAdapter().getItem(position);
    Intent intent = new Intent(getActivity(),NoteDetailActivity.class);
    //passing info from the item
    intent.putExtra("Title",note.getTitle());
    intent.putExtra("Message",note.getMessage());
    intent.putExtra("NoteID",note.getNoteID());
    startActivity(intent);

}




}
}

这是我的NoteDetail活动,当我点击这个项目时,它应该开始这个活动

public class NoteDetailActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_note_detail);
}
}
我不知道为什么当我单击一个项目/注释时,没有启动活动?而且它根本没有给出任何错误,而且,这些项目正按照我的要求呈现。

找到解决方案

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    Note note = (Note) getListAdapter().getItem(position);
    Intent intent = new Intent(getActivity(),NoteDetailActivity.class);
    //passing info from the item
    intent.putExtra("Title",note.getTitle());
    intent.putExtra("Message",note.getMessage());
    intent.putExtra("NoteID",note.getNoteID());
    startActivity(intent);

}

你试图实现这一目标的方式已经过时了。你应该继续使用RecyclerView,原因很明显。我有一个我以前开发的应用程序。它创建/编辑注释,您可以按时间顺序查看它们。此外,单击“编辑”按钮可记录编辑活动

在这里:

希望对您有用。:)干杯。

我想知道, 为什么要使用
Note Note=(Note)getListAdapter().getItem(position)尽管在
适配器
类中没有实现
getItem(position)
方法? 相反,请使用此选项,因为我认为您的
注释
将获得
null
-

Note note = notes.get(position);

您是否在AndroidManifest.xmlIntent intent=new intent(getActivity().getApplicationContext(),NoteDetailActivity.class)上声明了此活动;是的,一切都在那里删除'super.onListItemClick(l,v,position,id)`你也可以发布你的日志吗
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    Note note = (Note) getListAdapter().getItem(position);
    Intent intent = new Intent(getActivity(),NoteDetailActivity.class);
    //passing info from the item
    intent.putExtra("Title",note.getTitle());
    intent.putExtra("Message",note.getMessage());
    intent.putExtra("NoteID",note.getNoteID());
    startActivity(intent);

}
Note note = notes.get(position);