I have a BaseActivity like bellow... Where My Interface global object on super class beame null.
public abstract class BaseActivity extends BaseTabActivity {
//did some implementation
}
public abstract class BaseTabActivity extends BasePickerActivity {
//did some implementation
}
public abstract class BasePickerActivity extends Activity {
// This is an interface
private IOnImagSelected iOnImagSelected;
public void imagePicker(IOnImagSelected iOnImagSelected){
this.iOnImagSelected = iOnImagSelected;
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// process data and called iOnImagSelected
// Here I get null point which mean iOnImagSelected is null
iOnImagSelected.onImageSelected(imagepath)
}
}
Now I have a Activity like
public class MyActivity extends BaseActivity implements IOnImagSelected {
public void onPickImageClick(View v){
imagePicker(this);
}
#Override
poublic void onImageSelected(){
}
}
Why My interface variable became null . Please help me with this
I solved it its all hapening because of this option on my Google Nexus 7 developer settings
goto settings->Developer options
in that in APPS category(scroll down to see), see the option
Don't keep Activities (Destroy every Activity as soon as user leabes it).
I found this from this post
That to that guy.:)
You're passing one Activity, MainActivity to the BasePickerActivity but when BasePickerActivity is active MainActivity is null. Basically, your code is invalid; you ought to use Fragments for this sort of thing.
Related
I have a BaseActivity, in which I am trying to retrieve the instance of a subclass in order to inject it with dagger.
I've been trying to figure out a way to not check manually the object passed on is an instance of every single Activity, but if there is a more optimal way of doing it.
F.i., if the component is as such:
AppComponent
#Singleton
#Component(modules = {AppModule.class})
public class AppComponent {
public void inject(FooActivity fooActivity);
public void inject(BarActivity barActivity);
public void inject(…);
}
Each activity extends BaseActivity and we have something like:
FooActivity
public class FooActivity extends BaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.inject(this);
}
}
Last, the issue comes when handling the BaseActivity.
BaseActivity
current implementation
...
public void inject(FooActivity fooActivity) {
DaggerAppComponent.create().inject(fooActivity)
}
public void inject(BarActivity barActivity) {
DaggerAppComponent.create().inject(barActivity)
}
...
However, I am looking for a more optimal way that reduces boilerplate, so I have been thinking something like the following:
BaseActivity
...
public void inject(Object activity) {
// retrieve the instance of the activity
// if possible and handle it so then we can:
DaggerAppComponent.create().inject(activity)
}
...
But since I am required to find its instance, instead of having a series of if statements with instanceof, which I will need to update for every new activity, is there a way to handle it in a more automated way?
Disclaimer: I'm coming from Swift so my idea on how delegation works is biased.
I have an interface defined:
public interface IChatButtonResponse {
void chat(ListingEntry listingEntry, String initialUserText);
}
My class has a local reference to it that is saved when instantiated:
public class ListingAdapter extends BaseAdapter {
private IChatButtonResponse delegate;
public ListingAdapter(Context context, ArrayList<ListingEntry> listingEntries, IChatButtonResponse delegate) {
this.delegate = delegate;
When I try to call the delegate in response to a button tap, I get a compilation error:
chatButton.setOnClickListener(new View.OnClickListener() {
delegate.chat(listingEntry, null); // Cannot resolve symbol 'chat'
});
Why can it not be resolved? I tried to make the method declaration in the interface public but that didn't change anything. (I would have been surprised if it did)
EDIT:
My usage of the interface in the same package:
public class MainActivity extends AppCompatActivity implements IChatButtonResponse {
.
.
.
#Override
void chat(ListingEntry listingEntry, String initialUserText) {
}
I am getting a compilation error here: 'chat(ListingEntry, String)' in ...sample.app.MainActivity' clashes with 'chat(ListingEntry, String) in ...sample.app.IChatButtonResponse'; attempting to assign weaker access privileges ('package-private'); was 'public'
Tried making the implementation method public with no change.
For the original error in question, the clue was found on another error I hadn't noticed earlier on the chatButton.setOnClickListener.
The OnClickListener method onClick must be overridden like this:
chatButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String referenceID = listingEntry.referenceID;
delegate.chat(listingEntry,null);
}
});
This fixes the resolution error at the call site.
I included the library Swipeable-Cards in my android project. In MainActvitiy.java the onCreate method includes something like that:
SimpleCardStackAdapter adapter = new SimpleCardStackAdapter(this);
//This should also be done on an event in the library class:
adapter.add(new CardModel("Title2", "Description2 goes here", r.getDrawable(R.drawable.picture2)));
Now, in the CardContainer.java (which belongs to the swipeable cards library) there is an event on which I want a new item added to the adapteradapter.add(...). The adapter was defined in the MainActvitiy.java as you can see above.
How can I achieve this?
I first thought about defining a new method in MainActivity and then calling it from my library-class, like that:
public void callfromlibrary() {
adapter.add(...);
}
However then the method and the adapter need to be defined static, additionally I don't know how to make this method of MainActivity available in CardContainer.java.
I believe I need to create kind of a listener to check in the MainActivity what happens in CardContainer.java? I don't know how to do this.
Any help is appreciated!
To allow CardContainer to communicate up to the MainActivity, you define an interface in CardContainer and implement it in MainActivity. When the event occurs in CardContainer, it can then call Interface method in order to add the CardModel to the adapater.
public class CardContainer extends ... {
CardContainerEventListener mCallback;
// Define a interface
public interface CardContainerEventListener {
public void addToAdapter();
}
// Method to register callback
void registerCallback(Activity callback) {
mCallback = (CardContainerEventListener) callback;
}
void someFunction() {
// Event got generated, invoke callback method
mCallback.addToAdapter();
}
}
public class MainActivity extends Activity implements CardContainer.CardContainerEventListener {
// Ensure you register MainActivity with CardContainer, by calling
// cardContainer.registerCallback(this)
public void addToAdapter() {
adapter.add(...);
}
}
please use a Java Interface for achieving this..
declare an interface in the cardcontainer class
public interface yourInterface{
public void callfromlibrary();
}
and intialize the object for calling the function
yourInterface object = (yourInterface) MainActivity;
and implement the interface in your main activity like
Class MainActivity extends activity implements yourInterface
and implement callfromlibrary() method
call this method from cardcontainer class whenever you needed using the object you have created ..
object.callfromlibrary()
I have my main class that extends Activity and it's over 2k line so I'm trying to write the code in a different class but not sure what it should extend. I've declared a Context and sent it the context from my main class but when I try something like context.findViewById() it won't let me.
What is the best practice here?
public class HomeScreen extends Activity implements OnItemSelectedListener{
//somewhere in a method, in an onClickListener
new Profile(getApplicationContext()).userCreate();
}
public class Profile{
Context ctx;
int userID;
public Profile (Context ctx)
{
this.ctx=ctx;
}
public void userCreate(){
Button create = (Button) ctx.findViewById()// can't call find view
}
}
I'm building and Android application using ActionBarSherlock.
I have multipe types of activities:
SherlockFragmentActivity
SherlockMapActivity
SherlockActivity
I want all my activities to share common methods to follow an internal workflow of screens.
If I create a Workflow class that extends SherlockFragmentActivity then my MapActivity does not work anymore.
If I create a Worflow class that extends SherlockMapActivity then my TutorialActivity does not work anymore (because it's using a new SectionsPagerAdapter(getSupportFragmentManager());.
Do note that the common methods I want are also running startActivity().
I know Java cannot have a class that extends more than one class, so how should I go about this?
public class Workflow extends SherlockMapActivity {
protected void goMain() {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
// ...
}
public class TutorialActivity extends Workflow {
// ...
// new SectionsPagerAdapter(getSupportFragmentManager());
// ...
}
public class GameActivity extends Workflow {
// ...
// MapView
// ...
}
I also want to share code like this:
#Override
protected void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setProgressBarIndeterminate(true);
setProgressBarIndeterminateVisibility(false);
}
You can create a custom Activity class and extend that in your activities.
So for example:
public abstract class DerivedActivityA extends Activity implements CustomInterfaceA {
// ... your code here
}
public abstract class DerivedActivityB extends DerivedActivityA implements CustomInterfaceB {
// ... your code here
}
If you have to implement multiple interfaces then use abstract classes like I did above and implement interfaces.
Edit:
If I get it right SectionsPagerAdapter is just an adapter so you can compose it in one of your classes as a field.
Edit2:
You can't extend two classes and you can't compose an Activity into another one so you either have to write things for yourself or extract the functionality you need from your third party library.