i have this problem : java.lang.NullPointerException
My Codes and LogCat pictures :
WebviewActivity.class
public class WebviewActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
Bundle alinan = getIntent().getExtras();
String alinmis = alinan.getString("veri");
WebView webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(alinmis);
webView.getSettings().setBuiltInZoomControls(true);
webView.setWebChromeClient(new WebChromeClient() {
});
MenutrActivity.class
public class MenutrActivity extends Activity{
Bundle bnd;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menutr);
bnd = new Bundle();
Button mekanlar = (Button)findViewById(R.id.mekanlar);
final Intent i = new Intent(getApplicationContext(), WebviewActivity.class);
mekanlar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent("android.intent.action.WEBK"));
String myString = "http://www.google.com.tr";
bnd.putString("veri", myString);
i.putExtras(bnd);
startActivity(i);
}
}); }}
application fails when I press the button.
LogCat error : java.lang.NullPointerException in WebviewActivity.class
First of all change "StartActivity(new Intent...." to "Intent i = new Intent(...". You call the activity before putting extra.
Next time just copy your code and paste as a code. This way it is very hard to see...
Related
I want to pass URL string from main activity to second activity and load the URL in second activity.... but when I click the go button at mainactivity it goes to second activity but it shows nothing but blank.
here is my code ..
MainActivity:-
public class MainActivity extends AppCompatActivity {
EditText editText;
Button go;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=findViewById(R.id.urltext);
go=findViewById(R.id.button6);
final String link=go.getText().toString();
go.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(),Webview.class);
intent.putExtra(link,1);
startActivity(intent);
}
});
}
}
Second activity:
public class Webview extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
String one = getIntent().getExtras().getString("1");
String http="https://";
String url=http+one;
WebView webView = (WebView)findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
}
}
No offence but you must have read the documentation first .
intent.putExtra(key,value);
Key should be constant in case of getting value from the same key in other component.So the answer is your OnClick shold be like below.
final String link=go.getText().toString();
go.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(YourActivity.this,Webview.class);
intent.putExtra("link",link);
startActivity(intent);
}
});
Getting url from intent in other activity.
String one = getIntent().getExtras().getString("link");
String http="https://";
String url=http+one;// Use this url to open in web view
How to refresh current Activity 1 time after load? My code refresh the activity, but in Loop.
This site have some examples, but most is after click button. Isn't my case.
My code is:
finish();startActivity(getIntent());
just do this way.
startActivity(getIntent());
finish();
public class MainActivity extends Activity {
String url;
//initializing WebView
private WebView mwebView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Div1().execute();
String tag = (String) getIntent().getSerializableExtra("tag");
Intent intent = getIntent();
url = "https://xxxxxxxx.com/profile/"+tag+"";
//WebView
mwebView = (WebView) findViewById(R.id.myWebView);
WebSettings webSettings = mwebView.getSettings();
webSettings.setJavaScriptEnabled(true);
//improve webView performance
mwebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
//mwebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
mwebView.getSettings().setAppCacheEnabled(false);
mwebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webSettings.setDomStorageEnabled(true);
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
webSettings.setUseWideViewPort(true);
webSettings.setSavePassword(true);
webSettings.setSaveFormData(true);
webSettings.setEnableSmoothTransition(true);
mwebView.loadUrl("https://xxxxxxxx.com/profile/"+tag+"");
//force links open in webview only
mwebView.setWebViewClient(new MyWebviewClient());
}
private class MyWebviewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if (Uri.parse(url).getHost().equals("xxxxxxxx.com/profile/\"+tag+\"")){
return false;
}
else {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}}
// Div1 AsyncTask
private class Div1 extends AsyncTask<String, String, String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected String doInBackground(String... params) {
return null;
}
#Override
protected void onPostExecute(String result) {
mwebView.loadUrl("javascript:(function(){"+
"l=document.getElementsByTagName('BUTTON')[1];"+
"e=document.createEvent('MouseEvent');"+
"e.initEvent('click',true, true);"+
"l.dispatchEvent(e);"+
"})()");
}
}
}
Button ok = (Button) findViewById(R.id.btnOK2);
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(getIntent());
finish();
}
});
This is sample code I wrote:
public class MainActivity extends AppCompatActivity {
static boolean isInit = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(isInit) {
isInit = false;
startActivity(new Intent(this, MainActivity.class));
finish();
Log.d("Restart", "asdasda");
}
}
}
One thing I need to say is that if you start this activity from another activity, in every call you need set the isInit variable true like this:
MainActivity.isInit = true;
startActivity(new Intent(this, MainActivity.class));
Using "getIntent()" instead of "new Intent(this, MainActivity.class)" cause closing the application after logging "Restart" text.
I don't know whether it is what you want (I think this can not be any solution of any problem). I still think better is reload only views.
I have two ImageButton with two different url. When I click the image, it returns an white screen.I don't know what is the problem. Any edit or suggestions are welcome.
Thanks!
main activity
public class main extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ImageButton amazon = (ImageButton)findViewById(R.id.imagebutton1);
ImageButton flipkart = (ImageButton)findViewById(R.id.imagebutton2);
amazon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),"Amazon",Toast.LENGTH_LONG).show();// display the toast on home button click
Intent intent = new Intent(main.this, MainActivity.class);
intent.setData(Uri.parse("http://www.amazon.com"));
WebView webview = new WebView(main.this);
setContentView(webview);
startActivity(intent);
}
});
flipkart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),"Flipkart",Toast.LENGTH_LONG).show();// display the toast on you tube button click
Intent intent = new Intent(main.this, MainActivity.class);
intent.setData(Uri.parse("http://www.flipkart.in"));
startActivity(intent);
}
});
}
my webview activity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView htmlWebView = (WebView) findViewById(R.id.webView);
htmlWebView.setWebViewClient(new CustomWebViewClient());
WebSettings webSetting = htmlWebView.getSettings();
webSetting.setJavaScriptEnabled(true);
webSetting.setDisplayZoomControls(true);
htmlWebView.loadUrl("");
}
class CustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
Do you want to load Url in your app or want to open via External Browser. anaway I'm given the snipped code for the open the url in webview as given below in your application as:
WebView mynews;
ProgressBar pb;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newsreadscreen);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
pb= (ProgressBar) findViewById(R.id.progress_bar);
mynews= (WebView) findViewById(R.id.mynews);
mynews.getSettings().setJavaScriptEnabled(true);
mynews.getSettings().setDefaultFontSize(17);
mynews.getSettings().setDisplayZoomControls(true);
mynews.getSettings().setDomStorageEnabled(true);
mynews.getSettings().setLoadsImagesAutomatically(true);
mynews.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
pb.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
pb.setVisibility(View.GONE);
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
pb.setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
}
});
mynews.loadUrl("https://www.google.co.in");
this is the code I have Edited
amazon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),"Amazon",Toast.LENGTH_LONG).show();// display the toast on home button click
Intent intent = new Intent(main.this, MainActivity.class);
intent.putExtra("url","http://www.amazon.com");
startActivity(intent);
}
});
get the value on the MainActivity.Class onCreateMethod like below
String url=getIntent().getStringExtra("url");
mynews.loadUrl(url);
i have a problem with loading a webview from a string url.
here is the code of an activity with only WebView.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
Intent intent=getIntent();
String url=intent.getStringExtra("url");
//EditText edit=(EditText)findViewById(R.id.editText1);
//String url=edit.getText().toString();
WebView webview=(WebView)findViewById(R.id.webView1);
webview.loadUrl(url);
webview.setWebViewClient(new WebViewClient());
}`
and code of the calling activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onGo(View v)
{
EditText edit=(EditText)findViewById(R.id.editText1);
Intent intent=new Intent(MainActivity.this,WebActivity.class);
intent.putExtra("url", edit.getText().toString());
startActivity(intent);
}
i have even added the android.permissions.INTERNET in the manifest
but still i cannot view the page with the click of the button in MainActivity.
From the comment
yes the real device has the same error with "www.google.com"
Use
http://www.google.com
Also make sure you have internet permission mentioned in manifest file
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
I am having a WebView and i am getting data in it from a WebService.
my problem is, there are so many url's in the data i am getting and when a user click on it, then it should load on a WebViewon next page not on a browser.
How can i achieve that.? I tried shouldOverrideUrlLoading but it's not working.
Try this.
public class WebViewActivity extends Activity {
private ProgressDialog progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
progressBar = ProgressDialog.show(this, "Hi", "me");
WebView webView = (WebView) findViewById(R.id.webView1);
webView.setWebViewClient(new MyWebViewClient());
webView.loadUrl("http://www.mysite.com/android.php");
}
private class MyWebViewClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
if (progressBar != null && progressBar.isShowing()) {
progressBar.dismiss();
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
System.out.println("here");
//Upadated code..........
Bundle bundle = new Bundle();
//Add your data from getFactualResults method to bundle
bundle.putString("NextUrl", url);
Intent i = new Intent(WebViewActivity.this, NextActivity.class);
i.putExtras(bundle);
startActivity(i);
//-----------------------
Log.i("Hi", "WEb nextttt");
return true;
}
}
}
NextActivity
public class NextActivity extends Activity {
WebView wv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
Bundle bundle = getIntent().getExtras();
//Extract the data…
String url = bundle.getString("NextUrl");
Log.i("Hi", "Get next");
wv1 = (WebView) findViewById(R.id.webView1);
wv1.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
wv1.getSettings().setSupportZoom(false);
wv1.getSettings().setSupportMultipleWindows(false);
wv1.setHorizontalScrollBarEnabled(false);
wv1.setVerticalScrollBarEnabled(false);
wv1.getSettings().setUseWideViewPort(true);
wv1.getSettings().setLoadWithOverviewMode(true);
wv1.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView viewx, String urlx) {
viewx.loadUrl(urlx);
return false;
}
});
try {
wv1.loadUrl(url);
} catch (Exception e) {
e.printStackTrace();
}
}
}