简体   繁体   中英

Launch url from one activity on another activity

I've been trying to make a small WebView app that lets the user search and load websites within it.

Currently my code looks promising in terms of the structure and the reference used, but I have stumbled upon an error which I can't seem to fix. Ive been looking for solutions on how to parse data as a string from my MainActivity class EditText to the WebView on my WebActivity class.

So far I've had no luck, I've tried several websites and have researched more into this but I have no clue what's going on. I even looked up similar answers regarding my situation on StackOverFlow but nothing has worked for me.

This Is the error I'm getting:

04-20 16:56:55.563: D/AndroidRuntime(32040): Shutting down VM
04-20 16:56:55.564: E/AndroidRuntime(32040): FATAL EXCEPTION: main
04-20 16:56:55.564: E/AndroidRuntime(32040): Process: com.thearclabs.carpo, PID: 32040
04-20 16:56:55.564: E/AndroidRuntime(32040): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.thearclabs.carpo/com.thearclabs.carpo.WebActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
04-20 16:56:55.564: E/AndroidRuntime(32040):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236)
04-20 16:56:55.564: E/AndroidRuntime(32040):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
04-20 16:56:55.564: E/AndroidRuntime(32040):    at android.app.ActivityThread.access$800(ActivityThread.java:151)
04-20 16:56:55.564: E/AndroidRuntime(32040):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
04-20 16:56:55.564: E/AndroidRuntime(32040):    at android.os.Handler.dispatchMessage(Handler.java:102)
04-20 16:56:55.564: E/AndroidRuntime(32040):    at android.os.Looper.loop(Looper.java:135)
04-20 16:56:55.564: E/AndroidRuntime(32040):    at android.app.ActivityThread.main(ActivityThread.java:5254)
04-20 16:56:55.564: E/AndroidRuntime(32040):    at java.lang.reflect.Method.invoke(Native Method)
04-20 16:56:55.564: E/AndroidRuntime(32040):    at java.lang.reflect.Method.invoke(Method.java:372)
04-20 16:56:55.564: E/AndroidRuntime(32040):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
04-20 16:56:55.564: E/AndroidRuntime(32040):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
04-20 16:56:55.564: E/AndroidRuntime(32040): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
04-20 16:56:55.564: E/AndroidRuntime(32040):    at com.thearclabs.carpo.WebActivity.<init>(WebActivity.java:9)
04-20 16:56:55.564: E/AndroidRuntime(32040):    at java.lang.reflect.Constructor.newInstance(Native Method)
04-20 16:56:55.564: E/AndroidRuntime(32040):    at java.lang.Class.newInstance(Class.java:1606)
04-20 16:56:55.564: E/AndroidRuntime(32040):    at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
04-20 16:56:55.564: E/AndroidRuntime(32040):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226)
04-20 16:56:55.564: E/AndroidRuntime(32040):    ... 10 more

This is the code in my MainActivity's onCreate method:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText searchInput = (EditText) findViewById(R.id.searchInput);
final String webUrl = searchInput.getText().toString();

searchInput.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            Intent intent = new Intent(MainActivity.this, WebActivity.class);
            intent.putExtra("website", webUrl);
            startActivity(intent);
            return true;
        }
        return false;
    }
});

And this is my WebActivity code:

public class WebActivity extends Activity {

    String webUrl = getIntent().getExtras().getString("webUrl"); // error is coming from here, according the the error log.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        WebView webView = (WebView) findViewById(R.id.webView);
        webView.loadUrl("http://example.com" + webUrl);

        // webView.getSettings().setJavaScriptEnabled(true);

    }
}

Call getIntent() in any method like onCreate method instead of at class level:

    String webUrl = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
         webUrl = getIntent().getExtras().getString("website");
        //..your code here...
   }

Also make following changes :

1. Add null check before accessing getExtras() method.

2. Use same key which is used in previous Activity for adding data in Intent. currently you are using webUrl for getting value but in previous Activity website is used

Move this line

String webUrl = getIntent().getExtras().getString("webUrl");

after onCreate() method.

EDIT:

Move this line

final String webUrl = searchInput.getText().toString(); 

in to this condition

searchInput.setOnEditorActionListener(new TextView.OnEditorActionListener(){
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    if (actionId == EditorInfo.IME_ACTION_SEARCH) { 
        String webUrl = searchInput.getText().toString(); 
        Intent intent = new Intent(MainActivity.this, WebActivity.class);
        intent.putExtra("website", webUrl);
        startActivity(intent);
        return true;
    }
    return false;
}
});

Since getIntent() method available in activity life cycle methods.therfore use

String webUrl = getIntent().getExtras().getString("website"); // error is coming from here, according the the error log.

inside onCreate method as

String webUrl = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);

       webUrl = getIntent().getExtras().getString("website");
        WebView webView = (WebView) findViewById(R.id.webView);
        webView.loadUrl("http://example.com" + webUrl);

        // webView.getSettings().setJavaScriptEnabled(true);

    }

you have to use key to get data from intent. try below code

public class WebActivity extends Activity {



        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_web);
  String webUrl = getIntent().getExtras().getString("website"); // error is coming from here, according the the error log.
            WebView webView = (WebView) findViewById(R.id.webView);
            webView.loadUrl("http://example.com" + webUrl);

            // webView.getSettings().setJavaScriptEnabled(true);

        }
    }

Please can you update webActivity there.

public class WebActivity extends Activity {
       String webUrl="";
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_web);
            Bundle bundle=getIntent().getExtras();

            if(bundle!=null){
               webUrl = bundle.getString("webUrl"); 
            }

            WebView webView = (WebView) findViewById(R.id.webView);
            if(!TextUtils.isEmpty(webUrl)){
                 webView.loadUrl("http://example.com" + webUrl);
            }
            // webView.getSettings().setJavaScriptEnabled(true);

        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM