在Android中向片段添加列表

时间:2016-08-30 10:23:55

标签: java android listview android-studio android-fragments

我通过修改示例来学习Android。我目前正在研究this示例。我的代码有什么不同之处在于我想在片段中显示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;
}

这里我有以下错误:

  1. 无法解析方法 findViewById(?)
  2. 无法解析符号 列表
  3. 无法解析 getActionBar
  4. 方法不会覆盖其超类
  5. 中的方法
  6. 无法解析方法 getMenuInflater()
  7. 无法解析符号 菜单
  8. customlistadapter中的
  9. customlistadapter(android.app.activity list)无法应用于(android.content.context)
  10. in pDialog = new ProgressDialog(this); ProgressDialog中的ProgressDialog(android.app.activity列表)无法应用于(com.kemo.editedtutorial.sliderfragments.MoviesListFragment)

6 个答案:

答案 0 :(得分:4)

<强> 1。无法解析方法findViewById(?)

您已在onCreateView()声明之后在return中编写此代码。

首先Inflate您的view,对此进行必要的操作,然后返回view

也使用view.findViewById()

<强> 2。无法解析符号列表
第3。无法解析符号菜单

此处R.java由于drawable或您的layout中的某些错误而未生成Fragment

<强> 4。无法解析getActionBar

你在getActionBarActivity是来自Fragment的方法,所以你需要调用`getActivity()。getActionBar()

<强> 5。无法解析方法getMenuInflater()
6。 method不会覆盖其超类

中的方法

您位于getMenuInflater()Activity是来自getActivity().getMenuInflater()的方法,因此您需要致电@Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); getActivity().getMenuInflater().... }

<div>

答案 1 :(得分:3)

首先来自您的 oncreateview(),您将返回您的视图,而不是使用

View view=inflater.inflate(R.layout.fragment_movies, container, false);

然后使用此视图将布局元素用作view.findViewById()而不是findViewById(),然后最后返回此view

关于错误无法解析符号列表
确保您的布局 fragment_movies 中定义了一个列表视图,其ID为android:id="@+id/list"

getActionBar 您将在具有此片段的活动中执行的操作。 如果您使用的是支持库,则可以将操作栏更改为

((YourParentActivity which has this fragment)(getActivity())).getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#1b1b1b")));  
e.g ((MainActivity)(getActivity())).getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#1b1b1b")));

您可以执行以下操作以避免getsupportActionbar附近的检查错误:

 ActionBar actionBar = ((MainActivity)getActivity()).getSupportActionBar();
        if(actionBar!=null) {
            actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#1b1b1b")));
        }

如果您希望使用片段中的选项菜单,则在片段的onCreate()中添加setHasOptionMenu(true)行。这将访问您的活动选项菜单 然后使用您必须编写的选项菜单

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
    }

菜单将是您的Android活动,使用该菜单执行您的操作。 获取选定的选项菜单

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }

关于错误: customlistadapter中的 customlistadapter(android.app.activity list)无法应用于(android.content.context) 在pDialog = new ProgressDialog(this); ProgressDialog中的ProgressDialog(android.app.activity列表)无法应用于(com.kemo.editedtutorial.sliderfragments.MoviesListFragment)

不是传递这个传递getActivity(),因为它们都期望context作为活动的参数值,但是你从Fragment传递它。因此,您可以通过getActivity()getActivity().getApplicationContext()来解决此错误 希望这会对你有所帮助

答案 2 :(得分:2)

试试这个

@Override
public View onCreateView(LayoutInflater inflater,
                        ViewGroup container,
                        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_movies, container, false);

    listView = (ListView) view .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();

    getActivity().getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#1b1b1b")));
    //cannot resolve getActionBar
return view;
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_sample, menu);
    super.onCreateOptionsMenu(menu,inflater);
}

答案 3 :(得分:1)

查看视图= inflater.inflate(R.layout.fragment_movies,container,false);

使用:

(ListView)view.findViewById(R.id.list); 等

答案 4 :(得分:1)

问题是,您执行命令inflater.inflate(R.layout.fragment_movies, container, false),立即返回结果,然后尝试查找适当的视图。

查看,这是膨胀的结果,包含其所有子项,因此必须使用相同的视图以便稍后查找它们。而不仅仅是findViewById你应该使用

View view = inflater.inflate(R.layout.fragment_movies, container, false);
listview = (ListView) view.findViewById(R.id.list);

return view;

答案 5 :(得分:1)

你应该在onCreateView()方法的末尾调用return方法,因为如果你在onCreateView()的下面调用return方法,那么View类无法找到任何id。所以,你可以这样使用: - < / p>

@Override
public View onCreateView(LayoutInflater inflater,
                        ViewGroup container,
                        Bundle savedInstanceState) {
    View  v = 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")));

return v;
}