LoadUrl处的空指针异常

时间:2014-06-06 06:40:53

标签: android nullpointerexception

我在customWebView.loadUrl上遇到Null指针异常。我在行尾提到了错误。我不知道如何解决这些错误。

StackTrace:

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

MagazinDetailFragment.java:

package com.sit.fth.frgment;

import java.net.URLDecoder;

import android.os.Bundle;
//import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.gems.android.R;
import com.sit.fth.app.BaseFragment;
import com.sit.fth.util.CustomWebView;

public class MagazineDetailFragment extends BaseFragment{

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

        this.setHasOptionsMenu(true);

        String url=getArguments().getString("url");
        View view=inflater.inflate(R.layout.magzine_detail, null);

        ((TextView) view.findViewById(R.id.header)).setText(getArguments().getString("title"));

        CustomWebView customWebView=new CustomWebView(getActivity());
        FrameLayout frameLayout=(FrameLayout) view.findViewById(R.id.webmagzine);
        frameLayout.addView(customWebView);
        ////AppLog.v("URL---->", "URLMAG"+url);

        customWebView.loadUrl("http://docs.google.com/gview?embedded=true&url="+URLDecoder.decode(url));  <----38th Line Error


          view.findViewById(R.id.blog_back).setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    getActivity().onBackPressed();

                }
            });

        return view;
    }
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        menu.clear();
        inflater.inflate(R.menu.main_menu, menu);
    }   

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menuitem_back:
            getActivity().onBackPressed();
            break;

        default:
            break;
        }

        return true;
    }

}

如果你知道如何解决这些问题,任何人都可以帮助我。谢谢。

1 个答案:

答案 0 :(得分:1)

实际上你在这里没有得到空值:

String url=getArguments().getString("url");

使用它来避免异常

if(url!=null){
customWebView.loadUrl("http://docs.google.com/gview?embedded=true&url="+URLDecoder.decode(url));}
else{
 Log.e("message","NULL value");
}