由setText()引起的片段中的空指针异常

时间:2015-08-12 20:53:54

标签: android android-fragments nullpointerexception

所以我有四个片段,我试图在其中一个片段上显示相关文字。但是,我得到一个空指针异常。我检查了其他文章,但没有找到可行的解决方案。我尝试使用view而不是getActivity()等,但似乎没有任何工作。我认为我没有做任何复杂的事情。我已经尝试将set命令放在onResume和onActivityCreated中,但这也没有用。似乎无论这些陈述在哪里,它们都会导致NPE。任何帮助将不胜感激。感谢。

(我在下面发布了相关代码)

public class TabFragment2 extends Fragment{

private TextView dilu;
private TextView incr;
private TextView plat;

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

    View view = inflater.inflate(R.layout.tab_fragment_2, container, false);


    TextView dilu = (TextView) view.findViewById(R.id.dilutionOutput);
    TextView incr = (TextView) view.findViewById(R.id.cIncrease);
    TextView plat = (TextView) view.findViewById(R.id.plateauOutput);


    return view;

}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    dilu.setText("dilution sample");
    incr.setText("increase sample");
    plat.setText("plateau sample");
}


@Override
public void onResume() {
    super.onResume();


}

编辑:这是错误日志

08-12 16:33:59.346  26015-26015/com.example.daniel.graphwear E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.daniel.graphwear, PID: 26015
java.lang.NullPointerException
        at com.example.daniel.graphwear.TabFragment2.onActivityCreated(TabFragment2.java:45)
        at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1797)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:979)
        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:1105)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:951)
        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1473)
        at android.view.View.measure(View.java:17365)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
        at android.view.View.measure(View.java:17365)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5380)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:17365)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5380)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
        at android.view.View.measure(View.java:17365)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5380)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2567)
        at android.view.View.measure(View.java:17365)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2282)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1393)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1592)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1251)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6563)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:813)
        at android.view.Choreographer.doCallbacks(Choreographer.java:613)
        at android.view.Choreographer.doFrame(Choreographer.java:583)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:799)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:146)
        at android.app.ActivityThread.main(ActivityThread.java:5692)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
        at dalvik.system.NativeStart.main(Native Method)

08-12 16:34:01.326 26015-26015 / com.example.daniel.graphwear I / Process:发送信号。 PID:26015 SIG:9

1 个答案:

答案 0 :(得分:1)

您尚未初始化字段diluincrplat,而是初始化方法内的变量。看看这个修正:

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

    View view = inflater.inflate(R.layout.tab_fragment_2, container, false);

    // initialize the proper fields instead of newly created variables in the scope of the method
    dilu = (TextView) view.findViewById(R.id.dilutionOutput);
    incr = (TextView) view.findViewById(R.id.cIncrease);
    plat = (TextView) view.findViewById(R.id.plateauOutput);


return view;

}