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
Related
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.
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.
I have used the code that Eclipse creates by default and added a button with an OnClickListener.
The following code crashes at the last line where I use setOnClickListener(this).
public class MainActivity extends Activity implements OnClickListener {
private Button startStopButton;
#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();
}
startStopButton = (Button) findViewById(R.id.startButton);
startStopButton.setOnClickListener(this);
}
This is probably something trivial but I don't understand what the issue is. The onClick method is defined below if that is in any way relevant, but it does nothing right now.
Using onClick in the layout xml works, but I have read elsewhere that it is bad practice to use it.
Thanks in advance !
The problem is that you are loading a fragment
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
and into the PlaceholderFragment() doesn´t exist a reference of startButton
startStopButton = (Button) findViewById(R.id.startButton);
your problem is similar to this post:
Unable to load url in webview (android)
Try changing the line implements OnClickListener to implements View.OnClickListener :-)
Or as I see now.
Are you sure that the button is not in the XML layout file, which belongs to PlaceHolderFragment?
Replace startStopButton.setOnClickListener(this);with
startStopButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i=new Intent("com.monster.android.activitylifecycle.SecndActivity");
startActivity(i);
}
});
You are passing this which is refering to object of current activity..instead you should pass object of annonymouse class View.OnClickListener which implements onclick() method..
Please implement the onclick() method.
public void onClick(View v){
if(v.getId()==R.id.startButton)
{
//type what you want to do here
}
}
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
I have a Working model of fragments, when i was debugging the code i saw that the Fragment onCreate is being called 4 times.
Below is my code:
MyFragmentActivity
class MyFragmentActivity extends FragmentActivity{
#Override
public void onCreate(Bundle savedInstanceState) {
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(fragmentID, new MyListFragmentt())
.replace(detailFragmentID, new MyDetailFragment()).commit();
}
}
#Override
protected void onRestart() {
getSupportFragmentManager().beginTransaction().replace(detailFragmentID, new MyDetailFragment()).commitAllowingStateLoss();
}
}
MyDetailFragment.class
class MyDetailFragment extends Fragment{
// has method like oncreate(),onCreateView(),onSaveInstanceState()
}
How my oncreate of MyDetailFragment is called ? When i go to some other activity and come back and then tilt the device only then oncreate and onSaveInstanceState of MyDetailFragment is called multiple times.
How can i solve this, i have looked into few posts on SO but it says that we need use HIDE,Show methods and other things ? but What is the proper soultion to this ?
EDIT
When i am coming back from previous activity, my data in the MyDetailFragment needs to be refreshed.
Try this
MyDetailFragment fragment = new MyDetailFragment();
#Override
public void onCreate(Bundle savedInstanceState) {
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(fragmentID, new MyListFragmentt())
.replace(detailFragmentID, fragment).commit();
}
}
#Override
protected void onRestart() {
if(fragment != null) {
getSupportFragmentManager().beginTransaction().replace(detailFragmentID, fragment).commitAllowingStateLoss();
}
}
i think ur recreating fragments multiple times, u do new MyListFragment everytime on onCreate function, call findFragmentByTag to get the existing fragment and set that, if null (first time) then create one
/here is some code mate, if this doesnt work and ur app has single fragment better to just create xml and have only a fragment tag in it, and set that xml in setContentView function*/
// declare following member variable
MyFragment _fragment;
// in onCreate function, call this method
private void setupFragment()
{
_fragment = (MyFragment)getFragmentManager().findFragmentByTag("MyFragment");
if(null == _fragment)
{
_fragment = new MyFragment();
}
// now do the fragment transaction
FragmentTransaction trans = getFragmentManager().beginTransaction();
trans.add(containerId, _fragment, "MyFragment"); // here tag is important
trans.commit();
}