Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
611 views
in Technique[技术] by (71.8m points)

android - NullPointerException in Fragment's onCreateView() method

My code:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // t-alk és impresszumhoz
    LinearLayout layout = (LinearLayout) getActivity().findViewById(
            R.layout.impresszum);
    ((TextView) layout.findViewById(R.id.impresszumtext1)).setText(" v.");...

This is a fragment's oncreateview method, and I have a nullpointerexception at the last line

03-04 13:48:20.149: E/AndroidRuntime(32586):    at com.myapp.TestFragment.onCreateView(TestFragment.java:53)

Here is the whole onCreateView():

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

        // feltételekhez és folyamatokhoz
        WebView wv = new WebView(getActivity());
        FrameLayout flayout = new FrameLayout(getActivity());

        flayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
        flayout.setBackgroundResource(R.drawable.gradient_gray_light);

        wv.setInitialScale(100);
        wv.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
        wv.setBackgroundColor(getResources().getColor(R.color.transparent2));
        wv.setPadding(5, 5, 5, 5);

        wv.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode,
                    String description, String failingUrl) {
                Toast.makeText(getActivity(), description, Toast.LENGTH_SHORT)
                        .show();
                Toast.makeText(getActivity(), "Hiba t?rtént a bet?ltés során",
                        Toast.LENGTH_LONG).show();
            }

            @Override
            public void onPageFinished(WebView view, final String url) {

                progressDialog.cancel();

            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                progressDialog = ProgressDialog.show(getActivity(), "",
                        "Loading...");
                progressDialog.setCancelable(true);
                progressDialog.setCanceledOnTouchOutside(false);
            }
        });

        // feltételek
        if (mContent.equalsIgnoreCase("Feltételek")) {
            getActivity().getWindow().setFormat(PixelFormat.RGBA_8888);
            wv.loadUrl("file:///android_asset/conditions.htm");
            flayout.addView(wv);
            return flayout;

        }
        // folyamatok
        else if (mContent.equalsIgnoreCase("Folyamatok")) {
            getActivity().getWindow().setFormat(PixelFormat.RGBA_8888);
            wv.loadUrl("file:///android_asset/process.htm");
            flayout.addView(wv);
            return flayout;
        }
        // T-alkalmazások
        else if (mContent.equalsIgnoreCase("T-alkalmazások")) {

        }
        // Impresszum
        else if (mContent.equalsIgnoreCase("Impresszum")) {

            final String TAG = "Impresszum";
            String versionName;
            String appName;

            appName = getResources().getString(R.string.app_name);
LinearLayout layout = (LinearLayout) getActivity().findViewById(
                R.layout.impresszum);
            try {

                versionName = getActivity().getPackageManager().getPackageInfo(
                        getActivity().getPackageName(), 0).versionName;


        ((TextView)layout.findViewById(R.id.impresszumtext1)).setText(appName
                 + " v." + versionName);


            } catch (NameNotFoundException ex) {
                Log.e(TAG + ".1", "error: " + ex.getMessage(), ex);
            }
            return layout;
        }


        return layout;
    }

And I am using ViewPagerIndicator.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here are the internals of the Fragment life cycle:


FragmentManager sets the activity value for a Fragment performing a state transition:

f.mActivity = mActivity;
f.mFragmentManager = mActivity.mFragments;
f.mCalled = false;
f.onAttach(mActivity);

Also, it sets this value to null, after detaching the Fragment:

f.mCalled = false;
f.onDetach();

. . .

f.mActivity = null;
f.mFragmentManager = null;

So, The value should not be null, between onAttach() and onDetach(), if every thing else is fine.


To be safe, move all such code to onActivityCreated();


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...