无法使用片段显示活动工具栏

时间:2015-11-28 15:56:41

标签: java android xml android-fragments

您好我在使用片段显示工具栏时遇到问题。我正在将我的应用程序与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);
            }
}

}

1 个答案:

答案 0 :(得分:0)

这里有几个不错的观点:

  • 不要放入内部片段布局,从活动中处理工具栏会更好更聪明。 (您仍然可以使用片段中的选项菜单将组件添加到工具栏中。

  • 在你的第二个活动中,xml阻止使用,将你的布局放在这个xml的顶部,并在容器上做片段事务。

你的片段应该只处理它们所做的事情,你的第二个活动应该替换你的片段(进入容器)并处理工具栏。

祝你好运