dimanche 19 avril 2015

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

}
}

Aucun commentaire:

Enregistrer un commentaire