2015-11-28 31 views
1

您好,我無法使用片段顯示我的工具欄。我正在將我的應用與Facebook進行整合。我可以連接到Facebook並獲得基本的用戶配置文件詳細信息,例如名字,姓氏和用戶配置文件圖片。現在我將主要活動的信息傳遞給我的第二項活動。我的第二項活動是使用碎片。現在我做了一個包含工具欄,用戶名和圖片的片段。但我無法顯示片段。有人會告訴我我做錯了什麼。謝謝。以下是我的文件。從使用片段的活動中顯示工具欄時出現問題

color.xml:

<resources> 
    <color name="toolBar">#00ff80</color> 
    <color name="colorPrimary">#cc3300</color> 
    <color name="colorPrimaryDark">#C51162</color> 
    <color name="textColorPrimaryDark">#FFFFFF</color> 
    <color name="windowBackground">#FFFFFF</color> 
    <color name="navigationBarColor">#808080</color> 
    <color name="colorAccent">#FF80AB</color> 
    <color name="backgroundColor">#003399</color> 
</resources> 

styles.xml:

<resources> 

    <!-- Base application theme. --> 
    <!-- <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> --> 
     <!-- Customize your theme here. --> 
    <!-- </style> --> 
    <!-- Base Application theme. --> 
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 

    </style> 

</resources> 

activity_second.xml:

<LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:background="@color/backgroundColor" 
     > 
     <android.support.v4.widget.DrawerLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <fragment 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       tools:layout="@layout/first_fragment" 
       android:name="edu.sjsu.cmpe277.termproject.Fragments.FirstFragment" 
       android:id="@+id/toolBar_fragment" 
       /> 

     </android.support.v4.widget.DrawerLayout> 
</LinearLayout> 

first_fragment.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <include 
     android:id="@+id/toolBar" 
     layout="@layout/toolbar" /> 

    <TextView 
     android:id="@+id/textName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/users_FirstName" 
     android:textSize="@dimen/font_size" 
     android:hint="JJJJ" 
     android:layout_gravity="center_horizontal"/> 

    <com.facebook.login.widget.ProfilePictureView 
     android:id="@+id/profilePic" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     > 
    </com.facebook.login.widget.ProfilePictureView> 

</LinearLayout> 

toolbar.xml:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:local="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/toolBar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:minHeight="?attr/actionBarSize" 
    android:background="@color/colorPrimary" 
    android:elevation="5dp" 
    android:gravity="top|start"> 

</android.support.v7.widget.Toolbar> 

FirstFragment.java:

public class FirstFragment extends Fragment implements FragmentCommunicator { 

    private ProfilePictureView profilePictureView; 
    private Toolbar toolbar; 
    private TextView textView; 
    private FragmentCommunicator fragmentCommunicator; 
// private GetDetails getdetails; 
    //private DrawerLayout drawerLayout; 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     // return super.onCreateView(inflater, container, savedInstanceState); 
     View rootView = inflater.inflate(R.layout.event_fragment, container, false); 
     toolbar = (Toolbar)rootView.findViewById(R.id.toolBar); 
     textView = (TextView)rootView.findViewById(R.id.textName); 
     profilePictureView = (ProfilePictureView)rootView.findViewById(R.id.profilePic); 

     return rootView; 
    } 

    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
     try{ 
      fragmentCommunicator =(FragmentCommunicator)context; 
     } 
     catch(ClassCastException ex) { 
      throw new ClassCastException(ex.toString()); 
     } 

    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     toolbar = (Toolbar)getActivity().findViewById(R.id.toolBar); 
     profilePictureView = (ProfilePictureView)getActivity().findViewById(R.id.profilePic); 

    } 

    @Override 
    public void setDetails(String userId, String name) { 

    } 

secondActivity.java:

public class secondActivity extends AppCompatActivity implements FragmentCommunicator{ 

private FirstFragment firstFragment; 
private FragmentTransaction fragmentTransaction; 
private Intent intent; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_second); 

    intent = getIntent(); 
    String firstName = intent.getStringExtra("firstName"); 
    String userId = intent.getStringExtra("Id"); 

    firstFragment = (FirstFragment)getSupportFragmentManager().findFragmentById(R.id.toolBar_fragment); 
    intent = getIntent(); 
    String firstName = intent.getStringExtra("firstName"); 
    String name = intent.getStringExtra("lastName"); 
    String userId = intent.getStringExtra("Id"); 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_second, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 


@Override 
public void setDetails(String userId, String name) { 

    FirstFragment firstFragment = (FirstFragment)getSupportFragmentManager().findFragmentById(R.id.toolBar_fragment); 
      if(firstFragment !=null) { 
       firstFragment.setDetails(userId, name); 
      } 
} 

}

回答

0

這裏有一個你的情侶不錯的點:

  • 不要放入碎片佈局中,從活動中處理工具欄會更好,更聰明。 (您仍然可以使用片段中的選項菜單將組件添加到工具欄中。

  • 您的第二個活動xml禁止使用,將您的佈局置於此xml的頂部,並且容器對其執行片段事務。

你的片段應該是什麼,他們對只取得了處理,你的第二個活動應該更換你的片段(進入容器)和處理工具欄。

好運

+0

我相信在2點你提到的我用我的話做到了activity_second.xml –

相關問題