问题
I have a program which makes some calculations according to user data. The program works fine ,but when i try to use achartengine in order to make the plot ,it crashes.(doesn't do the plot)
I am not sure if i am passing right the data in the LineGraph class.
As i understand i must use
"Bundle sth=getIntent.getExtras()" but i am not sure where to put it in LineGraph.
I have the number_cores class which in which the user enters the data and then presses the calculate button and in another activity shows the result. In this , i have :
public void cores_func(){
double initcores=Double.parseDouble(num_cores.getText().toString().trim());
double half_time=Double.parseDouble(halftimecores.getText().toString().trim());
double ttime=Double.parseDouble(timecores.getText().toString().trim());
double l=Math.log(2)/half_time;
double fcores=initcores*Math.exp(-l*ttime);
Intent i=new Intent(this,core_calcs.class);
i.putExtra("value",fcores);
i.putExtra("value2",initcores);
startActivity(i);
}
Then , in the core_calcs class (as you can see from the intent above) , i show the result and also i added a button which when the user clicks it ,shows the graph (right now ,it crashes here).
I have (core_calcs) in the onCreate method :
double fcores=getIntent().getExtras().getDouble("value");
double initcores=getIntent().getExtras().getDouble("value2");
and then :
public void onClick(View v) {
switch (v.getId()){
case R.id.show_cores_graph:
double fcores=getIntent().getExtras().getDouble("value");
double initcores=getIntent().getExtras().getDouble("value2");
Intent i = new Intent();
i.setClassName("com.wordpress.androiddevgeo.Radiation",LineGraph.class.getName());
i.putExtra("value", fcores);
i.putExtra("value2", initcores);
this.startActivity(i);
break;
}
}
(also, i have the public void LineGraphHandler (View view) here)
Finally , in the LineGraph class (the intent above):
public class LineGraph extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Bundle extras=getIntent().getExtras();
String fcores=extras.getString("value");
String initcores=extras.getString("value2");
}
public Intent getIntent(Context context){
//double ttime=getIntent(context).getExtras().getDouble("value");
double [] x = {0,100}; //time axis
double [] y = {initcores,fcores}; //number of cores axis
TimeSeries series = new TimeSeries("Number of cores");
for (int i=0;i<x.length;i++){
series.add(x[i],y[i]);
}
XYMultipleSeriesDataset dataset=new XYMultipleSeriesDataset();
dataset.addSeries(series);
XYMultipleSeriesRenderer mRenderer =new XYMultipleSeriesRenderer();
XYSeriesRenderer renderer =new XYSeriesRenderer();
mRenderer.addSeriesRenderer(renderer);
Intent intent=ChartFactory.getLineChartIntent(context, dataset, mRenderer,"Decay");
return intent;
}
}
How to pass the data (initcores and fcores ) to the LineGraph?
--------Error messages ---------------------------------------------
W/dalvikvm(734): threadid=3: thread exiting with uncaught exception
(group=0x4000fe70) 01-15 18:42:01.334: E/AndroidRuntime(734): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime(734): android.content.ActivityNotFoundException: Unable to find explicit activity class
have you declared this activity in your AndroidManifest.xml?
(I have declared the activity for the LineGraph and also "org.achartengine.GraphicalActivity") Thanks!
回答1:
Bundle approach:
Intent searchIntent = new Intent();
searchIntent.setClassName("com.mypackage",searrchActivity.class.getName());
searchIntent.putExtra("value", initcores); // key/value pair, where key needs current package prefix.
searchIntent.putExtra("value2", fcores);
thisactivity.startActivity(searchIntent);
and in your LineGraph activity:
class LineGraph extends Activity{
private Double initcores;
private Double fcores;
public Double getInitcores(){ return this.initcores;}
public void setInitcores(Double initcores){ this.initcores=initcores;}
public Double getFcores(){ return this.fcores;}
public void setFcores(Double fcores){ this.fcores=fcores;}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......
Bundle extras = getIntent().getExtras();
Double initcores= extras.getDouble("value");
setInitcores(initcores);
Double fcores= extras.getDouble("value2"));
setFcores(fcores);
}
public Intent getIntent(...){
Double initcores= getInitcores();
Double fcores= getFcores();
//yourcode
}
}
SharedPreferences approach:
SharedPreferences sp =PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor ed= sp.edit();
ed.putInt("screen_width", 480);
ed.commit();
and in your next activity
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
int width = sp.getInt("screen_width",default_int_value);
hope this helps abit
来源:https://stackoverflow.com/questions/8871052/android-how-to-make-plot-with-user-data