我正在通过修改示例来学习Android。我目前正在研究this例子。我的代码的不同之处在于我想在 fragment 中显示 listView。
代码(错误显示在注释中):
public class MovieListFragment extends Fragment {
private static final String TAG = MovieListFragment.class.getSimpleName();
private static final String url = "http://example/json/movies.json";
private ProgressDialog pDialog;
private List<Movie> movieList = new ArrayList<Movie>();
private ListView listView;
private CustomListAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_movies, container, false);
listView = (ListView) findViewById(R.id.list);
//cannot resolve method findViewById(?) and cannot resolve symbol list
adapter = new CustomListAdapter(this, movieList);
listView.setAdapter(adapter);
pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...");
pDialog.show();
getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#1b1b1b")));
//cannot resolve getActionBar
...
@Override
//method does not override method from its superclass
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
//cannot resolve method getMenuInflater() and cannot resolve symbol menu
return true;
}
这里我有以下错误:
- 无法解析方法findViewById(?)
- 无法解析符号列表
- 无法解析getActionBar
- 方法不会覆盖其父类(super class)中的方法
- 无法解析方法getMenuInflater()
- 无法解析符号菜单
- customlistadapter 中的 customlistadapter(android.app.activity 列表)无法应用于 (android.content.context)
- in pDialog = new ProgressDialog(this); ProgressDialog 中的 ProgressDialog(android.app.activity list) 无法应用于 (com.kemo.editedtutorial.sliderfragments.MoviesListFragment)
最佳答案
<强>1。无法解析方法findViewById(?)
您已在 return
语句之后的 onCreateView()
中编写了此代码。
首先膨胀
您的 View
,对其进行必要的操作,然后返回该 View
还可以使用view.findViewById()
<强>2。无法解析符号列表
3.无法解析符号菜单
这里,由于 drawable
或 layout
中的一些错误,您的 R.java
尚未生成。
<强>4。无法解析 getActionBar
您位于 Fragment
中,并且 getActionBar
是来自 Activity
的方法,因此您需要调用` getActivity().getActionBar()
<强>5。无法解析方法 getMenuInflater()
6.方法不会覆盖其父类(super class)中的方法
您位于 Fragment
中,并且 getMenuInflater()
是来自 Activity
的方法,因此您需要调用 getActivity().getMenuInflater( )
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
getActivity().getMenuInflater()....
}
关于java - 在 Android 中向 Fragment 添加列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39224900/