listView stop responding after back from webview - Java for Android App Development

HI, Guys,
I am a newbie, now i am developing a google rss reader for practicing. I used listview to display all the news titles. and webview to view the contents. Every time when i am back from webview, it shows this error:
Error: WebView.destroy() called while still attached!
I overwritten the onBackPressed() in webview activity like this like suggested on codeoverflow:
 @override
public void onBackPressed() {
super.onBackPressed();
if (mWebView != null) {
WebView w = mWebView;
w.removeAllViews();
w.destroy();
w = null;
mWebView = null;
}
}
but it doesn't work. everytime it still shows the same error.
meanwhile, when i am back from the webview, the pressing the items in the main listview doesn't redirect me to the webview activity anymore.
Thanks a lot if someone can give me guidance on this.

reply
crazybeeliuzhe said:
HI, Guys,
I am a newbie, now i am developing a google rss reader for practicing. I used listview to display all the news titles. and webview to view the contents. Every time when i am back from webview, it shows this error:
Error: WebView.destroy() called while still attached!
I overwritten the onBackPressed() in webview activity like this like suggested on codeoverflow:
@override
public void onBackPressed() {
super.onBackPressed();
if (mWebView != null) {
WebView w = mWebView;
w.removeAllViews();
w.destroy();
w = null;
mWebView = null;
}
}
but it doesn't work. everytime it still shows the same error.
meanwhile, when i am back from the webview, the pressing the items in the main listview doesn't redirect me to the webview activity anymore.
Thanks a lot if someone can give me guidance on this.
Click to expand...
Click to collapse
the error i see after scroll is :
at android.support.v4.view.ViewPager.onPageScrolled(ViewPager.java:1708)
at android.support.v4.view.ViewPager.pageScrolled(ViewPager.java:1646)
anyone has similar experience?

I don't know whether the destroy method is needed. However, I recommend to put that code into the onDestroy() method instead of the onBackPressed() method. Might as well solve the problem.

the same problem
nikwen said:
I don't know whether the destroy method is needed. However, I recommend to put that code into the onDestroy() method instead of the onBackPressed() method. Might as well solve the problem.
Click to expand...
Click to collapse
Thanks Nikwen, but it is still the same after i overwritten onDestroy():
 @override
protected void onDestroy() {
super.onDestroy();
if (mWebView != null) {
WebView w = mWebView;
w.removeAllViews();
w.destroy();
w = null;
mWebView = null;
}
int myPid = android.os.Process.myPid();
Log.d(fTag, "exit.myPid = " + myPid);
android.os.Process.killProcess(myPid);
}
seems the problem doesn't come form there.

crazybeeliuzhe said:
Thanks Nikwen, but it is still the same after i overwritten onDestroy():
@override
protected void onDestroy() {
super.onDestroy();
if (mWebView != null) {
WebView w = mWebView;
w.removeAllViews();
w.destroy();
w = null;
mWebView = null;
}
int myPid = android.os.Process.myPid();
Log.d(fTag, "exit.myPid = " + myPid);
android.os.Process.killProcess(myPid);
}
seems the problem doesn't come form there.
Click to expand...
Click to collapse
My first reaction is the same as nikwen's - do you need to do this at all ?
If you do, then it sounds as though the error is arising because your WebView is still attached to something at the point you are attempting to destroy it. So before you can destroy it you'll need to call removeView() on whatever the parent is (probably a layout, I guess).

thanks i will try
PicomatStudios said:
My first reaction is the same as nikwen's - do you need to do this at all ?
If you do, then it sounds as though the error is arising because your WebView is still attached to something at the point you are attempting to destroy it. So before you can destroy it you'll need to call removeView() on whatever the parent is (probably a layout, I guess).
Click to expand...
Click to collapse
Thanks. Then i think i should also call the removeView() before i try to destroy it. I will keep trying and let you guys know my update.

app uploaded
crazybeeliuzhe said:
Thanks. Then i think i should also call the removeView() before i try to destroy it. I will keep trying and let you guys know my update.
Click to expand...
Click to collapse
i have finished my toy app. this is a Google RSS news reader. A good practice for me. You can now download it from google play by searching my name crazybeeliuzhe. you will see two app from me. one is the ipcam viewer i made the other is the news reader.
I will upload my code later to source forge.
your comments are always welcome.

Related

New Developer - Need Some Advise on RSS Code

Hello,
I'm new to developing software in Java, but especially with Android. I am going through a tutorial on how to create an RSS Feed Reader and so far I've got it mostly working, but I've got a few bugs I can't seem to figure out.
I wanted to post images, but it wouldn't let me, but anyway:
main activity - title, pubdate both work, as does each article's title.
after clicking on a title, the other activity is launched and the article is shown:
showdescription activity is launched - title, pubdate, link all work and display correctly.
bug: description does not always display everything or even display at all depending on the xml source.
I'm wondering if my problem is somewhere these code snippets, as this tutorial was written some time ago.
These are pretty basic, so I'm doubting the problem is in here.
RSSHandler said:
if (localName.equals("description"))
{
currentstate = RSS_DESCRIPTION;
return;
}
...
case RSS_DESCRIPTION:
_item.setDescription(theString);
currentstate = 0;
break;
Click to expand...
Click to collapse
RSSItem said:
void setDescription(String description)
{
_description = description;
}
...
String getDescription()
{
return _description;
}
Click to expand...
Click to collapse
This is where I believe the problem exists, in the onItemClick method or the ShowDescription class.
RSSReader said:
public void onItemClick(AdapterView parent, View v, int position, long id)
{
Log.i(tag,"item clicked! [" + feed.getItem(position).getTitle() + "]");
Intent itemintent = new Intent(this,ShowDescription.class);
Bundle b = new Bundle();
b.putString("title", feed.getItem(position).getTitle());
b.putString("description", feed.getItem(position).getDescription());
b.putString("link", feed.getItem(position).getLink());
b.putString("pubdate", feed.getItem(position).getPubDate());
itemintent.putExtra("android.intent.extra.INTENT", b);
//startSubActivity(itemintent, 0);
startActivity(itemintent);
}
Click to expand...
Click to collapse
ShowDescription said:
public class ShowDescription extends Activity
{
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.showdescription);
String theStory = null;
Intent startingIntent = getIntent();
if (startingIntent != null)
{
Bundle b = startingIntent.getBundleExtra("android.intent.extra.INTENT");
if (b == null)
{
theStory = "bad bundle?";
}
else
{
theStory = b.getString("title")
+ "\n\n" + b.getString("pubdate")
+ "\n\n" + b.getString("description")
+ "\n\nView Website:\n" + b.getString("link");
}
}
else
{
theStory = "Information Not Found.";
}
TextView db= (TextView) findViewById(R.id.storybox);
db.setText(theStory);
Button backbutton = (Button) findViewById(R.id.back);
backbutton.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
finish();
}
});
}
}
Click to expand...
Click to collapse
Originally the tutorial called for "startSubActivity" for starting the ShowDescription activity, but that doesn't seem to exist anymore?
The XML source I'm using can't be displayed because I can't link in outside pages, but I can PM it to you if you'd like.
I have tried using a .replaceAll("\\<.*?>","") but it didn't seem to change much about whether the <description> tag's content is displayed or not.
Anyway, if anyone takes the time to look at this, it'd be greatly appreciated, and thank you!
If you need me to post anymore code let me know.
So I think I've figured it out, well somewhat.
While stepping through the code, the <description> tag's content consists of a single < character. Then the parser goes through two more tags that contain, p and > before actually hitting the content I want.
So it looks something like this:
<description>
<
<?>
p
<?>
>
<?>
"wanted content"
</?>
</?>
</?>
</description>
Click to expand...
Click to collapse

[Q] timer keeps going after app has been terminated?

I am making an app that plays sounds in a loop based on a timer. after I exit the program using finish(); the sounds keep playing in background anyway. Any ideas of why this is and how I can fix it (besides having to use a task killer every time)
thanks
hyperbyteX said:
I am making an app that plays sounds in a loop based on a timer. after I exit the program using finish(); the sounds keep playing in background anyway. Any ideas of why this is and how I can fix it (besides having to use a task killer every time)
thanks
Click to expand...
Click to collapse
finish() just closes the activity, it does *NOT* close the app. Threads will continue to run, services will continue to run, broadcast receivers will continue to receive, etc...
You need to actually stop/cancel the timer if you want it to stop.
I recommend reading this (and then re-reading it again): http://developer.android.com/guide/topics/fundamentals.html and this http://developer.android.com/guide/topics/fundamentals/processes-and-threads.html
hyperbyteX said:
I am making an app that plays sounds in a loop based on a timer. after I exit the program using finish(); the sounds keep playing in background anyway. Any ideas of why this is and how I can fix it (besides having to use a task killer every time)
thanks
Click to expand...
Click to collapse
The timer probably uses the system timer service, Which calls your sound file again and again.
Stop the timer in the onDestory function of your activity.
how exactly do I stop it?
hyperbyteX said:
how exactly do I stop it?
Click to expand...
Click to collapse
Take a look at the documentation from google. probably something like .stop(timer)
don't know
heres the code Im using:
// set timer
TimerTask scanTask = null;
final Handler handler = new Handler();
Timer t = new Timer();
scanTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
// stuff to run goes here
}
}
});
}};
t.schedule(scanTask, 1, 50); // time how often stuff happens
I've tried many ways to kill it but nothing seems to work
hyperbyteX said:
heres the code Im using:
// set timer
TimerTask scanTask = null;
final Handler handler = new Handler();
Timer t = new Timer();
scanTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
// stuff to run goes here
}
}
});
}};
t.schedule(scanTask, 1, 50); // time how often stuff happens
I've tried many ways to kill it but nothing seems to work
Click to expand...
Click to collapse
Did you try TimerTask .cancel(); ?
Dark3n said:
Did you try TimerTask .cancel(); ?
Click to expand...
Click to collapse
yeah I tried that but it gives me an error saying "Cannot make a static reference to the non-static method cancel() from the type TimerTask"
Ok, thank you very much. I had to tweak it a bit but I got it!

[Q] DatePickerDialog cancelclick

Hi there!
I have been trying to catch the Cancel click of a DatePickerDialog, because I want to do some additional stuff, when the user clicks on the Cancel Button.
I tried it like described in the second answer from esilver from this Question:
http://stackoverflow.com/questions/...erner-of-datepicker-dialog?tab=active#tab-top
But I can't get it to work like that. When do I have to call this onClick method?
Would be great if someone could help me with that!
Thanks!
cTrox said:
Hi there!
I have been trying to catch the Cancel click of a DatePickerDialog, because I want to do some additional stuff, when the user clicks on the Cancel Button.
I tried it like described in the second answer from esilver from this Question:
http://stackoverflow.com/questions/...erner-of-datepicker-dialog?tab=active#tab-top
But I can't get it to work like that. When do I have to call this onClick method?
Would be great if someone could help me with that!
Thanks!
Click to expand...
Click to collapse
the "checked" solution in that example seems wrong to me. but the second one people voted up seems correct.
You can also set the onDissmissListener which will catch if the user backs out with the back key ( recommended for user friendliness )
have a look here:
http://developer.android.com/refere...id.content.DialogInterface.OnDismissListener)
Also, since DatePickerDialog is a subclass of AlertDialog, you can set the buttons the same way:
http://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog
That should get you started but feel free to post back if you get stuck again. And post the code you are using.
Also, one other thing, it might be useful to keep a private reference to your dialog in your activity class.
All those examples (in the API docs and tutorials) always show a new dialog created when "onCreateDialog(int ID)" is called by the OS on your activity and they never save any sort of reference to it. They give you just enough code to hang yourself
Anyways, while this is a perfectly normal way to do things, it doesnt give you a chance to follow what is actually happening with the dialog. It also makes it harder to reference your dialog from elsewhere in the activity.
Keeping a reference, and exploring the onPrepareDialog(int ID) method are good for learning what the OS is doing with your dialog. (IMHO)
hth
Thanks a lot for your answers. But I still can't figure out how to do it.
Here's my current Code:
Code:
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker datePicker, int year, int monthOfYear,
int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
// do some more stuff...
}
};
Code:
protected Dialog onCreateDialog(int id) {
Calendar cDate = Calendar.getInstance();
int cyear = cDate.get(Calendar.YEAR);
int cmonth = cDate.get(Calendar.MONTH);
int cday = cDate.get(Calendar.DAY_OF_MONTH);
switch(id){
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, cyear, cmonth, cday);
}
return null;
}
With that I can just call showDialog(DATE_DIALOG_ID); and I get the dialog. Now, where do I have to implement this OnDismissListener and how?
Thanks!
there are lots of ways to do this but I broke it out into several parts so hopefully it seems more obvious what is happening.
Code:
//here's our field reference we could use later or reuse or whatever
private DatePickerDialog dateDialog = null;
protected Dialog onCreateDialog(int id)
{
//your calendar code here... just removed to save space
switch(id)
{
case DATE_DIALOG_ID:
dateDialog = new DatePickerDialog(this, mDateSetListener, cyear, cmonth, cday);
dateDialog.setButton ( DialogInterface.BUTTON_NEGATIVE, android.R.string.cancel, cancelBtnListener );
dateDialog.setOnDismissListener ( listener );
break;
}
return dateDialog;
}
//our dismiss listener
protected DialogInterface.OnDismissListener dismissListener = new OnDismissListener( )
{
@Override
public void onDismiss ( DialogInterface dialog )
{
// do your thang here
}
};
//our click listener
protected DialogInterface.OnClickListener cancelBtnListener = new OnClickListener( )
{
@Override
public void onClick ( DialogInterface dialog, int which )
{
dialog.dismiss ( );
// since we dismiss here, the next listener to get called
// is the dismiss listener. now we'll have consistent behavoir
}
};
Ah thank you very much! I was always confused, where to set the Button and the OnDismissListener.
It works perfectly like that!

Preference changes only happen when restarting the app

Hey there! I was coding an app I added a preferences menu, and it works, but changes happen only when I restart the application,anyone knows how to make changes happen whithout exitting the app??? Thanks in advance
My code ( from main activity):
preferencias = preferenceManager.getDefaultSharedPreferences(TimeToSpeechActivity.this);
OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
//nothing here, do I have to put anything?
}
};
preferencias.registerOnSharedPreferenceChangeListener(listener);
getPrefs();
changefont(fuente, letra);
if (boole == true) {fontcolors();}
private void getPrefs(){
fuente = Typeface.createFromAsset(getAssets() , preferencias.getString("elegirfuente", "fonts/Default.ttf"));
letra = Integer.parseInt(preferencias.getString("fontstyle", "0"));
bol = preferencias.getBoolean("randomcolors", true);
}
Click to expand...
Click to collapse
I have nothing put in preference activity, do i have to put anything?
Also, do I have to edit this?: (SharedPreferences prefs, String key) I ask this because i havent created prefs and key varibles
Thanks in advance!!!
It should update for you if you put getPrefs() inside the sharedpreferences listener
Sent from my Xoom using xda premium
thanks, but if I do that I get a nullpointer exception, I think its caused because the params I use to set "changefont(fuente, letra);" and "if (boole == true) {fontcolors();}" have null value....another idea ,please? I am breaking my head with this...
My updated code:
preferencias = preferenceManager.getDefaultSharedPreferences(Time ToSpeechActivity.this);
OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener () {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
getPrefs();
}
};
preferencias.registerOnSharedPreferenceChangeListener(listener);
changefont(fuente, letra);
if (boole == true) {fontcolors();}
private void getPrefs(){
fuente = Typeface.createFromAsset(getAssets() , preferencias.getString("elegirfuente", "fonts/Default.ttf"));
letra = Integer.parseInt(preferencias.getString("fontstyle ", "0"));
bol = preferencias.getBoolean("randomcolors", true);
}
Click to expand...
Click to collapse
i tried to put changefont and fontcolors functions inside the sharedpreference listener... but if i do, that options are not set... so I think there is a problem with the listener...why???
I will add the code of my preference class:
public class PantallaOpciones extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.opciones);
}
}
Click to expand...
Click to collapse
One way ive always done preference updates was to use a dedicated method for updating (like you have with openPrefs()) then calling it after operations:
runOperation(){
updatePrefsMethod();
getPrefsMethod();
}
If i was near my pc i could give you a more solid example, but alas, im stuck at work lol
Also, use the stacktraces to check which line gives it null (itll say something like "at com.yourapp.identifier(offending Class - line number)
maybe if you a null check on the boolean for color that sets it false, you may not get the nullPointerException
Sent from my Xoom using xda premium
z3nful said:
One way ive always done preference updates was to use a dedicated method for updating (like you have with openPrefs()) then calling it after operations:
runOperation(){
updatePrefsMethod();
getPrefsMethod();
}
If i was near my pc i could give you a more solid example, but alas, im stuck at work lol
Also, maybe if you a null check on the boolean for color that sets it false, you may not get the nullPointerException
Sent from my Xoom using xda premium
Click to expand...
Click to collapse
OK, could you give me an example as soon as you can,please?

[Q] onSaveInstanceState

I'm new to android app development
Facing an issue:
Data (String values) lost while changing screen orientation
I googled about it and found it can be handled by onSaveInstanceState()
But didn't get how to use that in the java file
Can someone clear it to me?
static final String STATE_SCORE = "playerScore";
static final String STATE_LEVEL = "playerLevel";
...
@override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the user's current game state
savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
 @override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // Always call the superclass first
// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
// Restore value of members from saved state
mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
} else {
// Probably initialize members with default values for a new instance
}
...
}
ChahatGupta said:
I'm new to android app development
Facing an issue:
Data (String values) lost while changing screen orientation
I googled about it and found it can be handled by onSaveInstanceState()
But didn't get how to use that in the java file
Can someone clear it to me?
Click to expand...
Click to collapse
If It s so important then save it as file
Or save it as temporary data to Bundle
Code:
Bundle.putExtra("name", Data);
:good:

Categories

Resources