I'm making a WebView browser and I get an unreachable code error at one line of the MainActivity, I'm still learning Android coding so i apologize if this is a noob question I'll paste the code below. Thanks!
package com.browser.tssomas;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
public class MainActivity extends Activity{
WebView ourBrow;
Button go;
Button back;
Button refresh;
Button forward;
Button clearHistory;
EditText Url;
ProgressBar Pbar;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
ourBrow = (WebView) findViewById(R.id.wvBrowser); <-- UNREACHABLE CODE HERE
WebSettings webSettings = ourBrow.getSettings();
webSettings.setJavaScriptEnabled(true);
ourBrow.getSettings().setLoadWithOverviewMode(true);
ourBrow.getSettings().setUseWideViewPort(true);
ourBrow.getSettings().setBuiltInZoomControls(true);
ourBrow.getSettings().setAllowFileAccess(true);
refresh = (Button) findViewById(R.id.bRefresh);
Url = (EditText) findViewById(R.id.etURL);
Pbar = (ProgressBar) findViewById(R.id.progBar);
ourBrow.setWebViewClient(new InsideWebViewClient());
ourBrow.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
if(progress < 100 && Pbar.getVisibility() == ProgressBar.GONE){
Pbar.setVisibility(ProgressBar.VISIBLE);
}
Pbar.setProgress(progress);
if(progress == 100) {
Pbar.setVisibility(ProgressBar.GONE);
}
}
});
{
ourBrow.loadUrl("http://www.google.com");
}
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.cH:
ourBrow.clearHistory();
return true;
case R.id.HAGAY:
String theWebsite = Url.getText().toString();
if(theWebsite != null)
ourBrow.loadUrl(theWebsite);
return true;
case R.id.Forward:
if (ourBrow.canGoForward())
ourBrow.goForward();
return true;
case R.id.back:
if (ourBrow.canGoBack())
ourBrow.goBack();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
class myWebClient extends WebViewClient
{
public void refreshButtonClicked(View view)
{
ourBrow.reload();
}
}
}
Move the return true; statement to the end of your method. In Java, returning from a method means that the method stops running.
Therefore, it is impossible for the code ourBrow = (WebView) findViewById(R.id.wvBrowser); to ever execute, because the method is instructed to stop right before it. This gives a compile time error.
Related
I’ve create this Web View app, but something doesn’t work well. The page is reloaded over and over again. Could you help me to find why it is happens ?
I think this happens when it try to load the body content of the url.
package com.maunexus;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private WebView webView;
Activity activity;
private ProgressDialog progDailog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView=findViewById(R.id.webviewid);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://pari365.mg");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.super_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.menu_back:
onBackPressed();
break;
case R.id.menu_forward:
onForwardPressed();
break;
case R.id.menu_refresh:
webView.reload();
Toast.makeText(this, "Reloading... Please Wait!", Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
private void onForwardPressed(){
if (webView.canGoForward()){
webView.goForward();
} else {
Toast.makeText(this, "Already there! ;)", Toast.LENGTH_SHORT).show();
}
}
public void onBackPressed(){
if (webView.canGoBack()){
webView.goBack();
}
}
}
There’s sometimes when the refresh is not happening, but the url does not load in full.
You need to add this line of code for accepting thirdparty cookies
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
CookieManager.getInstance().setAcceptThirdPartyCookies(webView,true);
}
It will work like a charm
MY GOAL:take whatever was placed into the search bar and then turn it into a string which can be a variable used across multiple activities. I am wondering on line 92 , would i need a if statement of some sort to see if there are any integers passed. That then would need to be converted into a string. Or Would the code work fine the way it is. Thank you for reading!
package com.karanvir.search;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
Intent intentGoogle;
Random rn;
SharedPreferences urls;
AutoCompleteTextView searchBar;
public static String urlGlobal;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
searchBar=(AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
Button button=(Button) findViewById(R.id.button);
Intent intentGoogle= new Intent(getApplicationContext(),Main2Activity.class);
rn= new Random();
urls=this.getSharedPreferences("com.karanvir.search", Context.MODE_PRIVATE);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings4) {
startActivity(intentGoogle);
return true;
} else if(id ==R.id.action_settings2){
return true;
}else if (id==R.id.action_settings3){
return true;
}else if(id==R.id.action_settings1){
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.alert_dark_frame)
.setTitle("About")
.setMessage("stuff");
return true;
}
return super.onOptionsItemSelected(item);
}
public void jump(View view){
//intnet changing target of our code
urlGlobal=searchBar.getText().toString();
if
//public static String urlGlobal=
/* urls.edit().putString("url",searchBar.getText().toString()).apply();
String Stringurls=urls.getString("url","");*/
int pageJump = rn.nextInt(3)+1;
if (pageJump==1){
startActivity(intentGoogle);
} else if (pageJump==2){
} else if(pageJump==3){
}
}
}
Turns out it does convert it into a string so there is no need for further complications.
I am trying to add a back button to app but when i add the code i get the "Non static method 'canGoBack() cannot be referenced from a static context" error. I have read several stack articles about this error but have not been able to solve it. Any ideas please?
package com.test;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;
import com.parse.ParseInstallation;
import com.parse.ParsePush;
import com.parse.ParseQuery;
import com.parse.PushService;
public class MainActivity extends Activity implements OnClickListener {
private Button push;
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(getApplicationContext(), "onReceive invoked!", Toast.LENGTH_LONG).show();
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PushService.setDefaultPushCallback(this, MainActivity.class);
Button back = (Button) findViewById(R.id.back);
WebView webView = (WebView) findViewById(R.id.webView1);;
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.xxxxx.xxxxxx.xxxxx.xxxx// ");
webView.setWebViewClient(new WebViewClient());
push = (Button)findViewById(R.id.senPushB);
push.setOnClickListener(this);
}
private class Callback extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return (true);
}
}
#Override
public void onBackPressed()
{
if(WebView.canGoBack()){
WebView.goBack();
}else{
super.onBackPressed();
}
}
#Override
public void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(this).unregisterReceiver(mBroadcastReceiver);
}
#Override
public void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver, new IntentFilter(MyCustomReceiver.intentAction));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public void onClick(View v) {
JSONObject obj;
try {
obj = new JSONObject();
obj.put("alert", "hello!");
obj.put("action", MyCustomReceiver.intentAction);
obj.put("customdata","My message");
ParsePush push = new ParsePush();
ParseQuery query = ParseInstallation.getQuery();
// Push the notification to Android users
query.whereEqualTo("deviceType", "android");
push.setQuery(query);
push.setData(obj);
push.sendInBackground();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
Try changing:
#Override
public void onBackPressed()
{
if(WebView.canGoBack()){
WebView.goBack();
}else{
super.onBackPressed();
}
}
to:
#Override
public void onBackPressed()
{
WebView webView = (WebView) findViewById(R.id.webView1);
if(webView.canGoBack()){
webView.goBack();
}else{
super.onBackPressed();
}
}
Explanation:
You should call canGoBack() and goBack() for the webview instance that you're using (move to the previous view). This is also why the method is declared at the instance level and not at the class level (static)
canGoBack is an instance (non-static) method. It can only be called on an instance of the WebView class. WebView is the class. Calling WebView.function() only works if function is a static function. You need to get the instance of the WebView and call it on that.
For the record, the difference between a static and instance method- a static method may not use any non-static data. An instance method can. Static data only has 1 copy per class. Non-static data has 1 copy per instance of the class.
I'm developing an android application and I'm using Eclipse ADT.
I want to get the content of a webpage that say only true or false
Here is my code of my MainActivity.java
package com.appmobirep;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addButtonClickListner();
}
public void addButtonClickListner()
{
Button btnLogin = (Button)findViewById(R.id.btnTest);
btnLogin.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Need to get the webpage content and set it to a string
String Data = "";
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Can you please help me on this
Thank you...
I'm not sure what's wrong here, all i want to do is randomly grab an item from my array. Which is just like a random sentence. Then generate another once the button is pressed. All my code looks good to me but it's causing a crash when i hit the button. any ideas?
package com.my.package;
import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View.OnClickListener;
public class Randomsentence extends Activity implements OnClickListener{
private String[] myString;
private static final Random rgenerator = new Random();
private TextView tv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources();
myString = res.getStringArray(R.array.myArray);
String q = myString[rgenerator.nextInt(myString.length)];
TextView tv = (TextView) findViewById(R.id.text1);
tv.setText(q);
View nextButton = findViewById(R.id.next_button);
nextButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.next_button:
tv.setText(myString[rgenerator.nextInt(myString.length)]);
break;
}
}
#Override
public boolean onCreateOptionsMenu (Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu3, menu);
return true;
}
#Override
public boolean onOptionsItemSelected (MenuItem item) {
switch (item.getItemId()) {
case R.id.menu:
startActivity(new Intent(this, Main.class));
return true;
case R.id.startnhie:
startActivity(new Intent(this, startnhie.class));
return true;
}
return false;
}
}
In your onCreate(), change
TextView tv = (TextView) findViewById(R.id.text1);
to
tv = (TextView) findViewById(R.id.text1);
since you already declared TextView tv as an instance variable.