findViewByID

All your problems with Audio, Video and Images.

findViewByID

Postby Ghost » Fri Feb 11, 2011 2:57 pm

Hi,
the following code crashes:
Code: Select all
public class Engine extends Activity {
   
   ImageView m_ImageView;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        requestWindowFeature(Window.FEATURE_NO_TITLE);
       
        m_ImageView = (ImageView) findViewById(R.id.imageView1);
        setContentView(m_ImageView);
    }
}

I have ImageView in main.xml. What is causing this not to work?
I just started learning to develop on android. I am going to make a simple game.
Is it alright to take handles for ImageView and TextView from xml and after editing them, render?
Or should I use something else to draw to and then output?

Thank you,
Martin
Ghost
Developer
Developer
 
Posts: 26
Joined: Fri Feb 11, 2011 2:50 pm

Top

Re: findViewByID

Postby Kerbik » Fri Feb 11, 2011 4:30 pm

Hello Martin,

at first to draw is probably best option use canvas ... you should check 2d/3d tutorials here on forum to check how to draw ...

second ... to find what error you have there on this few lines you have to check your layout xml file ... probably there will be some misstake in name of imageview ;)

probably here is a lot of other peoples who can help you better :) Kerbik
Kerbik
Developer
Developer
 
Posts: 30
Joined: Fri May 28, 2010 6:06 am

Re: findViewByID

Postby Ghost » Fri Feb 11, 2011 4:36 pm

Thank you Kerbik, I'll try searching for tutorials later ;)

Here's my XML code:
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:orientation="vertical">

   <ImageView android:id="@+id/imageView1"
      android:layout_height="fill_parent"
      android:layout_width="fill_parent"
      android:src="@drawable/image">
   </ImageView>
   
      <TextView
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:text="@string/hello"
       android:gravity="center"/>
   
</FrameLayout>


Is it faulty?
Ghost
Developer
Developer
 
Posts: 26
Joined: Fri Feb 11, 2011 2:50 pm

Re: findViewByID

Postby Kerbik » Fri Feb 11, 2011 4:42 pm

No this is ok i didn't find it first time but it is in upper code ...

Code: Select all
m_ImageView = (ImageView) findViewById(R.id.imageView1);


should be
Code: Select all
ImageView m_ImageView = (ImageView) findViewById(R.id.imageView1);


at least i think ... i cant check it right now :)
Kerbik
Developer
Developer
 
Posts: 30
Joined: Fri May 28, 2010 6:06 am

Re: findViewByID

Postby Ghost » Fri Feb 11, 2011 5:40 pm

I'm not used to java, but I think that's not the problem, because i have declared this variable like this:
Code: Select all
ImageView m_ImageView;

I still get exception while running on emulator.
Ghost
Developer
Developer
 
Posts: 26
Joined: Fri Feb 11, 2011 2:50 pm

Re: findViewByID

Postby Shattered9 » Sat Feb 12, 2011 4:43 pm

My guess would be that you need to rearrange these two lines :

Code: Select all
m_ImageView = (ImageView) findViewById(R.id.imageView1);
setContentView(m_ImageView);


To :

Code: Select all
setContentView(m_ImageView);
m_ImageView = (ImageView) findViewById(R.id.imageView1);


Otherwise it seems like it would be trying to find the ImageView but it has nowhere to start looking. Kind of in a rush at the moment, but if that doesn't fix it I will look into it more when I get back. :)
Shattered9
Developer
Developer
 
Posts: 39
Joined: Sat Feb 12, 2011 4:41 pm

Top

Re: findViewByID

Postby Ghost » Sat Feb 12, 2011 5:54 pm

Shattered9 wrote:My guess would be that you need to rearrange these two lines :

Code: Select all
m_ImageView = (ImageView) findViewById(R.id.imageView1);
setContentView(m_ImageView);


To :

Code: Select all
setContentView(m_ImageView);
m_ImageView = (ImageView) findViewById(R.id.imageView1);


Otherwise it seems like it would be trying to find the ImageView but it has nowhere to start looking. Kind of in a rush at the moment, but if that doesn't fix it I will look into it more when I get back. :)

What you have written makes complete sense to me, but did you mean:
Code: Select all
setContentView(R.layout.main);

Because otherwise I don't get it. I don't have exact same code, but I'll try this elswhere :)
BTW, thanks for writing your first post here ;)
Ghost
Developer
Developer
 
Posts: 26
Joined: Fri Feb 11, 2011 2:50 pm

Re: findViewByID

Postby Shattered9 » Sat Feb 12, 2011 6:07 pm

Oops! Yes, sorry about that. I figured I would try to get my first post in before I had to go earlier so I was in a rush, then ended up not having to leave at all. :lol:

And no problem at all! If you're still having a problem with it I will create a test project and help figure it out, I have a hard time figuring out coding problems without working with the code itself most of the time.

Best of luck!
Shattered9
Developer
Developer
 
Posts: 39
Joined: Sat Feb 12, 2011 4:41 pm

Re: findViewByID

Postby Ghost » Sun Feb 13, 2011 7:10 pm

This works :)
Though I could use some help here:
Code: Select all
public class MainView extends SurfaceView implements SurfaceHolder.Callback {

   SurfaceHolder sh;
   MainThread mt;
   
   public MainView(Context context, AttributeSet attrs) {
      super(context, attrs);
      
        sh = getHolder();
        sh.addCallback(this);
        mt = new MainThread();

   }

   @Override
   public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
      // TODO Auto-generated method stub
      
   }

   @Override
   public void surfaceCreated(SurfaceHolder holder) {
   }

   @Override
   public void surfaceDestroyed(SurfaceHolder holder) {
      // TODO Auto-generated method stub
      
   }
   
   class MainThread extends Thread {
      @Override
      public void run() {
           Canvas c = sh.lockCanvas(null);
           if (c != null) {
              c.drawCircle(100, 100, 10, null);
              sh.unlockCanvasAndPost(c);
           }
      }
   }
}

I wanted to atleast have anything drawn on the screen so I could get an idea how everything works, but unfortunately, all I see is default text and image from XML file:
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
   
    <com.RPG.MainView
       android:id="@+id/MainView"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"/>

   <RelativeLayout
      android:layout_height="fill_parent"
      android:layout_width="fill_parent">
      
      <ImageView android:id="@+id/imageView1"
         android:layout_height="fill_parent"
         android:layout_width="fill_parent"
         android:src="@drawable/image">
      </ImageView>
      
      <TextView
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:text="@string/hello"
          android:gravity="center"/>

   </RelativeLayout>
   
</FrameLayout>


My main class looks like this:
Code: Select all
public class Engine extends Activity {
   
   /** Declaration of main thread */
   //private MainThread m_MainThread;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        // Disable title bar.
        requestWindowFeature(Window.FEATURE_NO_TITLE);
       
        // Start our main thread.
        //m_MainThread = new MainThread();
       
        // Display message.
        setContentView(R.layout.main);
        MainView mt = (MainView) findViewById(R.id.MainView);
        Log.e("WARNING", "HFGSFDJSFDJKDFSKD");
    }
   
    /** Called when activity is about to end. */
    @Override
    public void onDestroy() {
       super.onDestroy();    
       
       //try {
          //m_MainThread.stop();
       //}
       //catch (Exception e)
       //{
       //}
    }
}


I was trying to understand lunarlander source from SDK samples by writting this.
I believe, that there is something essential that I don't understand.
Code compiles and doesn't show any signs of errors.

Thank you,
Martin
Ghost
Developer
Developer
 
Posts: 26
Joined: Fri Feb 11, 2011 2:50 pm

Re: findViewByID

Postby Shattered9 » Mon Feb 14, 2011 4:48 am

My computer has been having a fit since this afternoon and not allowing me to compile apps, so I can't mess with it to see what the exact issue is.

Unfortunately I have not had much occasion to work with the canvas yet myself, so I can't say exactly where your problem is coming from, and I don't want to try to give misguided advice. I've seen plenty of very good guides on working with the canvas in various ways though, so a quick search should come up with a tutorial you could follow to learn more about that aspect of Android.

Sorry that I'm not of much direct help on this one. Back to trying to be able to compile apps again. :(
Shattered9
Developer
Developer
 
Posts: 39
Joined: Sat Feb 12, 2011 4:41 pm

Re: findViewByID

Postby Ghost » Mon Feb 14, 2011 5:15 am

Nah, it's ok. I shouldn't be asking this much anyway. I'll try reading tutorials.
I, myself had problems with compiler, though a little ones on windows 7, but was unable to install on ubuntu :(
Hope you fix yours soon ;)
Ghost
Developer
Developer
 
Posts: 26
Joined: Fri Feb 11, 2011 2:50 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 7 guests