• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Toast attempted but no coffee

oosgearoo

Newbie
Hi
The following crashes and burns when the button is pressed, I suspect Line 34/35 to be at fault.
Can someone take note of line 36 and explain context for me?
[HIGH]package com.example.tostertime;

import java.util.Calendar;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
Button ToTime;
Button button1;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ToTime =(Button) this.findViewById(R.id.button1);
ToTime.setOnClickListener(new View.OnClickListener()

{

@Override
public void onClick(View v)
{
ToTime.setText("onRun");
String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
Context context = null;
Toast.makeText(context,"Time "+mydate,5);
//Can someone explain ^^ Context what's acceptable
}
});
}
@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;
}

}
[/HIGH]
Hope these posts are ok here their not being moved so I guess so.
 
Use getActivity() to get context.
Thanks

I still get fail on a 'hardware device' ToasterTime has stopped

when I track it down by commenting lines it fails on

[HIGH] Toast toast=Toast.makeText(context,msg,duration);[/HIGH]Full relevant snip-it

[HIGH] Context context = getActivity();
String msg = "Some Text";
int duration =Toast.LENGTH_LONG;



Toast toast=Toast.makeText(context,msg,duration);
//toast.show();



//context = getActivity();
//Toast.makeText( (Context) getActivity(),"Time "+mydate,LENGTH_SHORT);
//Toast.show();
//Can someone explain ^^ Context what's acceptable
}

private Context getActivity() {
// TODO Auto-generated method stub
return null;
}[/HIGH]This is how its laid out in a book which wont compile
[HIGH] Context context = this;
String msg = "Some Text";
int duration =Toast.LENGTH_LONG;


Toast toast=Toast.makeText(context,msg,duration);
toast.show();


//context = getActivity();
//Toast.makeText( (Context) getActivity(),"Time "+mydate,LENGTH_SHORT);
//Toast.show();
//Can someone explain ^^ Context what's acceptable
}

private Context getActivity() {
// TODO Auto-generated method stub
return null;
}[/HIGH]
 
Just for others and cleanness This WORK's
[HIGH]package com.example.tostertime;

import java.util.Calendar;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
Button ToTime;
Button button1;
protected Context getActivity;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ToTime =(Button) this.findViewById(R.id.button1);
ToTime.setOnClickListener(new View.OnClickListener()

{
public void onClick(View v)
{
ToTime.setText("onRun");
String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());

Context context = getApplicationContext();
CharSequence text = "Hello toast! "+mydate;
int duration = Toast.LENGTH_LONG;//LENGTH_SHORT

Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
});

};
}
[/HIGH]
 

  1. [HIGH]
  2. public class MainActivity extends Activity {
  3. Button ToTime;
  4. Button button1;
  5. Context context; //declare variable for context

  6. @Override
  7. protected void onCreate(Bundle savedInstanceState)
  8. {
  9. super.onCreate(savedInstanceState);
  10. context = this; //save activity context
  11. setContentView(R.layout.activity_main);
  12. ToTime =(Button) this.findViewById(R.id.button1);
  13. ToTime.setOnClickListener(new View.OnClickListener()
  14. {
  15. @Override
  16. public void onClick(View v)
  17. {
  18. ToTime.setText("onRun");
  19. String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
  20. Toast.makeText(context,"Time "+mydate,5);
  21. //-----------------^ using saved context
  22. }
  23. });
  24. }
  25. @Override
  26. public boolean onCreateOptionsMenu(Menu menu) {
  27. // Inflate the menu; this adds items to the action bar if it is present.
  28. getMenuInflater().inflate(R.menu.main, menu);
  29. return true;
  30. }
  31. }
[/HIGH]
 
Back
Top Bottom