llPointerException异常的片段的onCreate

llPointerException异常的片段的onCreate

本文介绍了NullPointerException异常的片段的onCreateView()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的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.");...

这是一个片段的oncreateview方法,我有一个NullPointerException异常,在最后一行

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)

下面是整个onCreateView():

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;
    }

和我使用的ViewPagerIndicator。

And I am using ViewPagerIndicator.

抱歉,如果这是菜鸟,我的眼睛流了出来......感谢您的帮助!

Sorry if this is noob, my eyes flow out...Thanks for your help!

最后的解决办法:

这是不需要的:

LinearLayout layout = (LinearLayout) view
                .findViewById(R.layout.impresszum);

代替这种应使用:

Instead this should be used:

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

和在此行的另一个修改:

And another modification in this line:

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

我已经发现了一些解释,太:如果我想正确的,首先,我需要充气布局能够使用findViewById访问对象 ...这是偷偷摸摸的,因为的setContentView()做到这一点对我们来说。

I have found some explanation, too:If I am thinking right, first I needed to inflate the layout to be able to access the objects using "findViewById"... It is sneaking, because setcontentview() does this for us.

推荐答案

首先,我需要膨胀的布局,可以使用findViewById() - 正确的code包含在现在的问题

First I needed to inflate the layout, to be able to use findViewById() - the correct code is included in the question now.

这篇关于NullPointerException异常的片段的onCreateView()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 16:47