这是代码,它基本上是一个简单的菜单驱动的应用程序,我试图在一个片段中使用自定义字体,这导致了一个问题。
我已经在一个活动中成功地使用了这个字体,这个菜单活动启动了另一个活动,在这个活动中这个片段被膨胀。
我不确定这是否是导致问题的原因,所以请帮我。
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)
最佳答案
在initfonts方法中,可以执行以下操作:
TextView tv_title = (TextView) rootView.findViewById(R.id.tv_about_title);
这里,rootview为空,因为它从未正确初始化。您似乎混淆了两个rootview变量:
View rootView; // this is one variable
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.fragment_about, container, false);
// this is not the same rootView !
// here, rootView != this.rootView
initFonts();
return rootView;
}
public void initFonts(){
TextView tv_title = (TextView) rootView.findViewById(R.id.tv_about_title);
// this is like using this.rootView.findViewById
// ...
}
initfonts方法看不到oncreateview方法中的rootview,因此它将使用您在oncreateview上声明的视图,第一个视图从未正确初始化。要解决此问题,可以将initfonts的代码移回oncreateview中,将视图作为initfonts方法的参数传递,或在oncreateview中初始化变量,如下所示:
this.rootView = rootView;