3

i'm using this method to get URL from Browsable intent filter and paste that URL in my text field which seems to be working fine but i want to load that URL in my webView. i have tried "webView.loadUrl(intentUri);" but it makes the app force close (see log cat below), so i can't load URL's in onCreate But i have found a workaround which is to Get URL from my Text Field and Load that URL in my WebView i have tried "webView.loadUrl(urlEditText.getText());" in webview settings but i cant get it working the error it give me is "The method loadUrl(String) in the type WebView is not applicable for the arguments (Editable)"

code

TextView uri = (TextView) findViewById(R.id.urlField);
//if (Intent.ACTION_MAIN.equals(getIntent().getAction())) {
    String intentUri = (new Intent("com.example.browsableintent.MY_ACTION"))
            .toUri(Intent.URI_INTENT_SCHEME).toString();
    uri.setText(intentUri);
    webView.loadUrl(intentUri);
    Log.w("URLHandler", intentUri);
//} else {
    Uri data = getIntent().getData();
    if (data == null) {
        uri.setText("");
    } else {
        uri.setText(getIntent().getData().toString());
    }
//}

Log Cat

02-11 17:50:48.678: E/AndroidRuntime(29023): FATAL EXCEPTION: main
02-11 17:50:48.678: E/AndroidRuntime(29023): Process: com.air.swiftmp, PID: 29023
02-11 17:50:48.678: E/AndroidRuntime(29023): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.air.swiftmp/com.air.swiftmp.MainActivity}: java.lang.NullPointerException
02-11 17:50:48.678: E/AndroidRuntime(29023):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
02-11 17:50:48.678: E/AndroidRuntime(29023):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350)
02-11 17:50:48.678: E/AndroidRuntime(29023):    at android.app.ActivityThread.access$800(ActivityThread.java:163)
02-11 17:50:48.678: E/AndroidRuntime(29023):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
02-11 17:50:48.678: E/AndroidRuntime(29023):    at android.os.Handler.dispatchMessage(Handler.java:102)
02-11 17:50:48.678: E/AndroidRuntime(29023):    at android.os.Looper.loop(Looper.java:157)
02-11 17:50:48.678: E/AndroidRuntime(29023):    at android.app.ActivityThread.main(ActivityThread.java:5335)
02-11 17:50:48.678: E/AndroidRuntime(29023):    at   java.lang.reflect.Method.invokeNative(Native Method)
02-11 17:50:48.678: E/AndroidRuntime(29023):    at java.lang.reflect.Method.invoke(Method.java:515)
02-11 17:50:48.678: E/AndroidRuntime(29023):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
02-11 17:50:48.678: E/AndroidRuntime(29023):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
02-11 17:50:48.678: E/AndroidRuntime(29023):    at dalvik.system.NativeStart.main(Native Method)
02-11 17:50:48.678: E/AndroidRuntime(29023): Caused by: java.lang.NullPointerException
02-11 17:50:48.678: E/AndroidRuntime(29023):    at com.air.swiftmp.MainActivity.onCreate(MainActivity.java:95)
02-11 17:50:48.678: E/AndroidRuntime(29023):    at android.app.Activity.performCreate(Activity.java:5389)
02-11 17:50:48.678: E/AndroidRuntime(29023):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
02-11 17:50:48.678: E/AndroidRuntime(29023):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2256)

i just want a way to Get URL from my Text Field and Load it in the WebView either in onCreate or in webView Settings. Thanks

Rishabh Srivastava
  • 3,683
  • 2
  • 30
  • 58
User_420
  • 83
  • 2
  • 11

1 Answers1

2

"webView.loadUrl(urlEditText.getText());" in webview settings but i cant get it working the error it give me is "The method loadUrl(String) in the type WebView is not applicable for the arguments (Editable)"

Because loadUrl takes url as String but getText() method return Editable object so use toString() with getText() like:

String strUrl=urlEditText.getText().toString();
if(strUrl.length()>0)
  webView.loadUrl(strUrl);
else
  //... show message if url is null

and getting NPE so make sure calling setContentView before initializing webView and urlEditText

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • i tried loading this in webview settings but it loads the url on every launch regardless of whether there is really a valid url in or or it is blank – User_420 Feb 13 '15 at 08:18
  • can we add a if- else statement to detect if url is blank or not and load only if not blank – User_420 Feb 13 '15 at 08:19
  • @User_420: Instead of solve another question on chat simply finish this question by accepting it as answer if this answer help in solving posted issue in current question and for new question create a fresh question by which other SO users also look your question. Thanks – ρяσѕρєя K Feb 13 '15 at 08:27
  • here please http://stackoverflow.com/questions/28496961/changing-screen-orientation-in-android – User_420 Feb 13 '15 at 10:30