2016-08-29 129 views
0

我試圖創建一個應用程序,只要你點擊導航抽屜中的選項就會顯示一個片段。然而,通過使用BlankFragment fragment = new BlankFragment();創建BlankFragment(的時候,我得到一個錯誤:BlankFragment變成紅色和Android Studio不承認它爲什麼BlankFragment不能正常工作?

MainActivity.java

... 

    } else if (id == R.id.nav_shooks) { 

     FragmentManager fragmentManager = getFragmentManager(); 

     FragmentTransaction transaction = fragmentManager.beginTransaction(); 

     BlankFragment fragment = new BlankFragment(); 
     transaction.add(R.id.shooksLayout, fragment); 

     transaction.commit(); 

    } else if (id == R.id.nav_prizes) { 

... 

ShooksFragment

package com.shook.android.shook; 

import android.content.Context; 
import android.net.Uri; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 


/** 
* A simple {@link Fragment} subclass. 
* Activities that contain this fragment must implement the 
* {@link ShooksFragment.OnFragmentInteractionListener} interface 
* to handle interaction events. 
*/ 
public class ShooksFragment extends Fragment { 

    private OnFragmentInteractionListener mListener; 

    public ShooksFragment() { 
     // Required empty public constructor 
    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.fragment_shooks, container, false); 
    } 

    // TODO: Rename method, update argument and hook method into UI event 
    public void onButtonPressed(Uri uri) { 
     if (mListener != null) { 
      mListener.onFragmentInteraction(uri); 
     } 
    } 

    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
     if (context instanceof OnFragmentInteractionListener) { 
      mListener = (OnFragmentInteractionListener) context; 
     } else { 
      throw new RuntimeException(context.toString() 
        + " must implement OnFragmentInteractionListener"); 
     } 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
     mListener = null; 
    } 

    /** 
    * This interface must be implemented by activities that contain this 
    * fragment to allow an interaction in this fragment to be communicated 
    * to the activity and potentially other fragments contained in that 
    * activity. 
    * <p/> 
    * See the Android Training lesson <a href= 
    * "http://developer.android.com/training/basics/fragments/communicating.html" 
    * >Communicating with Other Fragments</a> for more information. 
    */ 
    public interface OnFragmentInteractionListener { 
     // TODO: Update argument type and name 
     void onFragmentInteraction(Uri uri); 
    } 
} 

Here is the error. As you can see, the word BlankFragment is in red

+0

你在哪裏定義了你的BlankFragment? –

+0

在'onNavigationItemSelected(MenuItem項目)'if'上'@rakeshkashyap –

+0

'shooksLayout'是什麼樣的佈局?它是一個框架佈局嗎? – RamithDR

回答

1

您錯過了ShooksFragment中的newInstance()方法,

添加此方法並請回報。

// TODO: Rename and change types and number of parameters 
public static ShooksFragment newInstance() { 

    ShooksFragment fragment = new ShooksFragment(); 

    return fragment; 
} 

編輯:

既然要加載ShooksFragment裏面MainActivity,你可以簡單地使ShooksFragment的對象,並使用分段管理加載該片段。

... 

    } else if (id == R.id.nav_shooks) { 

     FragmentManager fragmentManager = getFragmentManager(); 

     FragmentTransaction transaction = fragmentManager.beginTransaction(); 

     ShooksFragment fragment = new ShooksFragment(); 
     transaction.add(R.id.shooksLayout, fragment); 

     transaction.commit(); 

    } else if (id == R.id.nav_prizes) { 

... 
+0

我仍然有與BlankFragment的錯誤 –

+0

發佈錯誤消息? – RamithDR

+0

沒有錯誤信息,BlankFragment這個詞只是紅色 –