下面是代碼,它基本上是一個簡單的菜單驅動的應用程序,我試圖在片段中使用自定義字體,並導致問題。 我已經在一個活動中成功地使用了這個字體,這個活動啓動了另一個活動,在這個活動中這個片段被誇大了。 我不確定這是什麼導致問題,請幫助我。設置自定義字體片段時出現異常錯誤android
package com.cbs.CoronaTheCarnival;
import android.app.Fragment;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* Created by aditya on 2/4/14.
*/
public class AboutFragment extends Fragment{
View rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.fragment_about, container, false);
initFonts();
return rootView;
}
public void initFonts(){
TextView tv_title = (TextView) rootView.findViewById(R.id.tv_about_title);
Typeface changeFont = Typeface.createFromAsset(getActivity().getAssets(), "TRACK.OTF");
tv_title.setTypeface(changeFont);
TextView tv_theme = (TextView) rootView.findViewById(R.id.tv_about_theme);
tv_theme.setTypeface(changeFont);
}
}
這裏是logcat的,它說,異常錯誤,我不知道爲什麼它不工作,請幫我出,其殺死我和4:21在上午。
02-06 04:19:41.534 3591-3591/com.cbs.CoronaTheCarnival E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cbs.CoronaTheCarnival/com.cbs.CoronaTheCarnival.HomeActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.cbs.CoronaTheCarnival.AboutFragment.initFonts(AboutFragment.java:27)
at com.cbs.CoronaTheCarnival.AboutFragment.onCreateView(AboutFragment.java:20)
at android.app.Fragment.performCreateView(Fragment.java:1695)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
at android.app.BackStackRecord.run(BackStackRecord.java:682)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
at android.app.Activity.performStart(Activity.java:5113)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
感謝隊友。它解決了它:D – dw19