Cannot access member object inside activity - java

Why can't I access remTime object? I get runtime exception while I can access all other Activity members declared the same way.
public class MainActivity extends Activity {
ListView lstDayInterval, lstWeekDay, lstMonthDay, lstMonth;
...
RemCalendar remTime;
#Override
public void onCreate(Bundle savedInstanceState) {
...
remTime.setToNow();
}
public class RemCalendar {
private Calendar C;
public RemCalendar() {
C = Calendar.getInstance();
}
public void setToNow() {
C = Calendar.getInstance();
}
}
Thanks

you forget to initialize remTime instance before calling method from RemCalendar class. initialize it inside onCreate :
#Override
public void onCreate(Bundle savedInstanceState) {
...
remTime=new RemCalendar();
remTime.setToNow();
}

Related

how to get lifecycle of abstract activity in android

I have a video player app where I need to access the lifecycle of an abstract activity from another class in Android. In my abstract activity, I've tried using LifecycleRegistry, but this is getting me the lifecycle owner not the actually lifecycle of the abstract class. How can I access the lifecycle of an abstract activity from another class?
Here is my abstract activity:
abstract public class MainActivity extends AppCompatActivity {
private LifecycleRegistry lifecycleRegistry;
VideoPlayer videoPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lifecycleRegistry = new LifecycleRegistry(this);
lifecycleRegistry.setCurrentState(Lifecycle.State.CREATED);
setContentView(R.layout.activity_main);
videoPlayer = new VideoPlayer();
playVideo();
}
public void playVideo(){
videoPlayer.init();
//calls function in VideoPlayer class
}
#Override
protected void onResume() {
super.onResume();
lifecycleRegistry.setCurrentState(Lifecycle.State.RESUMED);
}
#Override
protected void onDestroy() {
super.onDestroy();
lifecycleRegistry.setCurrentState(Lifecycle.State.DESTROYED);
}
}
Here is the class where I need to get the lifecycle of my abstract MainActivity:
public class VideoPlayer {
public void init() {
playVideo();
}
public void playVideo() {
//async call happens here, I need getLifeCycle() from MainActivity
}
}
Don't know a know about the context of you feature, but you You can do smth
public class VideoPlayer {
private Lifecycle mLifecycle;
public VideoPlayer(Lifecycle lifecycle) {
mLifecycle = lifecycle;
}
public void init() {
playVideo();
}
public void playVideo() {
//you have mLifecycle now
}
}
In Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
videoPlayer = new VideoPlayer(getLifecycle());
}

Can i call method of other Activity from another Activity?

I want to call method of Other activity. I searched alot and tried by myself but failed. It work when i call from class to class like classA m=new classA();
m.function();
But i want to call from another activity method. Please help
public void onNotificationPosted(StatusBarNotification sbn) {
ChatActivity ch=new ChatActivity();
ch.location();
}
public class ApplicationContext extends Application {
public ActivityMain activityMain;
public void setActivityMain(ActivityMain activityMain) {
this.activityMain = activityMain;
}
public ActivityMain getActivityMain() {
return activityMain;
}
In ActivityMain.class
public class ActivityMain extends BaseProject implements MainView {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ApplicationContext.getInstance().setActivityMain(this);
}
#Override
protected void onDestroy() {
super.onDestroy();
ApplicationContext.getInstance().setActivityMain(null);
}
Use it anywhere like Activity, Fragments, Broadcast Receiver or Service like
if (ApplicationContext.getInstance().getActivityMain()!=null){
ApplicationContext.getInstance().getActivityMain().callAnyMethod();
}
Enjoy :)

Calling a method of MainActivity from other class

I am developing an Android app and thus, I have a MainActivity class. Inside of that MainActivity class, I have a method, let's call it doSomething():
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void doSomething(){
// bla bla bla
}
}
I also have a different class (with different layout) that is called OtherActivity. I want to use the doSomething method inside it:
public class OtherActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other_activity_layout);
// Let's use doSomething()
}
}
I tried this:
public class OtherActivity extends AppCompatActivity {
MainActivity main;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other_activity_layout);
// Let's use doSomething()
MainActivity main = new MainActivity();
main.doSomething();
}
}
But it does not work. I also tried to make OtherActivity to extend the MainActivity, doing the following:
public class OtherActivity extends MainActivity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other_activity_layout);
// Let's use doSomething()
super.doSomething();
}
}
But it does not allow me to initialize the layout...
How can I do?
Thanks in advance.
To communicate between to Activity Broadcast is the best way, and for the same application, we can use local broadcast using LocalBroadcastManager.
First, we should register one broadcast in MainActivity,
public class MainActivity1 extends AppCompatActivity {
public static final String INTENT_FILTER = "do_some_action";
public static final String INTENT_BUNDLE_VALUE = "value1";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
LocalBroadcastManager.getInstance(this).registerReceiver(
mChangeListener, new IntentFilter(INTENT_FILTER));
}
#Override
protected void onDestroy() {
super.onDestroy();
LocalBroadcastManager.getInstance(this).unregisterReceiver(mChangeListener);
}
private BroadcastReceiver mChangeListener = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intentData) {
// you can do anything here
if (intentData != null && intentData.hasExtra(INTENT_BUNDLE_VALUE)) {
String value = intentData.getStringExtra(INTENT_BUNDLE_VALUE);
doSomeAction(value);
}
}
};
private void doSomeAction(String value) {
}
}
Then to do some action in MainActivity from OtherActivity, we can send Local broadcast from OtherActivity it will reach the receiver of Which we register in MainActivity,
public class OtherActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
// You can call MainActivity to do some actions
Intent intent = new Intent(MainActivity1.INTENT_FILTER);
intent.putExtra(MainActivity1.INTENT_BUNDLE_VALUE, "Any string or any value");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}
Done!!!.
Something like this should do the trick, I'm going to make a static navigator to handle your navigation logic. If you are opposed to static methods you could also make them on your Application object to make it easier to manage dependencies, I'm just making it static for simplicity.
//Making this fully static for simplicity, this is fine for a small app
//you can make it a singleton on the application class for more flexibility
public class Navigator {
//static member vars that determine navigation
// pass in Context if needed for navigation purposes
public static void doSomething(Context context){
// bla bla bla
}
}
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity_layout);
}
private void doSomething() {
Navigator.doSomething(this);
}
}
public class OtherActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other_activity_layout);
}
private void doSomething() {
Navigator.doSomething(this);
}
}

Call a method in different activities from a non-activity class

I have 3 classes with the same function. The class H is a controller class, so i have made a setactivity class to get the instance of the activity that called it. Now i have to call the function userleft() of the currently initialized class from the controller class H which is a non Activity.
The error i am getting is "The method userleft() is undefined for the type Activity" and i know that the exception would come, since the class Activity does not contain a method called userLeft(). But what is the best way to make this work?
See the structure of the app below.
public class A extends Activity {
void onCreate(Bundle savedInstanceState) {
H.getInstance().setactivity(this);
}
public void userleft() {
}
}
public class B extends Activity {
void onCreate(Bundle savedInstanceState) {
H.getInstance().setactivity(this);
}
public void userleft() {
}
}
public class C extends Activity {
void onCreate(Bundle savedInstanceState) {
H.getInstance().setactivity(this);
}
public void userleft() {
}
}
and i have another non activity class
public class H {
public static H getInstance() {
if (instance == null) {
instance = new H();
}
return instance;
}
public void setActivity(Activity activity) {
this.activityGame = activity;
}
public void randomCallBack(){
activityGame.userleft();
}
}
Thanks!
You need to redesign your classes like below
Declaire a base activity class
public abstract class BaseActivity extends Activity {
public abstract void userleft();
}
And declare your classes like
public class A extends BaseActivity {
void onCreate(Bundle savedInstanceState) {
H.getInstance().setactivity(this);
}
#Override
public void userleft() {
}
}
And now modify your Non-activity class like
public class H {
public static H getInstance() {
if (instance == null) {
instance = new H();
}
return instance;
}
public void setActivity(BaseActivity activity) {
this.activityGame = activity;
}
public void randomCallBack(){
activityGame.userleft();
}
}
Now you will be able to call your method for any subclass activity.
In your randomCallBack() method you have an Activity object, the class Activity does not contain a method called userLeft(), so it is normal that you get such an exception. So, you have two ways of solving this issue:
Either, you cast the activityGame to its corresponding class as:
public void randomCallBack(){
if (activityGame instanceof A) {
((A)activityGame).userleft();
}
if (activityGame instanceof B) {
((B)activityGame).userleft();
}
if (activityGame instanceof C) {
((C)activityGame).userleft();
}
}
Or, in a better way, you create an Interface, which is implemented by your clasess A, B, and C, and used by H class. Like this:
public interface UserLeft {
public void userleft();
}
public class A extends Activity implements UserLeft {
void onCreate(Bundle savedInstanceState) {
H.getInstance().setactivity(this);
}
public void userleft() {
}
}
public class B extends Activity implements UserLeft {
void onCreate(Bundle savedInstanceState) {
H.getInstance().setactivity(this);
}
public void userleft() {
}
}
public class C extends Activity implements UserLeft {
void onCreate(Bundle savedInstanceState) {
H.getInstance().setactivity(this);
}
public void userleft() {
}
}
public class H {
UserLeft activity;
public static H getInstance() {
if (instance == null) {
instance = new H();
}
return instance;
}
public void setActivity(UserLeft activity) {
this.activityGame = activity;
}
public void randomCallBack(){
activityGame.userleft();
}
}
Activity class does not have the method of userleft , so that you can't call that method by the reference activity . You can define one BaseActivity with the method userleft as the parent class of these classes.
You forgott your instance variable declaration :) private Activity activityGame = null;
Of course class Activity has no method called userleft().
You need to cast your variable activityGame to your specific Class like that:
public class H {
private Activity activityGame = null;
public static H getInstance() {
if (instance == null) {
instance = new H();
}
return instance;
}
public void setActivity(Activity activity) {
this.activityGame = (<CAST TO YOUR CLASS>) activity;
}
public void randomCallBack(){
activityGame.userleft();
}
}
EDIT:
Please try this:
public void setActivity(Activity activity) {
this.activityGame = (activity.getClass()) activity;
}

Access on create variables through out the android activity class

I am getting transaction_amount variable from getIntent() in OnCreate method.
String transaction_amount = getIntent().getStringExtra("transaction_amount");
I want to access this variable outside of the OnCreate method.
Something like,
public class PayActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
String transaction_amount = getIntent().getStringExtra("transaction_amount");
}
protected void my_function(){
// I want transaction_amount here.....
}
}
You can use a global variable :
public class PayActivity extends ActionBarActivity {
String value;
#Override
protected void onCreate(Bundle savedInstanceState) {
value = getIntent().getStringExtra("transaction_amount");
}
public void yourFunction() {
// You can acces value here
}
// your activity here....
}
create the transaction_amount variable as a class variable, then initialize it in the onCreate
public class PayActivity extends ActionBarActivity {
private String transaction_amount;
#Override
protected void onCreate(Bundle savedInstanceState) {
transaction_amount = getIntent().getStringExtra("transaction_amount");
}
protected void my_function(){
// I want transaction_amount here.....
}
}
maintain transaction amount as a global variable ,if you require that variable in whole class or if you want acces that variable in method, send that variable as a parameter to that function and called that function from oncreate.
Declare a global variable and intialize it to the transactionamount in onCreate. Then use that global variable anywhere.
public class PayActivity extends ActionBarActivity {
String s;
#Override
protected void onCreate(Bundle savedInstanceState) {
s = getIntent().getStringExtra("transaction_amount");
}
void anyFunction() {
// use the string s here now
}
}

Categories