Cannot draw a simple line

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Postby MrSnowflake » Mon Oct 13, 2008 5:26 pm

I meant this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public Mens(Context context, AttributeSet attrs)
  3.  
  4.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Top

Postby xplosive » Mon Oct 13, 2008 5:31 pm

Sorry, didn't understand.

I have implemented the constructor. Do i have to replace this constructor with the other one, or?

When i implement the constructor next to the other one i get this error:

Implicit super constructor View() is undefined. Must explicitly invoke another constructor

What do i need to code in the body of this constructor? What is AttributeSet for something?
xplosive
Developer
Developer
 
Posts: 26
Joined: Wed Oct 08, 2008 12:21 pm

Postby MrSnowflake » Mon Oct 13, 2008 5:35 pm

you need to call super(context, attrs) as the first statement in your constructor.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby xplosive » Mon Oct 13, 2008 5:40 pm

I replaced the constructor in Mens. The constructor now look like this:

Code: Select all
   public Mens(Context context, AttributeSet attrs)
   {
      super(context);
   }


In the main class i changed this

Code: Select all
ObjMens = new Mens(this);


to this

Code: Select all
ObjMens = new Mens(this, null);


Now i don't get any errors and i see my drawing. But when i want to add another View class:

Code: Select all
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        LinearLayout layMain = (LinearLayout)findViewById(R.id.layMain);
        Mens ObjMens = (Mens)findViewById(R.id.myView);
        ObjMens = new Mens(this, null);
        layMain.addView(ObjMens);
        [b]layMain.addView(new SampleView(this));[/b]
       
    }


I don't see the drawing of SampleView. SampleView is an inner class of the main class. But i don't see it on the screen. How is that?

I think it is strange because when i do this:

Code: Select all
setContentView( new SampleView(this))


it ain't a problem. Why is that?

NB: Android and views, men i have a lot to learn. At this time everything is acracadabra....
xplosive
Developer
Developer
 
Posts: 26
Joined: Wed Oct 08, 2008 12:21 pm

Postby MrSnowflake » Mon Oct 13, 2008 6:03 pm

Maybe, because you add it to the layout the previous object in the layout has layout_height="fill_parent" which will make your SampleView appear out side of the visible space of the layout.

And the docs are there to help. Though they don't really explain how to inflate a custom view from xml, the code in the docs is deprecated.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby xplosive » Mon Oct 13, 2008 6:10 pm

Now i don't understand nothing anymore.

When i erase all my code in the onCreate method:

Code: Select all
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
    }


I still see the drawing of my Mens class!! How is this possible?? Is it because the main.xml? Thus when you declare a View (or something similar) in the main.xml it will be displayed? But what if you declare something in the main.xml and you don't want it to be visible.. what do you have to do?

Pff i don't understand..
xplosive
Developer
Developer
 
Posts: 26
Joined: Wed Oct 08, 2008 12:21 pm

Top

Postby MrSnowflake » Mon Oct 13, 2008 6:19 pm

from the docs:

public void setVisibility(int visibility)
Set the enabled state of this view.
Parameters
visibility One of VISIBLE, INVISIBLE, or GONE.

View.setVisibility() lets you make visible, invisible and didsapear the view. GONE means invisible and not thanking space in the parentview.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby xplosive » Mon Oct 13, 2008 6:22 pm

And where do i have to put that? In the Mens class? If so.. my Mens class is already visible. But my SampleView class ain't. So i have to put the method in the innerclass? And which code do i have to put in the body?
xplosive
Developer
Developer
 
Posts: 26
Joined: Wed Oct 08, 2008 12:21 pm

Postby MrSnowflake » Mon Oct 13, 2008 6:54 pm

You should call it from wherever you want your View to be hidden. The function is View.setVisibility(), which means it works on the View and you don't need to implement it.

I seriously suggest you to take a look at the docs, the Notepad tutorial and some of the Sample applications in the SDK samples dir.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby xplosive » Mon Oct 13, 2008 9:03 pm

I'm reading the documentation now.

But the last thing i want to know is: How can i show 2 view classes? The point is, i have a matrix (draw in class1) and i want to draw a object in a cell (by coordinates). The object is drawed in another class(class2).

What do i have to do when i want to draw the matrix first and after that draw an object somewhere in the matrix.

This is what i want:

The matrix: (don't know the exact englisch term for it)

| X |---|---|---|
|---|---|---|---|
|---|---|---|---|
|---|---|---|---|
|---|---|---|---|

Where X is the object (class2).

How do i this? (the idea is to move the object by the arrow keys)

Thnx for all the help :)
xplosive
Developer
Developer
 
Posts: 26
Joined: Wed Oct 08, 2008 12:21 pm

Postby MrSnowflake » Mon Oct 13, 2008 10:10 pm

That is something you have to handle yourself. If you draw the matrix yourself, you should position object2 yourself within the matrix and draw the object's representation in the draw() of object1.

Maybe I can give you a tip: If you are doing graphics, you should use a canvas. Take a look at my DroidGaming System and DroidGamingExample in the svn. That's a more appropiote way to do heavy drawing. Your way works too and you can keep using that, which could be a smart thing, considering you are still learning, but just look at the code of DroidGamingExample and you'll see an alternative way of doing the drawing in android.

For the record: I have no problem with you asking a lot of questions, but in case of developing you should try to look in the docs, because that way your development will speed up a lot :).
I have always 2 google Chrome windows open: 1 for personal stuff and the other with the android docs open (especially the Class list).
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby xplosive » Tue Oct 14, 2008 10:28 am

I appreciate all the helping and i know you don't have a problem helping me. But the point is, the way Android build it's applications is for me a different manner of thinking. And there's not so much information avalaible. And the information that is available i don't understand much of it.

Anyway, thnx for all the help :)
xplosive
Developer
Developer
 
Posts: 26
Joined: Wed Oct 08, 2008 12:21 pm

Postby MrSnowflake » Tue Oct 14, 2008 10:32 am

Android development seems indeed a lot different from ordinary desktop development, but once you have you own look at the Activity, gui, ... it's pretty convenient. It takes time though.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby Niketa » Tue Dec 02, 2008 7:21 am

Hi,
My problem is also something like that. my code is as below. and m very new to openGL so please help me if
possible.

Actually m facing same error like

12-02 11:27:47.172: ERROR/AndroidRuntime(2223): Uncaught handler: thread main exiting due to uncaught exception
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.apis.graphics/com.example.android.apis.graphics.SurfaceViewOverlay}: android.view.InflateException: Binary XML file line #65: Error inflating class com.example.android.apis.graphics.MyCustomButton
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2156)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at android.app.ActivityThread.access$1800(ActivityThread.java:112)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1580)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at android.os.Handler.dispatchMessage(Handler.java:88)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at android.os.Looper.loop(Looper.java:123)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at android.app.ActivityThread.main(ActivityThread.java:3742)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at java.lang.reflect.Method.invokeNative(Native Method)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at java.lang.reflect.Method.invoke(Method.java:515)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:497)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at dalvik.system.NativeStart.main(Native Method)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): Caused by: android.view.InflateException: Binary XML file line #65: Error inflating class com.example.android.apis.graphics.MyCustomButton
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at android.view.LayoutInflater.createView(LayoutInflater.java:502)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:564)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at android.view.LayoutInflater.rInflate(LayoutInflater.java:617)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at android.view.LayoutInflater.rInflate(LayoutInflater.java:620)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at android.view.LayoutInflater.rInflate(LayoutInflater.java:620)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:227)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at android.app.Activity.setContentView(Activity.java:1569)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at com.example.android.apis.graphics.SurfaceViewOverlay.onCreate(SurfaceViewOverlay.java:37)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1122)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2103)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): ... 11 more
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): Caused by: java.lang.NoSuchMethodException: MyCustomButton
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at java.lang.Class.getMatchingConstructor(Class.java:537)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at java.lang.Class.getConstructor(Class.java:401)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at android.view.LayoutInflater.createView(LayoutInflater.java:474)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): ... 23 more





My code is:

Code: Select all
public class SurfaceViewOverlay extends Activity {
    View mVictimContainer;
    View mVictim1;
    View mVictim2;
    View v1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.surface_view_overlay);

        GLSurfaceView glSurfaceView =
            (GLSurfaceView) findViewById(R.id.glsurfaceview);
            glSurfaceView.setRenderer(new CubeRenderer(false));
 

        // Find the views whose visibility will change
       mVictimContainer = findViewById(R.id.hidecontainer);
       mVictim1 = findViewById(R.id.hideme1);       
             
       MyCustomButton mcb = (MyCustomButton)findViewById(R.id.myButton);
       mcb = new MyCustomButton(this);
       
         mVictim1.setOnClickListener(new HideMeListener(mVictim1));
        mVictim2 = findViewById(R.id.hideme2);
        mVictim2.setOnClickListener(new HideMeListener(mVictim2));
   
        // Find our buttons
        Button visibleButton = (Button) findViewById(R.id.vis);
        Button invisibleButton = (Button) findViewById(R.id.invis);
        Button goneButton = (Button) findViewById(R.id.gone);

        // Wire each button to a click listener
        visibleButton.setOnClickListener(mVisibleListener);
        invisibleButton.setOnClickListener(mInvisibleListener);
        goneButton.setOnClickListener(mGoneListener);
    }

    @Override
    protected void onResume() {
        // Ideally a game should implement onResume() and onPause()
        // to take appropriate action when the activity looses focus
        super.onResume();
    }

    @Override
    protected void onPause() {
        // Ideally a game should implement onResume() and onPause()
        // to take appropriate action when the activity looses focus
        super.onPause();
    }

    class HideMeListener implements OnClickListener {
        final View mTarget;

        HideMeListener(View target) {
            mTarget = target;
        }

        public void onClick(View v) {
            mTarget.setVisibility(View.INVISIBLE);
        }

    }

    OnClickListener mVisibleListener = new OnClickListener() {
        public void onClick(View v) {
            mVictim1.setVisibility(View.VISIBLE);
            mVictim2.setVisibility(View.VISIBLE);
           
            v1.setVisibility(View.VISIBLE);
            mVictimContainer.setVisibility(View.VISIBLE);
           
        }
    };

    OnClickListener mInvisibleListener = new OnClickListener() {
        public void onClick(View v) {
            mVictim1.setVisibility(View.INVISIBLE);
            mVictim2.setVisibility(View.INVISIBLE);
            mVictimContainer.setVisibility(View.INVISIBLE);
        }
    };

    OnClickListener mGoneListener = new OnClickListener() {
        public void onClick(View v) {
            mVictim1.setVisibility(View.GONE);
            mVictim2.setVisibility(View.GONE);
            mVictimContainer.setVisibility(View.GONE);
        }
    };
}



My MyCustomButton class is:

Code: Select all
public class MyCustomButton extends Button {

    static final int StateDefault = 0;
    static final int StateFocused = 1;
    static final int StatePressed = 2;

    private int mState = StateDefault;
         private Bitmap mBitmapDefault;
         private Bitmap mBitmapFocused;
         private Bitmap mBitmapPressed;
         private String mCaption;

         public MyCustomButton(Context context) {
         super(context);
         
         setClickable(true);
         mBitmapDefault = Bitmap.createBitmap(900, 500, Config.ARGB_4444);
         mBitmapFocused = Bitmap.createBitmap(900, 500, Config.ARGB_4444);
         mBitmapPressed = Bitmap.createBitmap(900, 500, Config.ARGB_4444);
         
         // create the Canvas
         Canvas canvas = new Canvas();
 
         canvas.setBitmap(mBitmapDefault);
         
         // create the Drawing Tool (Brush)
         Paint paint = new Paint();
         paint.setAntiAlias(true);  // for a nicer paint
         
          paint.setColor(Color.GRAY);
           paint.setStrokeWidth(3);
           paint.setStyle(Style.FILL);
           Path path = new Path();
           path.addRoundRect(new RectF(20, 20, 300, 120),
                   10, 10, Direction.CCW);
           canvas.drawPath(path, paint);
         
           Paint paintText = new Paint();
         paintText.setAntiAlias(true);
         paintText.setTextSize(20);
         paintText.setColor(0xffffffff);  // white
         
         // draw Text
         canvas.drawText("OK", 30, 55, paintText);
         // for the Pressed Image
         canvas.setBitmap(mBitmapPressed);
         paint.setColor(Color.BLACK);
         paintText.setColor(Color.WHITE); 
         canvas.drawPath(path, paint);
         canvas.drawText("OK", 30, 55, paintText);

         // for the Pressed Image
         canvas.setBitmap(mBitmapFocused);
         
         // Blue Color
         paint.setColor(Color.BLUE);
         paintText.setColor(Color.WHITE);  // white Line
         canvas.drawPath(path, paint);
         canvas.drawText("OK", 30, 55, paintText);

         // define OnClickListener for the Button
         setOnClickListener(onClickListener);
         
    }

         @Override
         protected void onDraw(Canvas canvas) {
              switch (mState) {
              case StateDefault:
                   canvas.drawBitmap(mBitmapDefault, 0, 0, null);
                   break;
              case StateFocused:
                   canvas.drawBitmap(mBitmapFocused, 0, 0, null);
                   break;
              case StatePressed:
                   canvas.drawBitmap(mBitmapPressed, 0, 0, null);
                   break;
              }
         }
         
         @Override
         protected void drawableStateChanged() {
              if (isPressed()) {
                   mState = StatePressed;
              } else if (hasFocus()) {
                   mState = StateFocused;
              } else {
                   mState = StateDefault;
              }
              // force the redraw of the Image
              // onDraw will be called!
              invalidate();
         }
         
         private OnClickListener onClickListener =
              new OnClickListener() {
              public void onClick(View arg0) {
                 Log.i("GameProfile","text:"+mCaption);
              }
         };

   }




My XML is:

Code: Select all
<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

    <!-- Here is where we put the SurfaceView, in a frame so that we can
         stack other views on top of it. -->
    <FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="0px"
            android:layout_weight="1">

        <com.example.android.apis.graphics.GLSurfaceView
              android:id="@+id/glsurfaceview"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />

        <LinearLayout android:id="@+id/hidecontainer"
                android:orientation="vertical"
                android:visibility="visible"
                android:gravity="center"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

            <Button android:id="@+id/hideme1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:visibility="visible"
                    android:text="@string/hide_me"/>
                                                     
            <Button android:id="@+id/hideme2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:visibility="visible"
                    android:text="@string/hide_me"/>

          <Button android:id="@+id/hideme3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:visibility="visible"
                    android:text="@string/hide_me"/>
                   
        <com.example.android.apis.graphics.MyCustomButton
              android:id="@+id/myButton"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="@string/myButton" />
       
         
        </LinearLayout>

    </FrameLayout>

    <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center">

        <Button android:id="@+id/vis"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/visibility_1_vis"/>

        <Button android:id="@+id/invis"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/visibility_1_invis"/>

        <Button android:id="@+id/gone"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/visibility_1_gone"/>

    </LinearLayout>

</LinearLayout>



Thanks in advance.

Note:
1. i have used example surfaceview overlay, custombutton by mauri, to implement this.
2. i have also did entry in string.xml .
Niketa
Developer
Developer
 
Posts: 36
Joined: Wed Sep 17, 2008 12:37 pm

Postby sunjavaduke » Wed May 27, 2009 4:27 am

plusminus wrote:
MrSnowflake wrote:
plusminus wrote:The activity needs to know what it display. So in the very beginning (after super.onCreate();) call:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. setContentView(R.layout.main);
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Usually you would also add the view in xml before.
What he sais, or setContentView(layMain);


Then [font=Courier New]findViewById[/font] would fail :P


That's right.
You have to first call setContentView(), then call the findViewById().
An alternative way is using inflate method: android.view.LayoutInflater#inflate()
The great thing in the world is not so much where we stand, as in what direction we are moving. - by Oliver Wendell Holmes
User avatar
sunjavaduke
Freshman
Freshman
 
Posts: 5
Joined: Tue Apr 21, 2009 11:09 am
Location: Shanghai, China

Top
Previous

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Google Feedfetcher, Yahoo [Bot] and 4 guests