1

我正在使用 Fragment,但我出错了。此代码在 3 天前有效,我不会更改任何内容(我猜),但它不起作用。我该如何解决?

当我调用 setContentView(R.layout.main); 时,我看到了 Log.d;在我的片段活动中,它崩溃了。为什么这样做?

我正在使用 v4.app.Fragment,

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

我的 xml 文件和第二个片段等于 15 行;

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

        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

    <fragment 
            android:name="com.ex.zzigez.Home"
            android:id="@+id/home"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    <fragment 
            android:name="com.ex.zzigez.Prj"
            android:id="@+id/prj"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
</LinearLayout>

我的片段活动

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.widget.TextView;

public class Main extends FragmentActivity {

    private UiLifecycleHelper uiHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Log.d("deneme","Main");
        uiHelper = new UiLifecycleHelper(this, callback);
        uiHelper.onCreate(savedInstanceState);

        init();
        for (int i = 0; i < fragments.length; i++) {
            fragmentTransaction.hide(fragments[i]);
        }
        fragmentTransaction.commit();
        Log.d("deneme", "1");
        Log.d("deneme", "OnCreate Starts and All Fragments hidden!");

    }
private void init(){
        Log.d("deneme", "11");
        tv1 = (TextView) findViewById(R.id.textView1);
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        fragments[HOME] = fragmentManager.findFragmentById(R.id.home);
        fragments[PRJ] = fragmentManager.findFragmentById(R.id.prj);
    }

我的项目班

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class Prj extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.prj, container, false);
        tv2 = (TextView) v.findViewById(R.id.textView2);

        Log.d("deneme", "PRJ.javadayız");
        Session session = Session.getActiveSession();
        if(session.isOpened()){
            makeRequest(session);
            Log.d("deneme", "PRJ session açıkk");

        }
        return v;

    }

我的日志猫

07-07 07:09:03.609: E/AndroidRuntime(30490): FATAL EXCEPTION: main
07-07 07:09:03.609: E/AndroidRuntime(30490): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ex.zzigez/com.ex.zzigez.Main}: android.view.InflateException: Binary XML file line #15: Error inflating class fragment
07-07 07:09:03.609: E/AndroidRuntime(30490):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
07-07 07:09:03.609: E/AndroidRuntime(30490):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
07-07 07:09:03.609: E/AndroidRuntime(30490):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
07-07 07:09:03.609: E/AndroidRuntime(30490):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
07-07 07:09:03.609: E/AndroidRuntime(30490):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-07 07:09:03.609: E/AndroidRuntime(30490):    at android.os.Looper.loop(Looper.java:137)
07-07 07:09:03.609: E/AndroidRuntime(30490):    at android.app.ActivityThread.main(ActivityThread.java:4898)
07-07 07:09:03.609: E/AndroidRuntime(30490):    at java.lang.reflect.Method.invokeNative(Native Method)
07-07 07:09:03.609: E/AndroidRuntime(30490):    at java.lang.reflect.Method.invoke(Method.java:511)
07-07 07:09:03.609: E/AndroidRuntime(30490):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
07-07 07:09:03.609: E/AndroidRuntime(30490):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
07-07 07:09:03.609: E/AndroidRuntime(30490):    at dalvik.system.NativeStart.main(Native Method)
07-07 07:09:03.609: E/AndroidRuntime(30490): Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class fragment
07-07 07:09:03.609: E/AndroidRuntime(30490):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
07-07 07:09:03.609: E/AndroidRuntime(30490):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
07-07 07:09:03.609: E/AndroidRuntime(30490):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
07-07 07:09:03.609: E/AndroidRuntime(30490):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-07 07:09:03.609: E/AndroidRuntime(30490):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
07-07 07:09:03.609: E/AndroidRuntime(30490):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:308)
07-07 07:09:03.609: E/AndroidRuntime(30490):    at android.app.Activity.setContentView(Activity.java:1924)
07-07 07:09:03.609: E/AndroidRuntime(30490):    at com.ex.zzigez.Main.onCreate(Main.java:24)
07-07 07:09:03.609: E/AndroidRuntime(30490):    at android.app.Activity.performCreate(Activity.java:5206)
07-07 07:09:03.609: E/AndroidRuntime(30490):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
07-07 07:09:03.609: E/AndroidRuntime(30490):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
07-07 07:09:03.609: E/AndroidRuntime(30490):    ... 11 more
07-07 07:09:03.609: E/AndroidRuntime(30490): Caused by: java.lang.NullPointerException
07-07 07:09:03.609: E/AndroidRuntime(30490):    at com.ex.zzigez.Prj.onCreateView(Prj.java:28)
07-07 07:09:03.609: E/AndroidRuntime(30490):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
07-07 07:09:03.609: E/AndroidRuntime(30490):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:884)
07-07 07:09:03.609: E/AndroidRuntime(30490):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1066)
07-07 07:09:03.609: E/AndroidRuntime(30490):    at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1168)
07-07 07:09:03.609: E/AndroidRuntime(30490):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:280)
07-07 07:09:03.609: E/AndroidRuntime(30490):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
07-07 07:09:03.609: E/AndroidRuntime(30490):    ... 21 more
4

0 回答 0