嗨,我正在制作一个可以从链接下载视频的应用程序。
但是我得到了NullPointerException (在kr.co.lenonstudio.kakaovideo.webviewActivity.onCreate(webviewActivity.java:43)
webviewactivity.java:43是 link = intent.getExtras()。getString(“link”);
我已经重点介绍了出现问题的地方。谢谢。

webviewactivity.java

package kr.co.lenonstudio.kakaovideo;

import java.util.ArrayList;

import kr.co.lenonstudio.kakaovideo.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;

import com.gc.materialdesign.views.ButtonRectangle;

public class webviewActivity extends Activity{


    WebView webview;
    int listcount=0;
    String link;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);


        //getExtra
        Intent intent=this.getIntent();
**link=intent.getExtras().getString("link");**


 ///
        webview=(WebView)this.findViewById(R.id.webView);
        webview.setWebViewClient(new WebClient());
        ButtonRectangle btn=(ButtonRectangle)this.findViewById(R.id.webView_btn);


        btn.setOnClickListener(btnClickListener);
        WebSettings set=webview.getSettings();
        set.setJavaScriptEnabled(true);

        webview.addJavascriptInterface(new myJavaInterface(this),"HtmlViewer");
        webview.loadUrl(""+link+"");
        webview.loadUrl("javascript:alert(InjectedObject.showHTML())");

//          Bus.getInstance().adapter=new Adapter_movie(Bus.getInstance().context,R.layout.item_movie,Bus.getInstance().arrayList);


    }

    View.OnClickListener btnClickListener=new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            webview.loadUrl("javascript:window.HtmlViewer.setCount"+"(document.getElementsByClassName('_videoThumbnailImg').length);");
            try {
                Thread.sleep(1000l);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            if(listcount>0)
            {

                webview.loadUrl("javascript:var t=document.getElementsByClassName('_videoThumbnailImg').length; for(var i=0;i<t;i++){window.HtmlViewer.setMovie"+"(document.getElementsByClassName('bn_play _btnViewVideo')[i].getAttribute('data-url'));}");
                webview.loadUrl("javascript:var t=document.getElementsByClassName('_videoThumbnailImg').length; for(var i=0;i<t;i++){window.HtmlViewer.setImg"+"(document.getElementsByClassName('_videoThumbnailImg')[i].getAttribute('src'));}");

                webview.loadUrl("javascript:window.HtmlViewer.inputlist"+"();");
                //
                try {
                    Thread.sleep(2000l);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Toast.makeText(webviewActivity.this,"Video Extracted",Toast.LENGTH_SHORT).show();

            }
            else
            {
                Toast.makeText(webviewActivity.this,"Video not Extracted.",Toast.LENGTH_SHORT).show();
            }

        }
    };


    public class myJavaInterface
    {
        Context ctx;
        ArrayList<String> movie_src=new ArrayList<String>();
        ArrayList<String> img_src=new ArrayList<String>();

        LoadImage li;
        int i;

        myJavaInterface(Context ctx)
        {
            this.ctx=ctx;
        }
        @JavascriptInterface
        public void showHTML(String html)
        {
            new AlertDialog.Builder(ctx).setTitle("HTML").setMessage(html).setPositiveButton(android.R.string.ok,null).setCancelable(false).create().show();
        }
        @JavascriptInterface
        public void setCount(String inputcount)
        {
            listcount= Integer.parseInt(inputcount);
            Log.d(""+listcount,"setCount");
        }


        @JavascriptInterface
        public void setImg(String src)
        {
            img_src.add(src);
            //async
            Log.d(""+src,"img_src");

        }

        Handler handler=new Handler()
        {

            public void handleMessage(Message message)
            {
                if(i<img_src.size())
                    Bus.getInstance().adapter.add(new Item_movie(li.bitmapImage,movie_src.get(i)));
            }

        };

        @JavascriptInterface
        public void inputlist()
        {
            Log.d(""+img_src.size(),"inputlist");
            //
            for(i=0;i<img_src.size();i++)
            {
                li=new LoadImage();
                li.ShowImageExecute(img_src.get(i));

                while(li.bitmapImage==null)
                {
                    if(li.bitmapImage!=null)
                    {
                        break;
                    }
                }

                if(Bus.getInstance().ImageList.size()!=0)
                {
                    Log.d(i+"ImageListSize!=0","inputlist");
                    boolean isequal=false;
                    for(LoadImage tmp:Bus.getInstance().ImageList)
                    {
                        if(tmp.src.equals(li.src))
                        {
                            Log.d("Has same list on LoadImage","inputlist");
                            isequal=true;
                            break;
                        }
                    }
                    if(!isequal)
                    {
                        Bus.getInstance().ImageList.add(li);
                        Bus.getInstance().arrayList.add(new Item_movie(li.bitmapImage,movie_src.get(i)));
                        Bus.getInstance().adapter.add(new Item_movie(li.bitmapImage,movie_src.get(i)));
                        //handler.sendEmptyMessage(0);
                        Bus.getInstance().adapter.notifyDataSetChanged();
                    }

                }else
                {


                    Log.d(i+"ImageListSize:0","inputlist");
                    Bus.getInstance().ImageList.add(li);
                    Bus.getInstance().arrayList.add(new Item_movie(li.bitmapImage,movie_src.get(i)));

                    Log.d("before"+Bus.getInstance().adapter.toString(),"inputlist");
                    handler.sendEmptyMessage(0);

                    Log.d("after","inputlist");




                }


            }//for



        }



        @JavascriptInterface
        public void setMovie(String src)
        {
            movie_src.add(src);
            Log.d(""+src,"movie_src");
        }





    }

    class WebClient extends WebViewClient
    {
        public boolean shouldoverrideUrlLoading(WebView view,String url)
        {
            view.loadUrl(url);
            return true;
        }
    }

}<br><br>

MainActivity.java
package kr.co.lenonstudio.kakaovideo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.gc.materialdesign.views.ButtonFlat;
import java.util.ArrayList;

public class MainActivity
  extends Fragment
{


 public void onCreate(Bundle paramBundle)
      {
        super.onCreate(paramBundle);
      }

  public View onCreateView(LayoutInflater paramLayoutInflater, ViewGroup paramViewGroup, Bundle paramBundle)
  {
    View localView = paramLayoutInflater.inflate(R.layout.activity_main, paramViewGroup, false);
    this.linkedit = ((EditText)localView.findViewById(R.id.main_search_edit));
    this.linksearch = ((ButtonFlat)localView.findViewById(R.id.main_search_btn));
    this.list = ((ListView)localView.findViewById(R.id.main_list));
    this.comment = ((TextView)localView.findViewById(R.id.search_comment));
    this.linkedit.setText(Bus.getInstance().link);
    Bus.getInstance().context = getActivity();
    this.adapter = new Adapter_movie(getActivity().getApplicationContext(), 2130903076, this.arrayList);
    this.list.setAdapter(this.adapter);
    this.list.setOnItemClickListener(this.listItemClickListener);
    Bus.getInstance().ImageList = new ArrayList();
    Bus.getInstance().arrayList = this.arrayList;
    Bus.getInstance().adapter = this.adapter;
    this.idsearch.setOnClickListener(this.idsearchClickListener);
    return localView;
  }

  Adapter_movie adapter;
  ArrayList<Item_movie> arrayList = new ArrayList();
  TextView comment;
  String link = null;
  EditText idedit;
  ButtonFlat linksearch;
  View.OnClickListener linksearchClickListener = new View.OnClickListener()
  {
    public void onClick(View paramAnonymousView)
    {
      TextView localTextView = MainActivity.this.comment;
      localTextView.setVisibility(View.VISIBLE);
      MainActivity.this.link = MainActivity.this.idedit.getText().toString();
      if (MainActivity.this.link.length() != 0)
      {
        LoadMovieAsync localLoadMovieAsync = new LoadMovieAsync();
        String[] arrayOfString = new String[1];
        arrayOfString[0] = MainActivity.this.link;
        Intent localIntent = new Intent(MainActivity.this.getActivity().getApplicationContext(), webviewActivity.class);
        localIntent.putExtra("link", MainActivity.this.link);
        MainActivity.this.startActivity(localIntent);
        return;
      }
      Toast.makeText(MainActivity.this.getActivity().getApplicationContext(), "Please Input ID.", Toast.LENGTH_SHORT).show();
    }
  };
  ListView list;
  AdapterView.OnItemClickListener listItemClickListener = new AdapterView.OnItemClickListener()
  {
    public void onItemClick(AdapterView<?> paramAnonymousAdapterView, View paramAnonymousView, int paramAnonymousInt, long paramAnonymousLong)
    {
      Bus.getInstance().sel_position = paramAnonymousInt;
      Log.d("aaaaa" + paramAnonymousInt, "pos");
    }
  };

  public void fileSave(android.content.Context paramContext)
  {
  }


}

Logcat
02-27 01:42:06.414      907-907/kr.co.lenonstudio.kakaovideo E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: kr.co.lenonstudio.kakaovideo, PID: 907
    java.lang.RuntimeException: Unable to start activity ComponentInfo{kr.co.lenonstudio.kakaovideo/kr.co.lenonstudio.kakaovideo.webviewActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference


***at kr.co.lenonstudio.kakaovideo.webviewActivity.onCreate(webviewActivity.java:43)***


 at android.app.Activity.performCreate(Activity.java:5933)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

最佳答案

因为使用link而不是Intent发送Bundle值。因此,使用IntentgetStringExtra获取价值:

    Intent intent=this.getIntent();
    if(intent !=null)
     link = intent.getStringExtra("link");

08-17 21:17