所以我试图在一个片段中的文本视图上使用自定义字体。我让它在一个活动中工作,但是当我将代码转移到片段时,它不喜欢它。我对它进行了一些修改,使其更喜欢片段中的代码,如下所示:

TextView title;
Typeface font;

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

    View viewRoot = inflater.inflate(R.layout.fillup, container, false);

    title = (TextView) getActivity().findViewById(R.id.fillup_title);
    font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/cgphenixamerican.ttf");
    title.setTypeface(font);

    Button btn_gas = (Button) viewRoot.findViewById(R.id.btn_IGotGas);

    View.OnClickListener onclick = new View.OnClickListener(){

                @Override
                public void onClick(View view){

                    consumer_transaction_total = (EditText)getActivity().findViewById(R.id.consumer_transaction_total);
                    consumer_price_per_gallon = (EditText)getActivity().findViewById(R.id.consumer_price_per_gallon);
                    consumer_odometer_miles_driven = (EditText)getActivity().findViewById(R.id.consumer_odometer_miles_driven);
                    consumer_trip_miles_driven = (EditText)getActivity().findViewById(R.id.consumer_trip_miles_driven);
                    consumer_fillup_date = (EditText)getActivity().findViewById(R.id.consumer_fillup_date);

                    Log.d("transaction_total: ", consumer_transaction_total.getText().toString());
                    Log.d("price_per_gallon: ", consumer_price_per_gallon.getText().toString());
                    Log.d("odometer_miles_driven: ", consumer_odometer_miles_driven.getText().toString());
                    Log.d("trip_miles_driven: ", consumer_trip_miles_driven.getText().toString());
                    Log.d("fillup_date: ", consumer_fillup_date.getText().toString());


                }
            };
    btn_gas.setOnClickListener(onclick);
    return viewRoot;
}

下面是我得到的日志错误:
05-03 15:03:15.668  15837-15837/loboweb.igg E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: loboweb.igg, PID: 15837
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setTypeface(android.graphics.Typeface)' on a null object reference
            at loboweb.igg.FillUp.onCreateView(FillUp.java:88)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
            at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)
            at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:1072)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:918)
            at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1440)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
            at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:444)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1166)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
            at android.view.Choreographer.doCallbacks(Choreographer.java:580)
            at android.view.Choreographer.doFrame(Choreographer.java:550)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            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:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

我不确定它为什么指向空引用,因为它应该与我的activity oncreateview中的另一个引用相同。
我搞不清出了什么事。请帮助!
编辑:
FillUp.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg_flower_road">

<!-- Fill Up!-->
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Fill Up!"
    android:textSize="100sp"
    android:id="@+id/fillup_title"
    android:textColor="#FFFFFF"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="30sp" />

<!-- Total Spent-->
<EditText
    android:layout_width="200sp"
    android:layout_height="40sp"
    android:hint="Total Spent"
    android:id="@+id/consumer_transaction_total"
    android:textSize="15sp"
    android:layout_marginTop="21sp"
    android:layout_below="@+id/fillup_title"
    android:layout_centerHorizontal="true" />

<!-- Price Per Gallon-->
<EditText
    android:layout_width="200sp"
    android:layout_height="40sp"
    android:id="@+id/consumer_price_per_gallon"
    android:hint="Price Per Gallon"
    android:textColor="#000000"
    android:textSize="15sp"
    android:layout_below="@+id/consumer_transaction_total"
    android:layout_centerHorizontal="true" />

<!-- Odometer-->
<EditText
    android:layout_width="200sp"
    android:layout_height="40sp"
    android:id="@+id/consumer_odometer_miles_driven"
    android:hint="Odometer"
    android:textColor="#000000"
    android:textSize="15sp"
    android:layout_below="@+id/consumer_price_per_gallon"
    android:layout_centerHorizontal="true" />

<!-- Trip Distance-->
<EditText
    android:layout_width="200sp"
    android:layout_height="40sp"
    android:id="@+id/consumer_trip_miles_driven"
    android:hint="Trip Distance"
    android:textColor="#000000"
    android:textSize="15sp"
    android:layout_below="@+id/consumer_odometer_miles_driven"
    android:layout_centerHorizontal="true" />

<!-- "Date of Fill Up-->
<EditText
    android:layout_width="200sp"
    android:layout_height="40sp"
    android:id="@+id/consumer_fillup_date"
    android:hint="Date of Fill Up"
    android:inputType="date"
    android:textColor="#000000"
    android:textSize="15sp"
    android:layout_below="@+id/consumer_trip_miles_driven"
    android:layout_centerHorizontal="true" />

<!-- Button Layout-->
<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="56sp"
    android:id="@+id/button_layout"
    android:gravity="center_horizontal"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/consumer_fillup_date"
    android:layout_marginTop="50sp" >

    <!-- I Got Gas Button-->
    <Button
        android:layout_width="170sp"
        android:layout_height="fill_parent"
        android:textColor="#000000"
        android:text="I Got Gas!"
        android:id="@+id/btn_IGotGas"
        android:textSize="13sp"
        android:layout_marginEnd="4sp" />
</RelativeLayout>

<!-- Fill Up Error-->
<!--<TextView-->
    <!--android:layout_width="fill_parent"-->
    <!--android:layout_height="56dp"-->
    <!--android:textColor="#000000"-->
    <!--android:id="@+id/"-->
    <!--android:layout_below="@+id/button_layout"/>-->
</RelativeLayout>

最佳答案

您应该在视图上调用findViewById而不是在Activity上调用。
更改代码:

View viewRoot = inflater.inflate(R.layout.fillup, container, false);

title = (TextView) getActivity().findViewById(R.id.fillup_title);
font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/cgphenixamerican.ttf");
title.setTypeface(font);

对此:
View viewRoot = inflater.inflate(R.layout.fillup, container, false);

title = (TextView) viewRoot.findViewById(R.id.fillup_title);
font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/cgphenixamerican.ttf");
title.setTypeface(font);

10-08 18:05