Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用片段显示活动中的工具栏时出现问题_Java_Android_Xml_Android Fragments - Fatal编程技术网

Java 使用片段显示活动中的工具栏时出现问题

Java 使用片段显示活动中的工具栏时出现问题,java,android,xml,android-fragments,Java,Android,Xml,Android Fragments,您好,我在使用fragment显示工具栏时遇到问题。我正在将我的应用程序与facebook集成。我能够连接到facebook,并获得基本的用户资料详细信息,如名字、姓氏和用户资料图片。现在,我将信息从主要活动传递到第二个活动。我的第二个活动是使用片段。现在我制作了一个包含工具栏、用户名和图片的片段。但是我在显示片段时遇到了问题。有人能告诉我我做错了什么吗。非常感谢。下面是我的档案 color.xml: <resources> <color name="toolBar"&

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

}这里有几点很好的建议:

  • 不要将片段放在布局中,从活动中处理工具栏会更好、更智能。(您仍然可以使用片段中的“选项”菜单将组件添加到工具栏

  • 在第二个活动中,xml阻止使用,将布局放在该xml的顶部,并使用容器在其上执行片段事务

您的片段应该只处理它们的用途,您的第二个活动应该替换您的片段(放入容器)并处理工具栏


祝你好运

这里有几点很好的建议:

  • 不要将片段放在布局中,从活动中处理工具栏会更好、更智能。(您仍然可以使用片段中的选项菜单向工具栏添加组件。)

  • 在第二个活动中,xml阻止使用,将布局放在该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>
<?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>
<?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>
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) {

    }
    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);
            }
}