2012-04-06 11 views
0

Ich bin ziemlich neu in diesem ganzen Grafik/Leinwand-Ding. Alles, was ich versuche, ist eine Leinwand mit einer Linie darunter unter einigen Knöpfen. Was habe ich falsch gemacht? Hier ist mein Code:Warum wird meine Leinwand nicht angezeigt (Android)?

public class Vectors extends Activity{ 

    VectorsView vectorsView; 
    LinearLayout l; 
    Canvas canvas; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.vectors); 
     vectorsView = new VectorsView(this); 

    } 

    public class VectorsView extends View{ 

     public VectorsView(Context context) { 
      super(context); 
     } 

     @Override 
     protected void onDraw(Canvas canvas) { 
      // TODO Auto-generated method stub 
      super.onDraw(canvas); 
      l = (LinearLayout) findViewById(R.layout.vectors); 


      Paint paint = new Paint(); 
      canvas = new Canvas(); 

      paint.setColor(Color.WHITE); 
      canvas.drawLine(0, 0, 100, 100, paint); 
      vectorsView.draw(canvas); 

      l.addView(vectorsView); 
      setContentView(l); 
     } 
    } 
} 

Und hier wird die angeforderte vectors.xml:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent"> 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="@drawable/background" 
     android:orientation="vertical"> 

     <ImageView android:layout_height="wrap_content" 
      android:src="@drawable/vectors" 
      android:layout_width="wrap_content" 
      android:layout_gravity="center" 
      android:paddingTop="5dp" 
      android:paddingBottom="5dp" 
      android:adjustViewBounds="true"> 
     </ImageView> 
     <LinearLayout android:layout_height="wrap_content" 
      android:layout_width="match_parent" 
      android:orientation="horizontal"> 
      <Button android:text="Choose Program" 
       android:id="@+id/bChsProgV" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="5dp" 
       android:layout_marginLeft="97dp" 
       android:adjustViewBounds="true"> 
      </Button> 
      <ImageButton android:layout_height="wrap_content" 
       android:src="@drawable/help" 
       android:id="@+id/ibHelpV" 
       android:layout_width="wrap_content" 
       android:layout_marginLeft="65dp" 
       android:background="@null" 
       android:layout_marginTop="10dp"> 
      </ImageButton> 
     </LinearLayout> 
     <LinearLayout android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 
      <TextView android:text="Value" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:paddingLeft="5dp" 
       android:paddingRight="5dp" 
       android:textColor="#fffafa"> 
      </TextView> 
      <EditText android:text="" 
       android:id="@+id/etValueV" 
       android:layout_height="wrap_content" 
       android:inputType="numberSigned|numberDecimal" 
       android:layout_width="100dp" > 
      </EditText> 
      <Button android:text="Next" 
       android:id="@+id/bNextV" 
       android:layout_width="65dp" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="10dp"> 
      </Button> 
      <TextView android:text="" 
       android:id="@+id/tvAnsValV" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
      </TextView> 
     </LinearLayout> 
     <LinearLayout android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 
      <TextView android:text="Angle" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:paddingLeft="5dp" 
       android:paddingRight="5dp" 
       android:textColor="#fffafa"> 
      </TextView> 
      <EditText android:text="" 
       android:id="@+id/etAngleV" 
       android:layout_height="wrap_content" 
       android:inputType="numberSigned|numberDecimal" 
       android:layout_width="100dp" > 
      </EditText> 
      <Button android:text="Prev" 
       android:id="@+id/bPrevV" 
       android:layout_width="65dp" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="10dp"> 
      </Button> 
      <TextView android:text="" 
       android:id="@+id/tvAnsAng" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
      </TextView> 
     </LinearLayout> 
     <LinearLayout android:layout_height="wrap_content" 
      android:layout_width="match_parent" 
      android:orientation="horizontal"> 
      <LinearLayout android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:orientation="vertical"> 
       <RadioGroup 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" >    
        <RadioButton android:layout_height="wrap_content" 
         android:layout_width="wrap_content" 
         android:checked="true" 
         android:text="Degrees" 
         android:layout_marginLeft="5dp" 
         android:id="@+id/rbDegV"> 
        </RadioButton> 
        <RadioButton android:layout_height="wrap_content" 
         android:layout_width="wrap_content" 
         android:checked="false" 
         android:text="Radians" 
         android:layout_marginLeft="5dp" 
         android:id="@+id/rbRadV"> 
        </RadioButton> 
       </RadioGroup> 
      </LinearLayout> 
      <RadioGroup 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" > 
       <LinearLayout android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:orientation="horizontal"> 
        <LinearLayout android:layout_height="wrap_content" 
         android:layout_width="wrap_content" 
         android:orientation="vertical"> 
         <RadioButton android:layout_height="wrap_content" 
          android:layout_width="wrap_content" 
          android:checked="false" 
          android:text="Q2" 
          android:layout_marginLeft="90dp" 
          android:id="@+id/rbQ2V"> 
         </RadioButton> 
         <RadioButton android:layout_height="wrap_content" 
           android:layout_width="wrap_content" 
           android:checked="false" 
           android:text="Q3" 
           android:layout_marginLeft="90dp" 
           android:id="@+id/rbQ3V"> 
         </RadioButton> 
        </LinearLayout> 
        <LinearLayout android:layout_height="wrap_content" 
         android:layout_width="wrap_content" 
         android:orientation="vertical"> 
         <RadioButton android:layout_height="wrap_content" 
          android:layout_width="wrap_content" 
          android:checked="true" 
          android:text="Q1" 
          android:layout_marginLeft="5dp" 
          android:id="@+id/rbQ1V"> 
         </RadioButton> 
         <RadioButton android:layout_height="wrap_content" 
           android:layout_width="wrap_content" 
           android:checked="false" 
           android:text="Q4" 
           android:layout_marginLeft="5dp" 
           android:id="@+id/rbQ4V"> 
         </RadioButton> 
        </LinearLayout> 
       </LinearLayout> 
      </RadioGroup>              
     </LinearLayout> 
     <Button android:text="Submit" 
      android:id="@+id/bSubmitV" 
      android:layout_width="75dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center"> 
     </Button> 
     <TextView android:text="" 
      android:id="@+id/tvErrorV" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:textColor="#fffafa"> 
     </TextView> 
     <LinearLayout android:id="@+id/llCanvasV" 
      android:layout_height="wrap_content" 
      android:layout_width="match_parent"> 
     </LinearLayout> 
    </LinearLayout> 

</ScrollView> 
+0

können Sie Ihre vectors.xml zeigen? – ligi

Antwort

0

Sie entweder die VectorsView in Ihrem vectors.xml verwenden - aber dann werden Sie benötigen einen Konstruktor mit Attributen hinzufügen

oder

Sie einen Container hinzufügen (zB Linearlayout) in Ihrem vectors.xml und fügen Sie Ihre vectorsView über den Code, um es - dann der Konstruktor Sie haben, ist genug

0

Ihre Ansichtshierarchie wird von Ihrem XML definiert.

Ihr Aufruf von setContentView() macht die ScrollView, die Ihr oberstes XML-Element darstellt, zur Stammansicht Ihrer Ansichtshierarchie.

Das VectorsView-Objekt, das Sie im Code erstellt haben, ist dieser Ansichtshierarchie nicht zugeordnet. Da es nicht in dieser Hierarchie ist, wird es nie angezeigt. Sein onDraw() Code wird niemals aufgerufen.

Die Aufrufe von addView() und setContentView() in Ihrer onDraw() - Routine sind ungeeignet. Die allgemeine Reihenfolge besteht darin, Ihre Ansichtshierarchie in Code zu definieren, der von onCreate() aufgerufen wird, und dann dem Betriebssystem zu erlauben, Ihre onDraw() -Methoden in der gesamten Hierarchie aufzurufen.

Wenn Sie alles außer den Zeichnungsbefehlen von onDraw() entfernt haben, können Sie es sehen, indem Sie den Aufruf von setContentView (R.layout.vectors) durch setContentView (vectorsView) ersetzen. Natürlich wäre dann keines Ihrer XML-definierten Widgets sichtbar.

Diese Seite bietet einige zusätzliche Informationen:

http://developer.android.com/guide/topics/ui/index.html