来自android的通胀异常错误

时间:2015-11-29 10:55:37

标签: java android

你好,我在几个小时的过程中一直在努力解决这些错误,但无济于事。我试图将数据从我的活动传递到片段,但没有成功。我猜它在<fragment>文件中抱怨activity_second.xml。我的猜测可能是错的。有没有人遇到过这个错误?但我不知道如何解决这个错误。有些人请指导我做错了什么。以下是我的代码:

first_fragment.xml:

<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 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: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>

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.first_fragment, container, false);
    Bundle bundle = new Bundle();
    bundle = this.getArguments();
    String profileId = bundle.getString("profileId"); //complains here and returns null
    TextView textView = (TextView)rootView.findViewById(R.id.textName);

    textView.setText(bundle.getString("names"));

    return rootView;
 }
}

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"
          android:id="@+id/drawer">

          <fragment
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              tools:layout="@layout/first_fragment"
              class="edu.sjsu.cmpe277.termproject.Fragments.FirstFragment"
              android:id="@+id/fragment1"
              />
      </android.support.v4.widget.DrawerLayout>
</LinearLayout>

secondActivity.java:

public class secondActivity extends AppCompatActivity {
    private Intent intent;
    private Toolbar toolbar;
    private String firstName, profileId;
    private FirstFragment firstFragment;

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

        intent = getIntent();
        firstName = intent.getStringExtra("firstName");
        profileId = intent.getStringExtra("Id");

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

        firstFragment = new FirstFragment();
        Bundle bundle = new Bundle();
        bundle.putString("names", firstName);
        bundle.putString("profileId", profileId);
        firstFragment.setArguments(bundle);

        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.drawer, firstFragment,"newFrag").commit();
      }

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

        switch(item.getItemId()) {
            case android.R.id.home:
                Intent homeIntent = new Intent(this, secondActivity.class );
                homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(homeIntent);
        }
        int id = item.getItemId();

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

以下错误:

29 01:50:11.402  13536-13536/edu.sjsu.cmpe277.termproject E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: edu.sjsu.cmpe277.termproject, PID: 13536
    java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.sjsu.cmpe277.termproject/edu.sjsu.cmpe277.termproject.secondActivity}: android.view.InflateException: Binary XML file line #82: Error inflating class fragment
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
            at android.app.ActivityThread.access$900(ActivityThread.java:177)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5942)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
     Caused by: android.view.InflateException: Binary XML file line #82: Error inflating class fragment
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:770)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:821)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:255)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
            at edu.sjsu.cmpe277.termproject.secondActivity.onCreate(secondActivity.java:43)
            at android.app.Activity.performCreate(Activity.java:6289)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
            at android.app.ActivityThread.access$900(ActivityThread.java:177)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5942)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
            at edu.sjsu.cmpe277.termproject.Fragments.FirstFragment.onCreateView(FirstFragment.java:44)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:995)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1185)
            at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1287)
            at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2243)
            at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:111)
            at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:278)
            at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
            at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:78)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:740)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:821)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:255)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
            at edu.sjsu.cmpe277.termproject.secondActivity.onCreate(secondActivity.java:43)
            at android.app.Activity.performCreate(Activity.java:6289)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
            at android.app.ActivityThread.access$900(ActivityThread.java:177)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5942)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)

2 个答案:

答案 0 :(得分:1)

摆脱

Activity

并仅使用您在Bundle中使用的明确交易。您无法通过Fragment,在布局中声明{{1}},并尝试访问它,导致您的应用崩溃

答案 1 :(得分:0)

activity_second.xml文件中,更改:

class="edu.sjsu.cmpe277.termproject.Fragments.FirstFragment"

android:name="edu.sjsu.cmpe277.termproject.Fragments.FirstFragment"

这是正确的使用前缀。