Fragment处的空指针异常

时间:2014-08-05 12:48:18

标签: android

我在尝试在标签中实现这些代码时得到null pointer exception。那个时候我在运行时遇到了错误。

堆栈跟踪:

08-05 08:40:53.884: E/AndroidRuntime(5560): FATAL EXCEPTION: main
08-05 08:40:53.884: E/AndroidRuntime(5560): Process: com.gems.android, PID: 5560
08-05 08:40:53.884: E/AndroidRuntime(5560): java.lang.NullPointerException
08-05 08:40:53.884: E/AndroidRuntime(5560):     at com.sit.gems.frgment.AudioFragment.onCreateView(AudioFragment.java:24)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at android.view.View.measure(View.java:16497)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at android.view.View.measure(View.java:16497)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at android.view.View.measure(View.java:16497)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at android.view.View.measure(View.java:16497)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at android.view.View.measure(View.java:16497)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at android.view.Choreographer.doFrame(Choreographer.java:544)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at android.os.Handler.handleCallback(Handler.java:733)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at android.os.Handler.dispatchMessage(Handler.java:95)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at android.os.Looper.loop(Looper.java:136)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at android.app.ActivityThread.main(ActivityThread.java:5017)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at java.lang.reflect.Method.invokeNative(Native Method)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at java.lang.reflect.Method.invoke(Method.java:515)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-05 08:40:53.884: E/AndroidRuntime(5560):     at dalvik.system.NativeStart.main(Native Method)

AudioFragment.java:

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ViewFlipper;

import com.gems.android.R;
import com.sit.gems.app.BaseFragment;
import com.sit.gems.util.Utilities;
//import android.util.Log;

public class AudioFragment extends BaseFragment
{
    int mFlipping = 0;
    private View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
        ViewFlipper flipper = (ViewFlipper) view.findViewById(R.id.flipper1); //---- > Error
        /* Start Flipping */
        flipper.startFlipping();
        mFlipping = 1;

        return view = inflater.inflate(R.layout.slide_left, null);
    }
}

slide_left.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >



    <ViewFlipper
        android:id="@+id/flipper1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:flipInterval="3000"
        android:inAnimation="@anim/slide_in_right"
        android:outAnimation="@anim/slide_in_left"
        android:layout_centerInParent="true" >

        <ImageView
            android:src="@drawable/img1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:contentDescription="@string/str_img1"
            android:layout_gravity="center_horizontal" />

        <ImageView
            android:src="@drawable/img2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:contentDescription="@string/str_img2"
            android:layout_gravity="center_horizontal" />

        <ImageView
            android:src="@drawable/img3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:contentDescription="@string/str_img3"
            android:layout_gravity="center_horizontal" />

        <ImageView
            android:src="@drawable/img4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/str_img4"
            android:layout_gravity="center_horizontal" />

        <ImageView
            android:src="@drawable/img5"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:contentDescription="@string/str_img5"
            android:layout_gravity="center_horizontal" />

    </ViewFlipper>

</RelativeLayout>

任何人都可以帮助我。谢谢。

4 个答案:

答案 0 :(得分:1)

View对象的使用位置为null

试试这个:

public class AudioFragment extends BaseFragment  { 
    int mFlipping = 0 ;
    private View view;
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.slide_left, null);

        ViewFlipper flipper =(ViewFlipper)view.findViewById(R.id.flipper1); ---->Error 


            /** Start Flipping */ 
            flipper.startFlipping();
            mFlipping=1;

        return view;

    } 

} 

答案 1 :(得分:1)

您正在试用R.layout.slide_left布局,并在 后尝试使用它来实例化view 。在您当前的代码中,当您尝试与其进行交互时,viewnull。在尝试使用它之前,需要先将夸大。所以你需要改变它;

ViewFlipper flipper =(ViewFlipper)view.findViewById(R.id.flipper1); // Error 
    /** Start Flipping */
    flipper.startFlipping();
    mFlipping=1;
return view = inflater.inflate(R.layout.slide_left, null);

对此;

view = inflater.inflate(R.layout.slide_left, null);
ViewFlipper flipper =(ViewFlipper)view.findViewById(R.id.flipper1); // No Error 
    /** Start Flipping */
    flipper.startFlipping();
    mFlipping=1;
return view;

我希望这会有所帮助。

答案 2 :(得分:1)

你应该定义&#34;查看&#34;变量...类似于:

final View view = inflater.inflate(R.layout.your_layout, null);

然后你就可以打电话了:

ViewFlipper flipper =(ViewFlipper)view.findViewById(R.id.flipper1);

答案 3 :(得分:1)

您正在使用视图变量,但尚未为其定义值... 首先为它定义值,然后使用它......

view = inflater.inflate(R.layout.slide_left, null, false);

这应该是那样...............

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

        view = inflater.inflate(R.layout.slide_left, null, false);
        ViewFlipper flipper =(ViewFlipper)view.findViewById(R.id.flipper1); ---->Error solved 

        /** Your Code... */


        return view;

    }