Added button and app crushed - android

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.

Related

MediaRouteButton is not active in Fragment

I use button for starting chromecast android.support.v7.app.MediaRouteButton in my app in activities xml and fragments xml with videoplayer.
For initializing cast button I use the next code:
private void setupChromeCast() {
try {
CastButtonFactory.setUpMediaRouteButton(getActivity(), castButton);
castContext = CastContext.getSharedInstance(getActivity());
castSession = castContext.getSessionManager().getCurrentCastSession();
onCastStateChanged(castContext.getCastState());
castSessionManager = new CastSessionManager(this);
isChromeCastAvailable = true;
} catch (Exception e) {
isChromeCastAvailable = false;
}
}
And it works fine in activities. I mean, when chromecast device is near, my MediaRouteButton becomes active and I can press it. But when this Button is on Fragment, it does not become active. And callback
#Override
public void onCastStateChanged(int state)
doesnt call. So, how to fix this bug? And there is one interesting moment: when I`m in fragment, button is not active, but when I hide my app into background, and then open into foreground, my mediaroutebutton becomes active. Its so strange.
In your activity, initialize Cast:
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CastContext.getSharedInstance(this);
}
Then in your fragment you can get hold of the Cast context:
#Override
public void onResume() {
super.onResume();
CastContext cc = CastContext.getSharedInstance();
cc.addCastStateListener(this);
cc.getSessionManager().addSessionManagerListener(
this, CastSession.class);
}
Verified with Cast version 18.1.0.
Taken from example docs:
"In order to integrate Google Cast functionality, the Activities need to inherit from either the AppCompatActivity or its parent the FragmentActivity. This limitation exists since we would need to add the MediaRouteButton (provided in the MediaRouter support library) as an MediaRouteActionProvider and this will only work if the activity is inheriting from the above-mentioned classes."
Are you sure you have inherited FragmentActivity or AppCompatActivity?
If you ever got it to work let me know, I want it to work this way too.

.init(activity); is showing error [android]

i am trying to implement ShineButton in my project . I have successfully synced the library to the gradle and added shine button in the xml.
now when i am trying to write the java code
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button)findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Kill bill", Toast.LENGTH_SHORT).show();
}
});
ShineButton shineButton = (ShineButton) findViewById(R.id.po_image2);
shineButton.init(context);
}
}
.init(activity); is showing cannot resolve symbol activity.
You don't literally copy the code verbatim, you read the documentation and object types supported by the method.
public void init(Activity activity) {
For example, I assume you are running that from an activity based on the usage of findViewById? Then you need "this instance of the Activity"
shineButton.init(this);
or an instance of an Activity if you were in a Fragment
shineButton.init(getActivity());
If the code is in Activity use shineButton.init(Activityname.this).
If it is in
fragment use shineButton.init(getActivity()).
Read this: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/ and this https://developer.android.com/training/index.html
Change:
shineButton.init(context);
To:
shineButton.init(MainActivity.this);
MainActivity.this holds the instance of MainActivity class and can be used to initialise the view.

Error with setOnClickListener(this)

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
}
}

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

Activity instance remains in the memory after onDestroy()

I know that this topic has been already beaten enough, but I still don't understand completely if Android System has fine behavior in following case:
I created small app consists of two classes, here is the code:
Main.java
public class Main extends Activity {
private Button bv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bv = (Button) findViewById(R.id.hello_txt);
bv.setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Main.this, Main2.class);
startActivity(i);
}
}
);
}
}
Main2.java
public class Main2 extends Activity {
private TextView countOfActivities;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
countOfActivities = new TextView(this);
setContentView(countOfActivities);
countOfActivities.setText("Count of Activities: " + getInstanceCount());
}
}
When I clicked on the button from first activity several times, I get that even after pressing BACK button that should call second Activity's onDestroy() it's instance remains in the memmory.
Only after creating about 35 instances next click let me know, that GC cleared the memmory.
I just want to completely be sure that it is normal system's behavior.
Following pictures from Emulator and LogCat
Button clicked 10 times
LogCat output after clicked
Yes, the system works fine.
When you press the back button, your activity is removed from the activity stack.
onDestroy() may have been called, this doesn't mean that the instance was actually unallocated from the memory.

Categories

Resources