Android: OnResume causes force close - android

I'm trying to make a simple notepad application and I would like to refresh the notes when the New Note activity finishes and the main screen resumes. However I get force close when trying to open the application with this code. If I remove the OnResume thing it doesn't force close. Help?
public class NotePadActivity extends Activity implements View.OnClickListener {
TextView tw;
String data;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tw = (TextView)findViewById(R.id.uusi);
tw.setOnClickListener(this);
Note note = new Note(this);
note.open();
data = note.getData();
note.close();
tw.setText(data);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.uusi:
try {
startActivity(new Intent(PadsterActivity.this, Class.forName("com.test.notepad.NewNote")));
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Note note = new Note(this);
note.open();
data = note.getData();
note.close();
tw.setText(data);
}
}

The problem is you have two different TextViews called tw see my comments on your code...
public class NotePadActivity extends Activity implements View.OnClickListener {
TextView tw; // This never gets instantiated
...
Another here...
public void onCreate(Bundle savedInstanceState) {
...
// This is instantiated but is local to onCreate(...)
TextView tw = (TextView)findViewById(R.id.uusi);
Then in onResume(...) you attempt to use the instance member tw which is null...
protected void onResume() {
...
tw.setText(data);
Change the line in onCreate to...
tw = (TextView)findViewById(R.id.uusi);
...and it should fix the problem.
BTW, you don't need to duplicate everything in onCreate(...) again in onResume() as onResume() is always called after onCreate(...) when an Activity is created.

Related

How to start an Activity and call a method of that class using another class

I want to open a class and a method of that class from another class simultaneously. When i click on button it stops the application. Help!
Method of class 'Fixtures' from which I want to call class and Method
public void onClick(View arg0) {
// TODO Auto-generated method stub
int id = arg0.getId();
FixtureDetails abc = new FixtureDetails();
abc.xyz(id);
startActivity(new Intent(Fixtures.this, FixtureDetails.class));
}
Class and method which I want to be opened
public class FixtureDetails extends Activity{
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fixturedetails);
tv = (TextView) findViewById(R.id.tv);
}
void xyz(int lmn)
{
switch(lmn)
{
case R.id.tvMatch1:
tv.setText("Hey there, wassup");
break;
}
}
}
Because Android handles the lifecycles of Activity classes it's not recommended to instantiate it directly, and calling that method like you are Android will recreate the class anyways destroying anything you changed in it.
The recommended way to do what you want is to use Intent Extras to pass data to your Activity.
public void onClick(View arg0) {
// TODO Auto-generated method stub
int id = arg0.getId();
Intent intent = new Intent(Fixtures.this, FixturesDetails.class);
intent.putExtra("id_key", id); // Set your ID as a Intent Extra
startActivity(intent);
}
FixtureDetails.class
public class FixtureDetails extends Activity{
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fixturedetails);
tv = (TextView) findViewById(R.id.tv);
Intent intent = getIntent();
if(intent != null && intent.getExtras() != null) {
xyz(intent.getIntExtra("id_key", -1)); // Run the method with the ID Value
// passed through the Intent Extra
}
}
void xyz(int lmn) {
switch(lmn) {
case R.id.tvMatch1:
tv.setText("Hey there, wassup");
break;
}
}
}

Difference between oncreate and onRestoreInstanceState method in activity

Guys I am stuck in a problem with these two methods:-
When I change the orientation of device and set the text in edit text after retriving the text from bundle,it does't work.But the same code is working in onrestoreStoreInstante method.
Please have a look at my code:-
public class LifeCycleActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
EditText user;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
user=(EditText)findViewById(R.id.et_user);
if(savedInstanceState!=null){
String s =savedInstanceState.get("Key").toString();
user=(EditText)findViewById(R.id.et_user);
user.setText(savedInstanceState.get("Key").toString());
Toast.makeText(this, s,Toast.LENGTH_SHORT).show();
}
Toast.makeText(this, "onCreate",Toast.LENGTH_SHORT).show();
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(this);
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Toast.makeText(this, "onSaveInstanceState",Toast.LENGTH_SHORT).show();
outState.putString("Key", "Deepak");
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
//String s =savedInstanceState.get("Key").toString();
//user.setText(s);
Toast.makeText(this, "onRestoreInstanceState",Toast.LENGTH_SHORT).show();
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Toast.makeText(this, "onStart",Toast.LENGTH_SHORT).show();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Toast.makeText(this, "onResume",Toast.LENGTH_SHORT).show();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Toast.makeText(this, "onPause",Toast.LENGTH_SHORT).show();
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Toast.makeText(this, "onStop",Toast.LENGTH_SHORT).show();
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(this, "onDEstroy",Toast.LENGTH_SHORT).show();
}
#Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Toast.makeText(this, "onRestart",Toast.LENGTH_SHORT).show();
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent(LifeCycleActivity.this,SecondActivity.class));
}
}
when I set the text in edit text in oncreate method after getting the value from Bundle,it doesn't work.But the same code works in onRestoreInstanceState() method.
According to me , it should work for oncreate also as we can get the Bundle class object there.
Please help me to sort out this problem..
EditText and most of other views have their own methods to save/restore their own data. So in general it's not necessary to save/restore them on your code.
You can see this here on the Android TextView source code (remember that EditText extends from TextView) on line 3546:
if (ss.text != null) {
setText(ss.text);
}
so the reason you can't set it onCreate and can do it onRestoreInstanceState it's because during your activity super.onRestoreInstanceState(savedInstanceState) the activity calls the EditText.onRestoreInstanceState and the EditText restore it self to the previous value.
You can see it happening on the Activity source code on line 940
protected void onRestoreInstanceState(Bundle savedInstanceState) {
if (mWindow != null) {
Bundle windowState = savedInstanceState.getBundle(WINDOW_HIERARCHY_TAG);
if (windowState != null) {
mWindow.restoreHierarchyState(windowState);
}
}
}
hope it helps.
Overview
onSaveInstanceState() is used to put the data to bundle
onRestoreInstanceState() is used to set the data available in
bundle to your activity
So do as follows:
i Guess until the activity is created onorientation change the
bundle is not available, that's way you are not able to retrieve it
in oncreate
Let the activity create in oncreate
Then restore the instance in onRestoreInstanceState
Hope this helps !

BadTokenException in Asynctask progress dialog

i'm new to android,this maybe simple question but i cant able to find the answer for it,i'm using tabactivity while changing the activities the bottom tab's are missing so i
used below code to hold the tab by changing the view,so it works fine
public void replaceContentView(String id, Intent newIntent) {
try {
#SuppressWarnings("deprecation")
View view = getLocalActivityManager().startActivity(id,
newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
this.setContentView(view);
} catch (Exception e) {
e.printStackTrace();
}
}
Actually my problem is i want to select something from 2nd activity to 1st ActivityGroup,in the 2nd activity i have one button called done
Here is my 2nd activity
public class Woosuite_Twiter_Feed_list extends Activity implements
OnItemClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.woosuite_twitlist);
Button done = (Button) findViewById(R.id.btn1);
done.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(v.getContext(), Woosuite_Feed.class);
i.setClass(getParent(), Woosuite_Feed.class);
Woosuite_Feed feed = (Woosuite_Feed) getParent();
feed.replaceContentView("one", i);
}
});
while clicking done button in 2nd activity then first activitygroup asyncTask class is called,in that i'm getting
Android: “BadTokenException: Unable to add window; is your activity running?
i dont know how to solve this,please provide some solution.
here is my 1st ActivityGroup oncreate
public class Woosuite_Feed extends ActivityGroup implements OnClickListener
{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.woosuite_feed);
LinkedFeeds feeds = new LinkedFeeds();
feeds.execute("");
}
Here is my 1st ActivityGroup AsyncTask
public class LinkedFeeds extends AsyncTask<String, Void, String> {
public ProgressDialog dialog;
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
if(!isFinishing()){
dialog = new ProgressDialog(Woosuite_Feed.this);
dialog.setMessage("Loading...");
dialog.setCancelable(false);
dialog.show();
}
super.onPreExecute();
}
#Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
return null;
}
#Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
dialog.dismiss();
}
}
Thanks

how do i update the database table on the basis of checkbox/unchecked

how can i retrieve the checkboxes value to update the database table on pressing the update button...
TeacherLoggedInClass
public class TeacherLoggedInPage extends Activity implements OnClickListener {
Button UpdateAttendanceButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.attendancesheet);
UpdateAttendanceButton = (Button) findViewById(R.id.bUpdateAttendance);
UpdateAttendanceButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
The isChecked() method return a boolean value with the information you need.
I would also suggest you use another way with implementing the Listener. This is what Google recommends:
public class MyActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.content_layout_id);
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click... for example your checkBox.isChecked()
}
});
}
If you want to get the single checkbox value and set database, you can follow below code,
public class TeacherLoggedInPage extends Activity implements OnClickListener {
Button UpdateAttendanceButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.attendancesheet);
UpdateAttendanceButton = (Button) findViewById(R.id.bUpdateAttendance);
UpdateAttendanceButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (cbBox.isChecked()) {
// do your operation
} else {
// do your operation
}
}
}
if you need to use a list of checkbox status to update databases, you'd better to create a collection to cache every checkbox status , and with bulkInsert or transaction to commit.

Android: How do you start a wallpaperservice from a button?

I have a test live wallpaper that I wish to start from an activity. At the moment, it immediately finds itself in the live wallpaper chooser as I run the project on my emulator. Having searched everywhere, I still can't find a way to make it show up there only after a button in my main activity is pressed.
I tried introducing a static boolean in my main activity, isnotpressed, and setting it to false in the button onClick(). Then I used stopSelf() in my wallpaperservice class while isnotpressed is true. Unfortunately, none of this was to any avail and I don't think I'm going about it in the right way anyway. I also tried this, but it didn't work either. Any help would be deeply appreciated here.
Here's the main activity:
public class TestProjectActivity extends Activity{
static boolean isnotPressed;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
isnotPressed = true;
final Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
isnotPressed = false;
}
});
}
}
And here's part of the wallpaperservice:
public class MyWallpaperService extends WallpaperService {
#Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
while(TestProjectActivity.isnotPressed){stopSelf();}
}
Try this :
Intent i = new Intent();
if (Build.VERSION.SDK_INT > 15)
{
i.setAction(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
String pkg = MyPreferencesActivity.class.getPackage().getName();
String cls = MyPreferencesActivity.class.getCanonicalName();
i.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT, new ComponentName(pkg, cls));
}
else
{
i.setAction(WallpaperManager.ACTION_LIVE_WALLPAPER_CHOOSER);
}
startActivityForResult(i, 0);

Categories

Resources