I'm using the sample Google provides, FragmentTabsPager to work with tabs in my application. I'm having a lot of trouble switching the default class for my own, which is nothing more than Preferences right now. Can anyone offer some suggestions?
From the sample:
Code:
mTabsAdapter.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
LoaderCursorSupport.CursorLoaderListFragment.class, null);
My class:
Code:
public class Preferences extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
I think the "trick" they use to connect ViewPager with TabHost isn't set up correctly to handle Intents, but I could be wrong. I'm just unsure how to get setContent working with these tabs. Any thoughts?
Related
I have 2 classes in one .java file and it runs fine without errors or anything (the second class is used as a timer and changes variables every second) everything works but it wont call methods properly. Any idea of why this would be??? Heres my code of the second class.
Code:
class MyTime extends TimerTask{
//java.text.DateFormat format = SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM, Locale.getDefault());
public game timecall2= new game();
public MyTime(Context ctx) {
// create internal instance
Context ctx2;
}
@Override
public void run() {
game.sec--;
if(game.sec==-1){game.sec=59;game.min--;}
game.Title2(); // set text in title bar
}
}
It would be easier if you explain more cleary, what you want to do.
Or post more code.
public game timecall2= new game();
I think, game is your first class?
Then you want to use the variable sec of the class game?
-> are they declared as static or why you don't call it over the object timecall2 you created?
Sry, but without more Code/Information to unterstand your problem, it's difficult to help. Also don't know, how skilled your are in programming.
*game* is the first class
I've tried calling the method through *timecall2* object but it doesn't help.
I've tried declaring the function as both static and no-static but nothing seems to change.
I've got lots of programming experience in other languages, but I'm less experienced with Java
And what's going wrong now?
Why don't you debug trough the code. Should be the easiest ways to find the problem.
You generally should use getters and setters for accessing variables inside of another class. Meaning that you have a method to set the value of the variable and a method to retrieve the value
Code:
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MyClass extends Activity {
private static int mMin = 0;
private static int mSec = 0;
private TextView titleText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView titleText = (TextView) findViewById(R.id.titleText);
}
public static int getSec() {
return mSec;
}
public static void setSec(int newVal) {
mSec = newVal;
}
public static int getMin() {
return mMin;
}
public static void setMin(int newVal) {
mMin = newVal;
}
public static void setTitleText(String newVal) {
if (titleText != null) {
titleText.setText(newVal);
}
}
}
Notice the "static" modifier of the class methods.
Generally, you wouldn't instantiate this activity class. Especially if it already lives in memory. What I am guessing that you are doing here is that "game" is your activity and that MyTime is an object that you are using from inside of the activity and that you need to modify variables that live in the main Activity.
If that is the case then you would just do something like this:
Code:
import java.util.TimerTask;
import com.myapp.game.MyActivity;
class MyTime extends TimerTask {
private int sec = 0;
private int min = 0;
@Override
public void run() {
sec = MyActivity.getSec();
min = MyActivity.getMin();
sec--;
if(sec==-1) {
sec=59;
min--;
}
String title = String.valueOf(min)+":"+String.valueOf(sec); //obviously you need formatting
MyActivity.SetTitle(title); // set text in title bar
MyActivity.setSec(sec);
MyActivity.setMin(min);
}
}
That said... I just did the above as an example. This is entirely the wrong way to make a game timer. There is a better example here
From what I remember from C++, you would have a "base" class and then other classes beneath that base class.
To use the base class methods, the other classes had to be "friends". I'm a little rusty on my JAVA syntax...is "extends" the same thing as a friend class?
Java doesn't have friend functions, and "extends" means that the class is a subclass of whatever its extending.
To access a function/variable between classes that said function/variable must be static. Beware tho when do this depending on you implementation you must check for null variables. Since its static you can access you dont need a class object to access them through.
ak. SomeClass.function();
instead of using SomClass sc = new SomeClass(); sc.function();
since u can access it at any time its contents may not be initialized and be null. So ALWAYS check for null varaibles! lol. That or u can have one variable of that class to check if its class is initialized. such as..
SomeFunction.isInit();
where isInit(); is
private static initialized = false;
public static boolean isInit()
{
return initialized;
}
where in your onCreate & on onDestroy functions you set the initialized variable accordingly.
or..u could just do if(SomeClass.this!=null) lol :S
/me stops writting wot
Thanks for the input everyone. I've realised the problem (but still can't fix it). I can call methods in other classes from my timer class.... but my main class that has the methods that I need implements OnClickListener (public class game extends Activity implements OnClickListener) so it is ONLY updating the view methods when something is clicked on. How should I go about fixing it so that I can call methods that will update when a timer calls them (e.g. I display the remaining time in the title bar and it doesn't update the current time UNLESS a button is clicked on)
Why not run the timer as a thread in the activity class itself?
I'm wanting to "do something" (actually play an mp3 sound) when someone has finished entering data in an EditText object in my activity, but I'm not sure how to tell when this has occurred. In VB, there is a event called lostfocus, but I don't know how to do it in java. I'm guessing that it has something to do with OnClickListener to see when the user is actually in the EditText object (let's call it et_01), but how do I know when they've gone to some other object?
I'd appreciate any specific help, as I'm pretty new to java.
How about OnFocusChangeListener?
http://developer.android.com/reference/android/view/View.OnFocusChangeListener.html
I did some searching and it seems like I need to use OnFocusChangeListener. I found this code snippet (below), but I'm too dumb to figure out how to plug it into my app. If my EditText that I'm watching is called et_01, can somebody please tell me 2 things...
1. How do I tie the OnFocusChangeListener to my et_01?
2. Where (specifically) does this code go in my activity? Inside my extends Activity...after the extends Activity...where oh where?
Code:
OnFocusChangeListener focusListener = new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
/* When focus is lost check that the text field
* has valid values.
*/
if (!hasFocus) {
validateInput(v);
}
}
You have to connect a TextView object to your actual textview, then override the OnFocusChangeListener of that textview object.
Here's a complete implementation using your snippit (You have to implement validateInput() yourself):
Code:
package com.myapp.mytest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.TextView;
public class MyTest extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv=(TextView)findViewById(R.id.et_01);
tv.setOnFocusChangeListener(new OnFocusChangeListener(){
@Override
public void onFocusChange(View v,boolean hasFocus){
/* When focus is lost check that the text field
* has valid values.
*/
if (!hasFocus) {
validateInput(v);
}
}
});
}
}
Thank you, thank you, thank you! That is some nice help. I have one follow up question:
In my own activity, I should be able to put the code below in just under my own call: setContentView(R.layout.main);
In other words, I don't need to create a new class to do this. Correct? I apologize for my ignorance.
Code:
TextView tv=(TextView)findViewById(R.id.et_01);
tv.setOnFocusChangeListener(new OnFocusChangeListener(){
@Override
public void onFocusChange(View v,boolean hasFocus){
/* When focus is lost check that the text field
* has valid values.
*/
if (!hasFocus) {
validateInput(v);
}
}
});
Gene Poole said:
You have to connect a TextView object to your actual textview, then override the OnFocusChangeListener of that textview object.
Here's a complete implementation using your snippit (You have to implement validateInput() yourself):
Code:
package com.myapp.mytest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.TextView;
public class MyTest extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv=(TextView)findViewById(R.id.et_01);
tv.setOnFocusChangeListener(new OnFocusChangeListener(){
@Override
public void onFocusChange(View v,boolean hasFocus){
/* When focus is lost check that the text field
* has valid values.
*/
if (!hasFocus) {
validateInput(v);
}
}
});
}
}
Click to expand...
Click to collapse
Yes.
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (stupid forum says my response was too short)
OK, I'm having an issue that I don't understand. In the code below, I'm getting an error on the MediaPlayer mpWeight = MediaPlayer.create(this, R.raw.mppig);
Holding my cursor over .create says:
The method create(Context, int) in the type MediaPlayer is not applicable for the arguments (new View.OnFocusChangeListener(){}, int)
What does that mean, and more importantly, how do I resolve it?
Here's the whole routine:
Code:
TextView tv=(TextView)findViewById(R.id.weight);
tv.setOnFocusChangeListener(new OnFocusChangeListener(){
@Override
public void onFocusChange(View v,boolean hasFocus){
/* When focus is lost check that the text field
* has valid values.
*/
if (!hasFocus) {
float tempweight = Float.parseFloat(et_weight.getText().toString());
if(tempweight > 200){
MediaPlayer mpWeight = MediaPlayer.create(this, R.raw.mppig);
mpWeight.start();
}
}
}
});
It has to do with the "this" pointer you are passing to the create() method. Since you are creating this within the OnFocusChangeListener() class, that it the "this" pointer. OnFocusChangeListener() does not resolve to a type "context" whereas if you'd created your media player within your Activity, Activity does resolve to a context.
To resolve this, make a class member of your activity that keeps a copy of the context. Assign it in the activity's OnCreate:
Code:
public class MyTest extends Activity {
[COLOR="blue"]protected Context mContext=null;[/COLOR]
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
[COLOR="Blue"]mContext=this;[/COLOR]
TextView tv=(TextView)findViewById(R.id.et_01);
tv.setOnFocusChangeListener(new OnFocusChangeListener(){
@Override
public void onFocusChange(View v,boolean hasFocus){
/* When focus is lost check that the text field
* has valid values.
*/
if (!hasFocus) {
float tempweight = Float.parseFloat(et_weight.getText().toString());
if(tempweight > 200){
MediaPlayer mpWeight = MediaPlayer.create([COLOR="blue"]mContext[/COLOR], R.raw.mppig);
mpWeight.start();
}
}
}
});
Gene, you are awesome! Thanks again for the help.
As with many things in life, this has led me to a new problem, that I do believe will be the end of this subject, if I can get it resolved.
In my activity, I have a few EditText objects mixed in with a few Spinner objects. The new problem is that if you have your cursor in an EditText object and the next item in the activity is a Spinner, the focus doesn't leave the EditText when the Spinner is selected. The focus of any of my EditText objects only leaves those objects IF the next thing selected is another EditText.
I'm unsure how to resolve this. Is there possibly a way to force the focus out of the EditText once a Spinner (or any other object) is touched? I would have thought that would happen automatically, but it doesn't seem to be doing so.
Gene Poole said:
It has to do with the "this" pointer you are passing to the create() method. Since you are creating this within the OnFocusChangeListener() class, that it the "this" pointer. OnFocusChangeListener() does not resolve to a type "context" whereas if you'd created your media player within your Activity, Activity does resolve to a context.
To resolve this, make a class member of your activity that keeps a copy of the context. Assign it in the activity's OnCreate:
Click to expand...
Click to collapse
I have been using a hand built ListActivity as my AppWidget config Activity but to be honest it looks ****ty and doesnt fit in with the rest of the app. So i wanted to transition over to a PreferenceActivity. i made a stub of a PreferenceActivity that launches the layout just fine when it is the main activity. but when i try to change from my ListActivity to my PreferenceAcitivity i get no launching of PreferenceActivity when i launch my widget. all i get is my widget.
i did a refactoring of my xml files to change the instances of my ListActivity (GITextConfig) to my PreferenceActivity (GITextPreferences).
how can i use a PreferenceActivity as my config?
do i need to have a dummy Activity be the config with a button to launch the PreferenceActivity? cause that works just fine. its lame and shouldnt be that way... nvm that does not work... i just cant get a prefs activity to run from an appwidget
anyone? this is quite annoying... i know it can be done as other widgets like gmail unread count does this. at least it appears to have a preferenceactivity as the configuration
got it to work. i dont know what i was doing before but i think i had a little too much of a stub for it to work. after i implemented the appwidget id verification and created my PreferenceManger and overode the onBackPressed() method it seems to work.... happy and confused.
Code:
public class GITextPreferences extends PreferenceActivity {
private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PreferenceManager localPrefs = getPreferenceManager();
localPrefs.setSharedPreferencesName("GITC_Prefs");
addPreferencesFromResource(R.xml.gitc_preferences);
// Find the widget id from the intent.
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
}
// If they gave us an intent without the widget id, just bail.
if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
finish();
}
}
@Override
public void onBackPressed() {
// Make sure we pass back the original appWidgetId
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}
}
Hello, I'm extremely new to both android application developing, and this forum.
I am trying to open another screen ( activity ) when a button ( readyButton ) on the splash screen is pressed. I've tried at least ten different times with different tutorials to no avail, this is my current code which didn't work, and instead forces the app to crash.
Code:
public class MainActivity extends Activity {
// Called when the activity is first created.
[user=439709]@override[/user]
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
OnClickListener listener = new OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
Intent intent = new Intent("SecondActivity");
startActivity(intent);
}
};
Button btn = (Button) findViewById(R.id.readybutton);
btn.setOnClickListener(listener);
}
}
Please help.
The button's name is 'readybutton'
the second activity name is 'SecondActivity'
also, where am I supposed to put this code into the java class? Here is how it is currently set up:
Code:
package com.unchat;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
// Default Items
public class FirstActivity extends Activity {
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
}
/** New button code start */
public class MainActivity extends Activity {
// Called when the activity is first created.
[user=439709]@override[/user]
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
OnClickListener listener = new OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
Intent intent = new Intent("SecondActivity");
startActivity(intent);
}
};
Button btn = (Button) findViewById(R.id.readybutton);
btn.setOnClickListener(listener);
}
}
/** new button code end */
[user=439709]@override[/user]
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
[user=439709]@override[/user]
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
// End of Default Items
incorrectly announces intent
Try like this.
Code:
Intent intent = new Intent(this, SecondActivity.class) ;
startActivity(intent);
and check whether your Listener
1. you need to use the full name of your activity, including the package name.
2. you need to declare the activity in your AndroidManifest.xml file before calling it.
rhmsoft said:
2. you need to declare the activity in your AndroidManifest.xml file before calling it.
Click to expand...
Click to collapse
Unless he want's to run an activity that's not his, like opening the contact list, but I think you're right in assuming he's looking to launch a second one of his own activities.
bornander said:
Unless he want's to run an activity that's not his, like opening the contact list, but I think you're right in assuming he's looking to launch a second one of his own activities.
Click to expand...
Click to collapse
I thought you had to declare all your activities in the manifest?
Log
Post the error log please
Akatsuki
You can skip the intro and just look at the examples below and check out the GitHub page(down below) if you already know what I'm talking about through the title.
Motivation/Intro
Coming from standard desktop app development, configuration changes such as screen rotation is new. The way Android handles these configuration changes is by restarting your app(the current Activity, to be precise). You lose all your fields so you have to retain them some how, here's a typical case:
PHP:
public class MainActivity extends Activity {
private static final String MY_STRING = "myString";
private static final String MY_INT = "myInt";
private static final String ACCOUNT = "account";
String myString;
int myInt;
Account account;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myString = savedInstanceState.getString(MY_STRING);
myInt = savedInstanceState.getInt(MY_INT);
account = savedInstanceState.getParcelable(ACCOUNT);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(MY_STRING, myString);
outState.putInt(MY_INT, myInt);
outState.putParcelable(ACCOUNT, account);
}
}
As you can see, you need extra constants and lots of boilerplates just to retain the values of some fields. Java is already kinda verbose, Android is making things worse by requiring you retain the fields manually.
Akatsuki eliminates all those boilerplate, here's what the example from above looks like with Akatsuki:
PHP:
public class MainActivity extends Activity {
@Retained String myString;
@Retained int myInt;
@Retained Account account; // Account implements Parcelable
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Akatsuki.restore(this, savedInstanceState);
//everything restored!
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Akatsuki.save(this, outState);
}
}
And that's it, the fields are retained.
Performance Impact
Next to 0, the whole processing is done at compile time.
Magic?
No black magic used, all this is possible because of JSR269 (Annotation Processing Tool). There are similar libraries out there such as Butterknife
Notable Features
Supports most commonly used types (beyond what Bundle supports!)
Pluggable type conveter for your custom types
Near zero runtime performance impact
Integration tests included
Generic type support
View state support
Can be used as an Object to Bundle serialization solution as well
License
Apache 2.0
GitHub page with download links (gradle dependencies) and a more detailed README:
https://github.com/tom91136/Akatsuki
(Sorry, can't have links because I got less than 10 posts for now)