片段中OnClickListener的NullPointerException

时间:2014-05-08 20:06:19

标签: android android-fragments nullpointerexception onclicklistener

我正在尝试在片段中运行计时器,我的一篇关于点击的文本视图正在获得NPE。它可能是我很容易忽视的东西,但我似乎无法弄明白。

import android.app.Fragment;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Chronometer;
import android.widget.TextView;

public class TimersCountdownFragment extends Fragment {

    final static String ARG_POSITION = "position";
    int mCurrentPosition = -1;
    TextView countdownStart;
    TextView countdownStop;
    Chronometer countdownChronometer;
    boolean isChronometerRunning;

    public TimersCountdownFragment(){}

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

        // If activity recreated (such as from screen rotate), restore
        // the previous article selection set by onSaveInstanceState().
        // This is primarily necessary when in the two-pane layout.
        if (savedInstanceState != null) {
            mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
        }
        View view = inflater.inflate(R.layout.fragment_timer_countdown, container, false);

        countdownStart = (TextView) getActivity().findViewById(R.id.timerStartDown);
        countdownStop = (TextView) getActivity().findViewById(R.id.timerStopDown);
        countdownChronometer = (Chronometer) getActivity().findViewById(R.id.chronometerDown);
        //countdownStop.setAlpha(50);

        countdownStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isChronometerRunning) {
                    isChronometerRunning = true;
                    countdownChronometer.start();
                    countdownStop.setText("Stop");
                    //countdownStart.setAlpha(50);
                    //countdownStop.setAlpha(100);
                }

            }
        });

        countdownStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isChronometerRunning){
                    countdownChronometer.stop();
                    countdownStop.setText("Reset");
                    isChronometerRunning = false;
                    countdownStart.setAlpha(100);
                } else {
                    countdownChronometer.setBase(SystemClock.elapsedRealtime());
                    countdownStop.setText("Stop");
                    isChronometerRunning = false;
                    //countdownStart.setAlpha(100);
                    //countdownStop.setAlpha(50);
                }


            }
        });

        // Inflate the layout for this fragment
        return view;
    }

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


    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        // Save the current article selection in case we need to recreate the fragment
        outState.putInt(ARG_POSITION, mCurrentPosition);
    }
}

logcat的

05-08 16:02:40.490: E/AndroidRuntime(31992): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rcd.mypr/com.rcd.mypr.Timers.TimersActivity}: java.lang.NullPointerException
05-08 16:02:40.490: E/AndroidRuntime(31992):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
05-08 16:02:40.490: E/AndroidRuntime(31992):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
05-08 16:02:40.490: E/AndroidRuntime(31992):    at android.app.ActivityThread.access$800(ActivityThread.java:139)
05-08 16:02:40.490: E/AndroidRuntime(31992):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
05-08 16:02:40.490: E/AndroidRuntime(31992):    at android.os.Handler.dispatchMessage(Handler.java:102)
05-08 16:02:40.490: E/AndroidRuntime(31992):    at android.os.Looper.loop(Looper.java:136)
05-08 16:02:40.490: E/AndroidRuntime(31992):    at android.app.ActivityThread.main(ActivityThread.java:5102)
05-08 16:02:40.490: E/AndroidRuntime(31992):    at java.lang.reflect.Method.invokeNative(Native Method)
05-08 16:02:40.490: E/AndroidRuntime(31992):    at java.lang.reflect.Method.invoke(Method.java:515)
05-08 16:02:40.490: E/AndroidRuntime(31992):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-08 16:02:40.490: E/AndroidRuntime(31992):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-08 16:02:40.490: E/AndroidRuntime(31992):    at dalvik.system.NativeStart.main(Native Method)
05-08 16:02:40.490: E/AndroidRuntime(31992): Caused by: java.lang.NullPointerException
05-08 16:02:40.490: E/AndroidRuntime(31992):    at com.rcd.mypr.Timers.TimersCountdownFragment.onCreateView(TimersCountdownFragment.java:45)
05-08 16:02:40.490: E/AndroidRuntime(31992):    at android.app.Fragment.performCreateView(Fragment.java:1700)
05-08 16:02:40.490: E/AndroidRuntime(31992):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
05-08 16:02:40.490: E/AndroidRuntime(31992):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
05-08 16:02:40.490: E/AndroidRuntime(31992):    at android.app.BackStackRecord.run(BackStackRecord.java:684)
05-08 16:02:40.490: E/AndroidRuntime(31992):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
05-08 16:02:40.490: E/AndroidRuntime(31992):    at android.app.Activity.performStart(Activity.java:5257)
05-08 16:02:40.490: E/AndroidRuntime(31992):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2182)
05-08 16:02:40.490: E/AndroidRuntime(31992):    ... 11 more

一些额外的眼睛和任何帮助将不胜感激。

由于

编辑:

XML Layout fragment_timer_countdown.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="fill_parent">

    <Chronometer
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/chronometerDown"
        android:layout_gravity="center_horizontal"
        android:textSize="100dp"
        android:layout_weight="1"
        android:gravity="center"/>

    <LinearLayout
        android:orientation="vertical"
        android:layout_height="2dip"
        android:layout_width="fill_parent"
        android:background="@color/red"
        android:layout_marginBottom="5dp"/>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Start"
            android:id="@+id/timerStartDown"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_gravity="bottom"
            android:textSize="50dp"/>

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:layout_weight="0.02"
            android:background="#50ffffff" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Stop"
            android:id="@+id/timerStopDown"
            android:layout_weight="1"
            android:gravity="center"
            android:textAlignment="center"
            android:layout_gravity="bottom"
            android:textSize="50dp"/>
    </LinearLayout>


</LinearLayout>

3 个答案:

答案 0 :(得分:1)

countdownStopnullfindViewById在这里:

countdownStop = (TextView) getActivity().findViewById(R.id.timerStopDown);

未在R.id.timerStopDown布局中找到TimersCountdownFragment

也许你的意思是view.findViewById(R.id.timerStopDown)

答案 1 :(得分:1)

我认为你应该使用以下方式。

View view = inflater.inflate(R.layout.fragment_timer_countdown, container, false);
countdownStart = (TextView) view.findViewById(R.id.timerStartDown);
countdownStop = (TextView) view.findViewById(R.id.timerStopDown);
countdownChronometer = (Chronometer)view.findViewById(R.id.chronometerDown);

答案 2 :(得分:0)

你是否在持有这个片段的活动中有这些ID?

    R.id.timerStartDown, 
    R.id.timerStopDown, 
    R.id.chronometerDown

否则,如果它在你的R.layout.fragment_timer_countdown中输入,试试这个

countdownStart = (TextView) view.findViewById(R.id.timerStartDown);
countdownStop = (TextView) view.findViewById(R.id.timerStopDown);
countdownChronometer = (Chronometer) view.findViewById(R.id.chronometerDown);

因为你在视图中将布局膨胀为

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