0

I wanna draw a graph in android and for that I'm using a chart(I import into my app a jar file) to which I pass the valuess need to be shown...

The problem with this chart is that it occupies the full screen in the activity that is set.

public void onCreate(){

LineView lv;

lv=new LineView(this);

}

The values that I put on the chart are readed from DB using a Async thread so somwewhere I do this:

protected void onProgressUpdate(String...values) {


setContentView(lv);

}

over and over again with the whole data.

And here is my problem:

I tried to use an xml file in which I have a blank linear layout and a button(I wanna set a button underneath my chart) and I tried to pass my chart lv as a child to that Linear layout which doesn't occupies my whole screen:


xml file:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout

  xmlns:android="http://schemas.android.com/apk/res/android"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent">



<LinearLayout


    android:id="@+id/titlu"

    android:orientation="horizontal"

    android:layout_width="fill_parent"

    android:layout_height="250px" >


/>

</LinearLayout>

  <Button

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:text="Stop"

 android:gravity="bottom"

 android:id="@+id/stop"  

 />

When I tried in onCreate:

thatlinear=(LinearLayout)findViewById(R.id.titlu);

and this :

thatlinear.addView(lv);  in my function onProgresssUpdate()

I got an error which told me that my chart,lv,already has a parent and in order to pass it to other parent I have to remove the initial parent.

So...I did this:

ViewGroup v=(ViewGroup)(lv.getParent());

         v.removeView(lv);

But guess what it didn't worked this way...I got a FC-java lang null pointer exception at this line:

v.removeView(lv);

And the last this I tried was this:

  LinearLayout llay=new LinearLayout(this);

      LinearLayout lytContainer = (LinearLayout) View.inflate(
              this, R.layout.chart_speed, null);



      lytContainer.setLayoutParams(new LinearLayout.LayoutParams(

                          LinearLayout.LayoutParams.FILL_PARENT,

                          LinearLayout.LayoutParams.WRAP_CONTENT));


      llay.addView(lv);

chart_speed is the xml I posted above...

And it didn't worked this way too....still FC.

Question:

How do proceed in order to pass my chart as a child to that LInear layout so it won't filll my entire screen...and I could place buttons underneath it???

EDIT:

I've done this:

setContentView(R.layout.chart_speed);

ViewGroup thatlayout=(ViewGroup)findViewById(R.id.titlu);

thatlayout.addView(lv);

chart_speed-is the name of my whole xml titlu is the @+id of my LinearLayout...

And this is what my logcat displays:

 java.lang.NullPointerException
     at com.kidroid.kichart.view.AxisView.GenerateValue(AxisView.java:112)

at com.kidroid.kichart.view.AxisView.drawAxisXY(AxisView.java:61)

at com.kidroid.kichart.view.LineView.onDraw(LineView.java:19)

at android.view.View.draw(View.java:6535)

 at android.view.ViewGroup.drawChild(ViewGroup.java:1531)

  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)

0  at android.view.ViewGroup.drawChild(ViewGroup.java:1529)

    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)

    at android.view.ViewGroup.drawChild(ViewGroup.java:1529)

 at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)

  at android.view.View.draw(View.java:6538)

EDIT2:

I've done this in my ProgressUpdate(){

//initializating the component

thatlayoyt.add(lv);

}

And for a few second it displays correct,but after that I get FC:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

at android.view.ViewGroup.addViewInner(ViewGroup.java:1861)

at android.view.ViewGroup.addView(ViewGroup.java:1756)

at android.view.ViewGroup.addView(ViewGroup.java:1713)


FINAL VERSION and it works...I don't think is very correct but it works:

ViewGroup thatlayout;

public void onCreate(Bundle savedInstanceState) {

thatlayout=(ViewGroup)findViewById(R.id.titlu);

}

protected void onProgressUpdate(String...values) {

//initalizating

try{
         ViewGroup v=(ViewGroup)(lv.getParent());
         v.removeView(lv);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }


        thatlayout.addView(lv);

}


adrian
  • 4,574
  • 17
  • 68
  • 119

1 Answers1

0

I believe the trouble is here:

protected void onProgressUpdate(String...values) {

    setContentView(lv); //HERE

}

You only have to call setContentView once, in onCreate. After that, you can instantiate a LineView (I suppose this is the graph) and add it to the main layout.

// in onCreate   
setContentView(R.layout.main);
LineView lv = new LineView();
ViewGroup main = (ViewGroup)findViewById(R.id.main);
main.addView(lv);

I assumed your main layout is defined in main.xml and the root layout has android:id="@+id/main.

Gabriel Negut
  • 13,860
  • 4
  • 38
  • 45
  • It looks like there is an problem with the custom component. Before showing it you have to do some work initializing it. See [this link](http://writerbay.net/?p=401) or [this one](http://stackoverflow.com/questions/6221300/place-a-chart-in-a-view). – Gabriel Negut Jun 03 '11 at 12:29
  • =))) ....In romanian:din primul link mi am luat componenta...iar al doilea e o intrebare postata tot de mine...None of this helps:) – adrian Jun 03 '11 at 12:33
  • I tried this after initializating it(in ProgressUpdate()...) thatlayoyt.add(lv); ViewGroup v=(ViewGroup)(lv.getParent()); v.removeView(lv); – adrian Jun 03 '11 at 12:42
  • You got me wrong. The `NullPointerException` is caused by the `LineView`, because you didn't call `lv.setItems()` (with the properly initialized array of `Aitem`s, of course). Your latest layout modifications are unnecessary and cause more errors. – Gabriel Negut Jun 03 '11 at 12:48
  • Exactlyyy...lv.setItems() and other initializating things I do it in my onProgressUpdate()...so I do this...and after that I do thatlayout.add(lv);.... And then it works well for a second and after that crashes....:) – adrian Jun 03 '11 at 12:55
  • Do not do any initializing stuff in `onProgressUpdate`. Do it only once, in `onCreate`. If you want, you can read the values to an `Aitem` array in `onProgressUpdate` and call `lv.setItems` on `onPostExecute`. – Gabriel Negut Jun 03 '11 at 13:00
  • I've posted my final version on the question:) – adrian Jun 03 '11 at 13:09