Cannot draw a simple line

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

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

I don't really understand how to add a View in my main.xml I don't know how to do it.
Do i need to do something like this in my main.xml?

Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:id="@+id/layMain"
    >
   
  [b]  <View
       android:id="@+id/myView"
       />[/b]
</LinearLayout>



How do i add a view in my main.xml and how do i use it in the code? I mean i have also an external class which draw something. So i have to bind those 2 together somehow? I don't understand.

Hope you can help me (again :))

Thnx
xplosive
Developer
Developer
 
Posts: 26
Joined: Wed Oct 08, 2008 12:21 pm

Top

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

As this is a completely different mather it should be in a new thread, but I'll still help you :p.
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2.  
  3.  
  4. <?xml version="1.0" encoding="utf-8"?>
  5.  
  6. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  7.  
  8.    android:orientation="horizontal"
  9.  
  10.    android:layout_width="fill_parent"
  11.  
  12.    android:layout_height="fill_parent"
  13.  
  14.    android:gravity="center"
  15.  
  16.    android:id="@+id/layMain"
  17.  
  18.    >
  19.  
  20.    
  21.  
  22.   <View
  23.  
  24.       android:id="@+id/myView"
  25.  
  26.       android:layout_width="fill_parent"
  27.  
  28.       android:layout_height="wrap_content"
  29.  
  30.       />
  31.  
  32. </LinearLayout>
  33.  
  34.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4

And use it in your code by: (from your activity):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. TextView textView = (TextView)findViewById(R.id.myView);
  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

Postby xplosive » Mon Oct 13, 2008 1:49 pm

MrSnowflake wrote:As this is a completely different mather it should be in a new thread, but I'll still help you :p.
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.    android:orientation="horizontal"
  5.    android:layout_width="fill_parent"
  6.    android:layout_height="fill_parent"
  7.    android:gravity="center"
  8.    android:id="@+id/layMain"
  9.    >
  10.    
  11.   <View
  12.       android:id="@+id/myView"
  13.       android:layout_width="fill_parent"
  14.       android:layout_height="wrap_content"
  15.       />
  16. </LinearLayout>
  17.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4

And use it in your code by: (from your activity):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. TextView textView = (TextView)findViewById(R.id.myView);
  2.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Hehe, thnx for helping me, appreciate it :)

You use TextView.. But instead of using textView i suppose i can use my own (view)class? (where i draw something) (cannot test it right now, so that's why i ask..)

Thnx
xplosive
Developer
Developer
 
Posts: 26
Joined: Wed Oct 08, 2008 12:21 pm

Postby MrSnowflake » Mon Oct 13, 2008 2:01 pm

Yeah of course that's possible. You should extend TextView (class MyTextView extends TextView) and override its default draw().

in the layout xml you should do:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <your.full.packagename.MyTextView
  2. .... />
  3.  
Parsed in 0.000 seconds, using GeSHi 1.0.8.4

Just not that when you want to do really complex graphics you should do it in a seperate thread. Take a look at my GameTemplate or the LunarLander demo for more information about this. Because extending the draw() of a View is not hardware accelerated where using a surfaceView is.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby xplosive » Mon Oct 13, 2008 4:07 pm

I still don't get it work. What i have:

main.xml:

Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:id="@+id/layMain"
    >
   
    <View
       android:id="@+id/myView"
       android:layout_width="fill_parent"
          android:layout_height="wrap_content"
       />
</LinearLayout>



And de java code looks like this:

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);
        layMain.addView(ObjMens);
       
    }


The Mens class:

Code: Select all
package android.happer;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Bitmap.Config;
import android.view.View;

public class Mens extends View {
   
   public Mens(Context context)
   {
      super(context);
      
   }
   

   protected void onDraw(Canvas canvas)
    {
      Paint p = new Paint();
      p.setColor(Color.RED);
      
      canvas.drawLine(15, 15, 25, 5, p);
      
      
    }
}



So what i want is to add the Mens class to my layout. I tried several things, but don't get it work. So what am i doing wrong?

Thnx
xplosive
Developer
Developer
 
Posts: 26
Joined: Wed Oct 08, 2008 12:21 pm

Postby MrSnowflake » Mon Oct 13, 2008 4:08 pm

Your layout xml would need to be:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.    android:orientation="horizontal"
  7.  
  8.    android:layout_width="fill_parent"
  9.  
  10.    android:layout_height="fill_parent"
  11.  
  12.    android:gravity="center"
  13.  
  14.    android:id="@+id/layMain"
  15.  
  16.    >
  17.  
  18.    
  19.  
  20.     <android.happer.Mens
  21.  
  22.       android:id="@+id/myView"
  23.  
  24.       android:layout_width="fill_parent"
  25.  
  26.          android:layout_height="wrap_content"
  27.  
  28.       />
  29.  
  30. </LinearLayout>
  31.  
  32.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4
Last edited by MrSnowflake on Mon Oct 13, 2008 4:47 pm, edited 1 time in total.
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 4:16 pm

Thnx for the quick reply, but it didn't do the trick.

I don't get any errors, but in the emulator i get the error:

Sorry!

The application Happer has stopped unexpectedly. Please try again.

I have nothing changed in the Java code, but i have change the main.xml as you suggested. Is main.xml case sensitive?
xplosive
Developer
Developer
 
Posts: 26
Joined: Wed Oct 08, 2008 12:21 pm

Postby MrSnowflake » Mon Oct 13, 2008 4:29 pm

Then you have to look at the adb logcat.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby xplosive » Mon Oct 13, 2008 4:33 pm

Where can i find the adblogcat? (i use eclipse whit the adt plugin installed) I found DDMS, but i can't find adb.
xplosive
Developer
Developer
 
Posts: 26
Joined: Wed Oct 08, 2008 12:21 pm

Postby MrSnowflake » Mon Oct 13, 2008 4:36 pm

In the DDMS view the LogCat view.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby xplosive » Mon Oct 13, 2008 4:38 pm

10-13 15:36:05.070: ERROR/AndroidRuntime(355): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.happer/android.happer.Happer}: android.view.InflateException: Binary XML file line #10: Error inflating class android.happer.mens

10-13 15:36:05.070: ERROR/AndroidRuntime(355): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
10-13 15:36:05.070: ERROR/AndroidRuntime(355): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2156)
10-13 15:36:05.070: ERROR/AndroidRuntime(355): at android.app.ActivityThread.access$1800(ActivityThread.java:112)
10-13 15:36:05.070: ERROR/AndroidRuntime(355): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1580)
10-13 15:36:05.070: ERROR/AndroidRuntime(355): at android.os.Handler.dispatchMessage(Handler.java:88)
10-13 15:36:05.070: ERROR/AndroidRuntime(355): at android.os.Looper.loop(Looper.java:123)
10-13 15:36:05.070: ERROR/AndroidRuntime(355): at android.app.ActivityThread.main(ActivityThread.java:3742)
10-13 15:36:05.070: ERROR/AndroidRuntime(355): at java.lang.reflect.Method.invokeNative(Native Method)
10-13 15:36:05.070: ERROR/AndroidRuntime(355): at java.lang.reflect.Method.invoke(Method.java:515)
10-13 15:36:05.070: ERROR/AndroidRuntime(355): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
10-13 15:36:05.070: ERROR/AndroidRuntime(355): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:497)
10-13 15:36:05.070: ERROR/AndroidRuntime(355): at dalvik.system.NativeStart.main(Native Method)
10-13 15:36:05.070: ERROR/AndroidRuntime(355): Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class android.happer.mens
10-13 15:36:05.070: ERROR/AndroidRuntime(355): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:575)
10-13 15:36:05.070: ERROR/AndroidRuntime(355): at android.view.LayoutInflater.rInflate(LayoutInflater.java:617)
10-13 15:36:05.070: ERROR/AndroidRuntime(355): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
10-13 15:36:05.070: ERROR/AndroidRuntime(355): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
10-13 15:36:05.070: ERROR/AndroidRuntime(355): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)

10-13 15:36:05.070: ERROR/AndroidRuntime(355): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:227)
10-13 15:36:05.070: ERROR/AndroidRuntime(355): at android.app.Activity.setContentView(Activity.java:1569)
10-13 15:36:05.070: ERROR/AndroidRuntime(355): at android.happer.Happer.onCreate(Happer.java:19)
10-13 15:36:05.070: ERROR/AndroidRuntime(355): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1122)
10-13 15:36:05.070: ERROR/AndroidRuntime(355): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2103)
10-13 15:36:05.070: ERROR/AndroidRuntime(355): ... 11 more
10-13 15:36:05.070: ERROR/AndroidRuntime(355): Caused by: java.lang.ClassNotFoundException: android.happer.mens in loader dalvik.system.PathClassLoader@433e6fe0
10-13 15:36:05.070: ERROR/AndroidRuntime(355): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:215)
10-13 15:36:05.070: ERROR/AndroidRuntime(355): at java.lang.ClassLoader.loadClass(ClassLoader.java:453)
10-13 15:36:05.070: ERROR/AndroidRuntime(355): at java.lang.ClassLoader.loadClass(ClassLoader.java:421)

I'm sorry for the mess, but can you make something of this?
xplosive
Developer
Developer
 
Posts: 26
Joined: Wed Oct 08, 2008 12:21 pm

Postby MrSnowflake » Mon Oct 13, 2008 4:40 pm

Post your layout xml too.

Well the error is: ERROR/AndroidRuntime(355): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.happer/android.happer.Happer}: android.view.InflateException: Binary XML file line #10: Error inflating class android.happer.mens
Saying it can't make a view of android.happer.mens

Oh I see now: You need to name the view in your layout xml android.happer.Mens With a capital!!!
Don't just copy-paste, keep your head with it, you're here to learn :P.
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:03 pm

So it is case sensitive :P Anyway, i changed it to a capitol, but it didn't have any effect.

My complete main.xml:

Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:id="@+id/layMain"
    >
   
    <android.happer.Mens
       android:id="@+id/myView"
       android:layout_width="fill_parent"
          android:layout_height="wrap_content"
       />
</LinearLayout>



And here the onCreate method:

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);
        layMain.addView(ObjMens);
       
    }


This is the whole error:

Code: Select all
10-13 15:58:42.919: ERROR/AndroidRuntime(391): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.happer/android.happer.Happer}: android.view.InflateException: Binary XML file line #10: Error inflating class android.happer.Mens
10-13 15:58:42.919: ERROR/AndroidRuntime(391):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
10-13 15:58:42.919: ERROR/AndroidRuntime(391):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2156)
10-13 15:58:42.919: ERROR/AndroidRuntime(391):     at android.app.ActivityThread.access$1800(ActivityThread.java:112)
10-13 15:58:42.919: ERROR/AndroidRuntime(391):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1580)
10-13 15:58:42.919: ERROR/AndroidRuntime(391):     at android.os.Handler.dispatchMessage(Handler.java:88)
10-13 15:58:42.919: ERROR/AndroidRuntime(391):     at android.os.Looper.loop(Looper.java:123)
10-13 15:58:42.919: ERROR/AndroidRuntime(391):     at android.app.ActivityThread.main(ActivityThread.java:3742)
10-13 15:58:42.919: ERROR/AndroidRuntime(391):     at java.lang.reflect.Method.invokeNative(Native Method)
10-13 15:58:42.919: ERROR/AndroidRuntime(391):     at java.lang.reflect.Method.invoke(Method.java:515)
10-13 15:58:42.919: ERROR/AndroidRuntime(391):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
10-13 15:58:42.919: ERROR/AndroidRuntime(391):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:497)
10-13 15:58:42.919: ERROR/AndroidRuntime(391):     at dalvik.system.NativeStart.main(Native Method)
10-13 15:58:42.919: ERROR/AndroidRuntime(391): Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class android.happer.Mens
10-13 15:58:42.919: ERROR/AndroidRuntime(391):     at android.view.LayoutInflater.createView(LayoutInflater.java:502)
10-13 15:58:42.919: ERROR/AndroidRuntime(391):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:564)
10-13 15:58:42.919: ERROR/AndroidRuntime(391):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:617)
10-13 15:58:42.919: ERROR/AndroidRuntime(391):     at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
10-13 15:58:42.919: ERROR/AndroidRuntime(391):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
10-13 15:58:42.919: ERROR/AndroidRuntime(391):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
10-13 15:58:42.919: ERROR/AndroidRuntime(391):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:227)
10-13 15:58:42.919: ERROR/AndroidRuntime(391):     at android.app.Activity.setContentView(Activity.java:1569)
10-13 15:58:42.919: ERROR/AndroidRuntime(391):     at android.happer.Happer.onCreate(Happer.java:19)
10-13 15:58:42.919: ERROR/AndroidRuntime(391):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1122)
10-13 15:58:42.919: ERROR/AndroidRuntime(391):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2103)
10-13 15:58:42.919: ERROR/AndroidRuntime(391):     ... 11 more
10-13 15:58:42.919: ERROR/AndroidRuntime(391): Caused by: java.lang.NoSuchMethodException: Mens
10-13 15:58:42.919: ERROR/AndroidRuntime(391):     at java.lang.Class.getMatchingConstructor(Class.java:537)
10-13 15:58:42.919: ERROR/AndroidRuntime(391):     at java.lang.Class.getConstructor(Class.java:401)

xplosive
Developer
Developer
 
Posts: 26
Joined: Wed Oct 08, 2008 12:21 pm

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

Do you implement this constructor:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public Mens(Context context, AttributeSet attrs)
  3.  
  4.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
Last edited by MrSnowflake on Mon Oct 13, 2008 5:25 pm, edited 1 time in total.
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:10 pm

No i didn't. I don't have a DroidGamingView class. I only have the main class and a Mens class (and the xml layout file)

The only thing i want is to draw something in the Mens class and show that drawing in the main class on a layout.
xplosive
Developer
Developer
 
Posts: 26
Joined: Wed Oct 08, 2008 12:21 pm

Top
PreviousNext

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 8 guests