无法将片段显示到活动中

时间:2015-11-28 22:59:56

标签: java android xml android-fragments

您好我正在尝试将我的片段加载到一个活动中,但无法加载它。有人可以告诉我我做错了什么吗?我的工具栏位于活动中,但我的片段包含来自Facebook的textview和profilepictureview。我错过了什么,因为我无法显示textview和profilepicture视图?

下面是我的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"
    android:id="@+id/fragmentPic">

    <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>

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"
         >
    <include android:id="@+id/toolBar"
        layout="@layout/toolbar" />

      <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/fragment1"
              />

      </android.support.v4.widget.DrawerLayout>
</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 {

    private ProfilePictureView profilePictureView;
    private TextView textView;

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

        textView = (TextView)rootView.findViewById(R.id.textName);
        profilePictureView = (ProfilePictureView)rootView.findViewById(R.id.profilePic);

        return rootView;
    }

secondActivity.java:

     public class secondActivity extends AppCompatActivity {

      private Intent intent;
      private Toolbar toolbar;
     @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");

        toolbar = (Toolbar)findViewById(R.id.toolBar);
        setSupportActionBar(toolbar);
        toolbar.setLogo(R.drawable.ic_menu_image);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
    }

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

1 个答案:

答案 0 :(得分:0)

我发现了我的问题,我正在调用错误的布局文件。

View rootView = inflater.inflate(R.layout.event_fragment, container, false);

应该是:

View rootView = inflater.inflate(R.layout.first_fragment, container, false);