片段不起作用并且有错误

时间:2016-09-28 09:01:47

标签: java android-studio android-fragments fragment android-fragmentactivity

让app从服务器获取图像和文本然后在卡片视图中显示 enter image description here 并有另一个应用程序具有不同的选项卡和哪些选项卡从Fragment扩展 enter image description here

所以想把我的第一个应用程序放在哪个第二个应用程序选项卡中。 像这样 : enter image description here

所以粘贴我的主要活动代码来自片段中的第一个应用程序,但有错误:

public class news extends Fragment {

    private RecyclerView recyclerView;
    private ArrayList<Deatails> data;
    private DataAdapter adapter;


    private View myFragmentView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    myFragmentView= inflater.inflate(R.layout.news, container, false);
      initViews();
    return myFragmentView;
}

    private void initViews() {

        recyclerView = (RecyclerView)myFragmentView.findViewById(R.id.card_recycler_view);
        recyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(layoutManager);
        loadJSON();
    }

    private void loadJSON() {

            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl("http://memaraneha.ir/")
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            RequestInterface request = retrofit.create(RequestInterface.class);
            Call<JSONResponse> call = request.getJSON();

            final ProgressDialog progressDialog = new ProgressDialog(this);
            progressDialog.show();

            call.enqueue(new Callback<JSONResponse>() {
                @Override
                public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {
                    progressDialog.dismiss();

                    JSONResponse jsonResponse = response.body();
                    data = new ArrayList<>(Arrays.asList(jsonResponse.getAndroid()));
                    adapter = new DataAdapter(MainActivity.this, data);
                    recyclerView.setAdapter(adapter);
                }

                @Override
                public void onFailure(Call<JSONResponse> call, Throwable t) {
                    progressDialog.dismiss();
                    Log.d("Error", t.getMessage());
                }
            });
    }
}

这是我的错误:

enter image description here

,,,,

enter image description here

,,,,

enter image description here

3 个答案:

答案 0 :(得分:1)

您应该使用<section class="Sidebar"> <h1 class="HeadOne">Name</h1> <ul> <li> <a href="file:///C:/Users/hen/Desktop/Camp/assignments/Basic-Portfolio/portfolio.html">Portfolio</a> <!--CHANGE THE LINKS TO THE CORRECT WEBPAGES--> </li> <li> <a class="contact" href="file:///C:/Users/hen/Desktop/Camp/assignments/Basic-Portfolio/contact.html">Contact</a> </li> <li> <a class="blue" href="file:///C:/Users/hen/Desktop/Camp/assignments/Basic-Portfolio/index.html">About</a> </li> </ul> </section> <section class="main"> <div class="mainDiv"> <h1 class="mainDivH1">About Me</h1> <img id="Profile" src="assets/images/PortfolioPicture.jpg" alt="Mad Hatter" /> <p>Content.....</p> <p class="p2">Content.....</p> </div> <div class="DivConnect"> <h2 class="ConnectHeader"> Connect with Me </h2> <ol class="ConnectList"> <li class="connectLI"> <a href="https://www.facebook.com/" target="_blank"> <img id="facebook" src="assets/images/1474864409_facebook.png" alt="face" /> </a> </li> <li class="connectLI"> <a href="https://twitter.com/?lang=en" target="_blank"> <img id="twitter" src="assets/images/1474864502_Twitter_icon.png" alt="twitt" /> </a> </li> <li class="connectLI"> <a href="https://www.linkedin.com/" target="_blank"> <img id="linked" src="assets/images/1474864458_linkedin_circle_black.png" alt="linkedin" /> </a> </li> </ol> </div> </section> <footer class=f oot> &copy; Copyright 2016 Anonymous </footer>内的View链接。

内部片段

Fragment

您不能只将活动中的粘贴代码复制到您的片段中。 您需要从片段视图中启动findViewById。

private View myFragmentView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    myFragmentView = inflater.inflate(R.layout.news, container, false);
    return myFragmentView;
}

您无法在片段的myFragmentView.findViewById(R.id.addYourViewHere); 方法之前运行init方法。

答案 1 :(得分:1)

当您使用片段时,您必须覆盖它的onCreateView方法。

示例:

public class FavFragment extends Fragment {

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

    mRecyclerView = (RecyclerView) v.findViewById(R.id.my_recycler_view);
    layoutEmpty = (RelativeLayout) v.findViewById(R.id.layoutEmpty);
    btnShowOnMap = (FloatingActionButton) v.findViewById(R.id.btnShowOnMap);
    fabRoulette = (FloatingActionButton) v.findViewById(R.id.fabRoulette);
    mainView = v.findViewById(R.id.mainView);


    return v;
}

答案 2 :(得分:0)

我解决了我的问题所以它正确的代码为人们所用:

Parent.a_value