0

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!

Community
  • 1
  • 1
George
  • 5,808
  • 15
  • 83
  • 160
  • Either set it into a Bundle as you did it or into the SharedPreferences or if you want them to be persistent use a sqlite. Show your exception please when it crashes. thanks – Sergey Benner Jan 15 '12 at 16:28
  • Where is your onCreate(Bundle...) in the LineGraph activity? – Sergey Benner Jan 15 '12 at 16:49
  • I don't have!I tried with Bundle but i did't make it.Could you please tell me where exactly to put "Bundle" and how?Because i a, not familiar with sharedpreferences and i don't understand that.I am updating the error messages – George Jan 15 '12 at 16:53

1 Answers1

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

Sergey Benner
  • 4,421
  • 2
  • 22
  • 29
  • :I answere above.Please check.Thanks! – George Jan 15 '12 at 16:55
  • check this particular line searchIntent.setClassName("com.mypackage",searchActivity.class.getName()); – Sergey Benner Jan 15 '12 at 17:04
  • :I updated my " public void onClick(View v)" and the LineGraph class.The problem is that int the onCreate methid i do "String fcores=..." but then in the "public Intent getIntent(Context context)" how to use this variable?Also,in the onCreate method i don't use "setContentView" method because it supposes to do the plot.What remains to do?Thank you! – George Jan 15 '12 at 17:17
  • :) are you sure you need an Activity class for this which will reflect and show UI with components there? If not you might want to use a usual class with a set of methods and fields. And in this case if you want to use the variables in your getIntent() method you should use a global private variable and set it from within the onCreate() method. – Sergey Benner Jan 15 '12 at 17:23
  • :First of all thanks for your precious help!I have 2 problems now: 1) I want to pass two strings ,so if i use two times the getMyString() it doesn't work.(it says "Duplicate method"). 2) In the core_calcs class where the result is shown and the button "Show graph" exists,don't i have to start the activity "LineGraph"?I can't understand how to do it without starting an activity.Again thank you! – George Jan 15 '12 at 17:43
  • Your calculations should be moved to an Utility or a Helper class where you can make static methods with them where you will pass your strings and inputs and the Activities will reflect in UI your calculation results. I will update the class but I don't think that your application architecture is quite correct mate. – Sergey Benner Jan 15 '12 at 18:21
  • :Now, when i press the "Show graph"button it gives me a blank screen.What i am doing (in core_calcs) is passing the result fcores and the initcores(user input) to the LineGraph in order to use these data and make the plot.That's why i create this activity.I can't think sth else.You said about a Utility or Helper class,could you point me somewhere in order to understand what to do?I am so close!:) Thanks! – George Jan 15 '12 at 18:59
  • hmm in the onCreate() do the startActivity(getIntent(this)) after all init settings I wrote. this should work I guess in your case. – Sergey Benner Jan 15 '12 at 19:08
  • :Now it crushes again.I don't know why..Also,in your code it must be "Double initcores= Double.valueOf(extras.getString("value2")); value2 and not inicoeres because in the core_calcs i pass value2.The same for fcores.Also,in this line you have " public Double getFcores(){ return this.initcores;} " and it must be fcores.I 'll check again tomorrow,thanks a lot! – George Jan 15 '12 at 19:26
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/6739/discussion-between-sergey-benner-and-george) – Sergey Benner Jan 15 '12 at 20:03