Unable to load url in webview - android

I'm trying to load a URL in a webview but I keep getting an error. (see below)
this is the code that should load the URL at startup:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
WebView mywebview1;
mywebview1 = (WebView)findViewById(R.id.myownwebview);
mywebview1.loadUrl("http://www.mad-sharky.com");
}
I have a webview in my XML file with id "myownwebview". Whenever i start the projecti get the error message shown in the picture:
I searched a lot and most of the answers I found tell that the URL is written to the view before it is created and therefore it throws an exception. It is supposed to be after "seContentView". But in my case it is after setContentView so I can't find what is causing it.
Debug screen image:

probably the fragment transaction is causing the exception, so comment this code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}*/
WebView mywebview1;
mywebview1 = (WebView)findViewById(R.id.myownwebview);
mywebview1.loadUrl("http://www.mad-sharky.com");
}
or load the url into the WebView from the PlaceholderFragment

Related

Bundle is getting null and it didn't take the bundle args

Im developing a small android application, i was using shared preference in order to transfer data between fragments. now i want to use bundle but the problem is the bundle in the second fragment getting null value how do i solve that problem ?
here are some part of code
... some usefull Code in first fragment
args.putLong("favoriteCountry", countryListSpinnerData.get(favoriteCountrySpinner.getSelectedItemPosition()).getId());
args.putInt("favoriteCurrency", currencySpinner.getSelectedItemPosition());
args.putDouble("favoriteBudget", Double.parseDouble(budgetEditText.getText().toString()));
args.putString("additionalInformation", additionalInformationEditText.getText().toString());
MoneyPartnerShipStepTwo moneyPartnerShipStepTwo = new MoneyPartnerShipStepTwo();
moneyPartnerShipStepTwo.setArguments(args);
FragmentHelper.NAVIGATE_FRAGMENT(new MoneyPartnerShipStepTwo(), getActivity())
Now the second fragment
#Override
public void onCreate(Bundle savedInstanceState) { // savedInstanceState is null why ???
super.onCreate(savedInstanceState);
if (getArguments() != null) {
this.bundle = savedInstanceState;
}
setHasOptionsMenu(true);
}
you create new MoneyPartnerShipStepTwo(), not instance with your argument.
so, please change FragmentHelper.NAVIGATE_FRAGMENT(moneyPartnerShipStepTwo, getActivity())

Understanding how AndroidStudio generated code restores a PlaceholderFragment

I created a test application using AndroidStudio, selecting an activity with a fragment. What I do not understand is how the PlaceholderFragment is restored when savedInstanceState is not null, taking into account that setContentView is called after super.onCreate().
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
Hello the savedInstanceState is only not null when you rotated the device. When the activity recreate it should load from savedInstanceState. by the way the code you are talking about is part of android activity not android studio. Thank you.

Added button and app crushed

I tried to made button that goes to webpages but when I lunch app it crushes. There are no errors tos. What to do? Mby there is a problem in XML?
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
addButtonClickListener(); //Soc network buttons
}
public void addButtonClickListener() //soc network buttons
{
Button facebook = (Button)findViewById(R.id.Facebookpoga); //Facebook pogai
facebook.setOnClickListener(new OnClickListener(){
public void onClick(View arg)
{
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com"));
startActivity(intent);
}
});
}
I had encountered a similar problem, lot of times while learning the basics of Android development. And I can tell that the problem is that, for some reasons findViewById(id) returns null. This started happening after an update when eclipse started including a Fragment layout with the main layout for each Activity.
I sorted out the problems by putting all such calls in onStart() instead of onCreate(). And I am sure that sorts out the problem for you to.
Do this:
protected void onStart(){
addButtonClickListener(); //Soc network buttons
}
Add this function after onCreate(), hope it helps. And yes, remove the listener call addButtonClickListener(); from onCreate().
I guess the problem may be because Fragment layout is not instantinated immediately after setContentView() which results in findViewById(R.id.Facebookpoga); to return null, because no such View with id R.id.Facebookpoga is created yet.

setOnClickListener generates Fragment exception

My App works great when I implement View.OnClickListener, ToolTipView.OnToolTipViewClickedListener, extending Activity context.
But, when I extend FragmentActivity instead of Activity as I mentioned, the app throws an exception.
Code is storagged onto
Supertooltips
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ToolTipRelativeLayout toolTipRelativeLayout = (ToolTipRelativeLayout) findViewById(R.id.activity_main_tooltipRelativeLayout);
myToolTipView = toolTipRelativeLayout.showToolTipForView(
new ToolTip()
.withText("A beautiful View")
.withColor(Color.RED)
.withShadow(true)
.withAnimationType(ToolTip.ANIMATIONTYPE_FROMTOP),
findViewById(R.id.activity_main_redtv));
myToolTipView.setOnToolTipViewClickedListener(MainActivity.this);
}
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
[SOLVED]
I had override onCreateOptionsMenu and move all implementation there.
My problem was due to the repro deep in code

How to open Webview on top layer of my activity? (Android)

I have a problem to add a webview after I use startactivity. I have an intent and start it :
final Intent intent = new Intent(Intent.ACTION_VIEW, uri);
String uriString = uri.toString();
String extension = uriString.substring(uriString.lastIndexOf('.') + 1);
intent.setClass(this, extensionToActivity.get(extension));
startActivity(intent);
after my activity ran completely, I need to open a webview so I go to Oncreate function and add my code
public void onCreate(Bundle savedInstanceState)
{
WebView wv = (WebView)findViewById(R.id.webView1);
String summary = "<html><body><h1>some test</h1></body></html>";
wv.loadData(summary, "text/html", null);
wv.setVisibility(View.VISIBLE);
}
but my program crash!!! but if i show alert inside onCreat it works fine, what is the problem?
what is the problem?
You forgot to set layout by using setContentView() method. FYI, You can't find any views without setting content views to activity.
Try this,
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView wv = (WebView)findViewById(R.id.webView1);
String summary = "<html><body><h1>some test</h1></body></html>";
wv.loadData(summary, "text/html", null);
wv.setVisibility(View.VISIBLE);
}
EDIT:
#Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
I found that the Intent that I work uses another activity inside, this is because my program crash. I found that activity and add my webview there, now it is solved
setContentView(R.layout.browseritems);
WebView wv = (WebView)findViewById(R.id.webView1);
String summary = "<html><body><h1>some test</h1></body></html>";
wv.loadData(summary, "text/html", null);
wv.setVisibility(View.VISIBLE);

Categories

Resources