I am trying to build a char using the library from achartengine (http://www.achartengine.org/). So i try to run SalesGrowthChart.java on my own aplication so when someone clicks on a button it will show him the chart .
This is my code :
private IChart[] mCharts = new IChart[]{new generatedchart()};
And i try to generate it like this
bt.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), generatedchart.class);
myIntent=mCharts[0].execute(this);
}
});
But this won't work. How can i make it to work.Hope you understand what i am saying.
This is the error :
The method execute(Context) in the type IChart is not applicable for the arguments (new View.OnClickListener(){})
Your problem is that the "this" mentioned inside that method refers to a view (which is what you are creating at that point.)
The method execute needs a Context, so you need to get the context in a different manner.
You should try getting the context like this:
YourActivityName.this
Which in your code would be like:
bt.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), generatedchart.class);
myIntent=mCharts[0].execute(YourActivityName.this);
}
});
When you are calling this within execute(), it is referring to the OnClickListener class because of the dynamic class declaration. Try using getApplicationContext() instead of this.
Related
I'm new to Android development and Java and I'm having troubles with Toast:
I have a MainActivity with several buttons. Each one of this starts another Activity with typical setOnclickListener method like this:
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setClass(getApplicationContext(), secondaryActivity.class);
startActivity(intent);
}
});
Then, inside this secondaryActivity.class I have another button that make some stuff. Inside this Activity I wanna display a Toast on button's click but it doesn't work:
secondaryActivityBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(view.getContext(),"text",Toast.LENGTH_LONG).show();
}
});
As Toast's context I've tried: getApplicationContext(), getBasecontext(), view.getContext(), mySecondaryActivityClass.this... No one of this display the Toast, I don't know where's the mistake. Supposedly, view.getContext() might work but it doesn't display anything...
MainActivity extends AppCompactActivity and mySecondaryActivity extends Activity.
You can use secondaryActivity.this like:
Toast.makeText(secondaryActivity.this,"text",Toast.LENGTH_LONG).show();
It works only on secondaryActivity class
Try this:
Toast.makeText(getActivity().getApplicationContext(),"text",Toast.LENGTH_LONG).show();
I'm getting this error when I write the intent inside the onCreate method.
But when I write the intent inside an outer method and call it, it works.
Button click listener is an interface and you implemented it here as an anonymous class, so inside of that class this refers to that anonymous class, not your activity class, but Intent constructor needs activity class implementation, therefore as #ADITYA RANADE answered you need to change it to MainActivity.this.
However if you replace anonymous class with lambda you can avoid this:
Button button = new Button(context);
button.setOnClickListener(v -> {
Intent intent = new Intent(this, MainActivity.class);
});
Change it to MainActivity.this inside the intent
Well that is because of the place or more precisely "context" (not the androidish Context) of where you are calling it.
When you call it from the anonymously created inner class which implements the listener for a view click, so in this case the this represents something else - anonymous class.
But on the other side when you make the method e.g. openScheduleActivity() inside the activity itself, the this keyword represents the activity itself and in fact represents the androidish Context or in this particular case even the activity. So you can either stay with case that you have already had there and slightly edit it, or you can use lambda expression, or you can use the method inside the activity itself as you have already discovered.
edited case:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, schedule.class);
startActivity(intent);
}
});
lambda expression:
button.setOnClickListener(v -> {
Intent intent = new Intent(MainActivity.this, schedule.class);
startActivity(intent);
});
I'm new to android studio and i have a problem when I'm trying to jump to a new activity, so when the line is:
public class signup_activity extends AppCompatActivity {
ImageButton logupButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup_activity);
logupButton = findViewById(R.id.signuparrow);
logupButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, signup_activity.class);
startActivity(intent);
}
});
}}
I get the error:
'com.example.myapplication.MainActivity' is not an enclosing class
and i so a couple of of people advising to chane the intent to this instead of MainActivity.this
but when im changing to this i get the error:
Cannot resolve constructor 'intent'
Intent intent = new Intent(MainActivity.this, signup_activity.class);
Several things:
First, the first param of Intent() constructor is a context. Since you are on signup_activity you need to do signup_activity.this to use it as a context.
I'd assume you want to go to MainActivity, so your second param should be MainActivity.class. It seems you got the order altered there.
you are in signup_activity and when use Intent in fisrt part you should call the current contex to jump to other activity.
so you should replace
Intent intent = new Intent(signup_activity.this, MainActivity.class);
if you want to jump to signup_activity you can call intent from MainActivity.
String[] classes = {"Activity","Activity1"};
for( i = 0; i < sources.length; i++) {
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(v.getContext(),classes[i].class);
startActivityForResult(intent,0);
}
});
}
Here is problem with classes[i].class . Is it possible . Need help
So many things wrong with this code.
1)To access a variable in an anonymous inner class like that, it must be declared as final.
2)It won't work anyway- the class of "Activity" isn't Activity.class, it's String.class. Because "Activity" is a string. If you want this code to work, you need an array Class, not an array of Strings. And then the two members would need to be Activity.class and Activity1.class.
You can try this one:
You need to made few changes in your code:
First: You can declare your array string of classes, in this way:
String[] classes = {"YourPackageName.Activity","YourPackageName.Activity1"};
Second: For starting the activity.
for( i = 0; i < sources.length; i++) {
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent=new Intent();
intent.setClassName(v.getContext(),classes[i]);
startActivityForResult(intent,0);
}
});
}
Hope this helps you.
Considering your for loop does not give any kind of Exception, Bcoz i can't test that. But above code should work in order to start your Activity.
actually I am bit confused on some thing which is working in one scenario very well but in other scenario that is not working fine so I am here to explain my problem to identify the wrong move made by me, if any on will identify the problem please mention it. thank you.
working fine:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.distance_demo_button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ListViewClass.class);
intent.putExtra(ListViewClass.EXTRAS_TARGET_ACTIVITY, Distance.class.getName());
startActivity(intent);
}
});
NOt working fine:
scan = (Button) view.findViewById(R.id.scan);
scan.setOnClickListener(startListener);
}
//Create an anonymous implementation of OnClickListener
private OnClickListener startListener = new OnClickListener() {
public void onClick(View v) {
Toast.makeText(view.getContext(),"Scanning", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, ListViewClass.class);
intent.putExtra(ListViewClass.EXTRAS_TARGET_ACTIVITY, Distance.class.getName());
startActivity(intent);
}
}
now the problem is in this line
Intent intent = new Intent(MainActivity.this, ListViewClass.class);
when I use this line in this scenario it shows me error on this line:
Error: 'The constructor Intent(MainActivity, Class<ListViewClass>) is undefined' please mention my problem if you are well aware of it.
use a global context variable glaboaly and use it.
Context myContext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myContext = MyActivity.this;
}
and use myContext insted of MainActivity.this.
Im not sure whether it solve the problem, just try it out
When your calling Intent intent = new Intent(MainActivity.this, ListViewClass.class); inside MainActivity class. It will work fine. But when your not in Context of MainActivity class. i.e move to other class. This error will occur.
If you want to rectify, You can follow #jithu method. Otherwise store the context in Application class. And use it in anywhere.
In the first case you are overriding the method onClick().
If you override the method it's like you are declaring the function in your activity class.
This is the reason that you can call MainActivity.this
In the second case you are implementing the abstract method onClick(). You can't call MainActivity.this because your "MainActivity.this" is referencing to the current context and this isn't accessible from OnClickListener.class.