Android崩溃了ViewPager中的方向更改

时间:2013-09-24 21:09:12

标签: android android-fragments android-viewpager

我有一个运行两个片段的ViewPager,当应用程序的方向发生变化时,应用程序崩溃了。我附上下面的代码。

片段寻呼机适配器:

public class PagerAdapter extends FragmentPagerAdapter{


    public PagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        Fragment f;

        switch (position) {
        case 0:
            f = new Chemicals1();
            break;
        case 1:
            f = new Chemicals2();
            break;
        default:
            f = new Fragment();
            break;
        }

        return f;
    }

    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
        case 0:
            return getString(R.string.strChemicalsApplied);
        case 1:
            return getString(R.string.strRawFinished);
        default:
            return "Page " + (position + 1);
        }
    }
}

启动ViewPager

 super.onCreate(savedInstanceState);
    setContentView(R.layout.swipe_chemicals);

    //Set up ViewPager and allow app to hold prior pages when off screen
    ChemicalsPager = (ViewPager) findViewById(R.id.chemicals_pager);
    ChemicalsAdapter = new PagerAdapter(getSupportFragmentManager());
    ChemicalsPager.setAdapter(ChemicalsAdapter);
    ChemicalsPager.setOffscreenPageLimit(1);

ViewPager的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/chemicals_pager"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v4.view.PagerTabStrip
    android:id="@+id/pager_title_strip"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:paddingBottom="4dip"
    style="@style/pagerTabStrip" />

logcat的:

09-24 16:05:31.381: E/AndroidRuntime(19595): FATAL EXCEPTION: main
09-24 16:05:31.381: E/AndroidRuntime(19595): java.lang.RuntimeException: Unable to start activity ComponentInfo{us.rns.iepareporting/us.rns.iepareporting.SwipeChemicals}: java.lang.NullPointerException
09-24 16:05:31.381: E/AndroidRuntime(19595):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
09-24 16:05:31.381: E/AndroidRuntime(19595):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2146)
09-24 16:05:31.381: E/AndroidRuntime(19595):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3590)
09-24 16:05:31.381: E/AndroidRuntime(19595):    at android.app.ActivityThread.access$800(ActivityThread.java:140)
09-24 16:05:31.381: E/AndroidRuntime(19595):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
09-24 16:05:31.381: E/AndroidRuntime(19595):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-24 16:05:31.381: E/AndroidRuntime(19595):    at android.os.Looper.loop(Looper.java:137)
09-24 16:05:31.381: E/AndroidRuntime(19595):    at android.app.ActivityThread.main(ActivityThread.java:4947)
09-24 16:05:31.381: E/AndroidRuntime(19595):    at java.lang.reflect.Method.invokeNative(Native Method)
09-24 16:05:31.381: E/AndroidRuntime(19595):    at java.lang.reflect.Method.invoke(Method.java:511)
09-24 16:05:31.381: E/AndroidRuntime(19595):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
09-24 16:05:31.381: E/AndroidRuntime(19595):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
09-24 16:05:31.381: E/AndroidRuntime(19595):    at dalvik.system.NativeStart.main(Native Method)
09-24 16:05:31.381: E/AndroidRuntime(19595): Caused by: java.lang.NullPointerException
09-24 16:05:31.381: E/AndroidRuntime(19595):    at us.rns.iepareporting.Chemicals1$GenericTextWatcher.setPhosphateUsed(Chemicals1.java:251)
09-24 16:05:31.381: E/AndroidRuntime(19595):    at us.rns.iepareporting.Chemicals1$GenericTextWatcher.afterTextChanged(Chemicals1.java:128)
09-24 16:05:31.381: E/AndroidRuntime(19595):    at android.widget.TextView.sendAfterTextChanged(TextView.java:7573)
09-24 16:05:31.381: E/AndroidRuntime(19595):    at android.widget.TextView.setText(TextView.java:3804)
09-24 16:05:31.381: E/AndroidRuntime(19595):    at android.widget.TextView.setText(TextView.java:3655)
09-24 16:05:31.381: E/AndroidRuntime(19595):    at android.widget.EditText.setText(EditText.java:100)
09-24 16:05:31.381: E/AndroidRuntime(19595):    at android.widget.TextView.setText(TextView.java:3630)
09-24 16:05:31.381: E/AndroidRuntime(19595):    at android.widget.TextView.onRestoreInstanceState(TextView.java:3530)
09-24 16:05:31.381: E/AndroidRuntime(19595):    at android.view.View.dispatchRestoreInstanceState(View.java:12270)
09-24 16:05:31.381: E/AndroidRuntime(19595):    at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2882)
09-24 16:05:31.381: E/AndroidRuntime(19595):    at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2882)
09-24 16:05:31.381: E/AndroidRuntime(19595):    at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2882)
09-24 16:05:31.381: E/AndroidRuntime(19595):    at android.view.View.restoreHierarchyState(View.java:12248)
09-24 16:05:31.381: E/AndroidRuntime(19595):    at android.support.v4.app.Fragment.restoreViewState(Fragment.java:417)
09-24 16:05:31.381: E/AndroidRuntime(19595):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:933)
09-24 16:05:31.381: E/AndroidRuntime(19595):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
09-24 16:05:31.381: E/AndroidRuntime(19595):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070)
09-24 16:05:31.381: E/AndroidRuntime(19595):    at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1861)
09-24 16:05:31.381: E/AndroidRuntime(19595):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:547)
09-24 16:05:31.381: E/AndroidRuntime(19595):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1178)
09-24 16:05:31.381: E/AndroidRuntime(19595):    at android.app.Activity.performStart(Activity.java:5217)
09-24 16:05:31.381: E/AndroidRuntime(19595):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2094)
09-24 16:05:31.381: E/AndroidRuntime(19595):    ... 12 more

setPhosphate使用导致崩溃的代码:

private void setPhosphateUsed()
    {
        try
        {
            PhosphateReading = ((SwipeChemicals) getActivity()).getPhosphateReading();
            Double PhosphateUsed = PhosphateReading - Double.parseDouble(txtPhosphateReading.getText().toString());
            txtPhosphateUsed.setText(Double.toString(PhosphateUsed));
        }
        catch (NumberFormatException e) {}
    }

1 个答案:

答案 0 :(得分:0)

正在导致NullPointerException,因为即使实际文本值未更改,方向更改时也会调用GenericTextWatcher。只需添加catch (NullPointerException e) {}即可捕获错误并导致应用程序强行关闭。