Code showing no errors but getting a force close when ran?

Put your problem here if it does not fit any of the other categories.

Code showing no errors but getting a force close when ran?

Postby xcallmejudasx » Mon Oct 12, 2009 4:18 pm

I'm new to Android but have a few years of experience in Java and need some help explaining why Friday my code ran and Monday morning it isn't.

I'm developing using
Eclipse 1.5(Ganymede)
Default AVD
Android 1.5
Java 1.6

Here is my code. I'm not receiving any errors in Eclipse but whenever I launch it I'm required to force close with the error "The application has stopped unexpectedly"

Code: Select all
package org.carsonrs;

import android.app.Activity;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class myLight extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        View v = findViewById(android.R.id.background);
        Canvas c = new Canvas();
        v.draw(c);
       

       
        Gallery g = (Gallery) findViewById(R.id.gallery);
        g.setAdapter(new ImageAdapter(this));
        g.setSpacing(10);
        g.setPadding(5, 300, 5, 5);
        g.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position, long id) {
                Toast.makeText(myLight.this, "" + position, Toast.LENGTH_SHORT).show();
            }
        });
    }
}


Code: Select all
package org.carsonrs;

import android.content.Context;
import android.content.res.TypedArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    private Context mContext;

    private Integer[] mImageIds = {
            R.drawable.icon,
            R.drawable.cryptography,
            R.drawable.picture_6,
            R.drawable.whypeopleseemtohavefreetime,
    };

    public ImageAdapter(Context c) {
        this.mContext = c;
    //    TypedArray a = obtainStyledAttributes(android.R.style.Theme);
    //    mGalleryItemBackground = a.getResourceId(android.R.style.Theme_Dialog, 0);
    //    a.recycle();
    }

    public int getCount() {
        return mImageIds.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(this.mContext);

        i.setImageResource(this.mImageIds[position]);
        i.setLayoutParams(new Gallery.LayoutParams(150, 100));
        i.setScaleType(ImageView.ScaleType.FIT_XY);
        i.setBackgroundResource(mGalleryItemBackground);

        return i;
    }
}


And here is my XML file
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"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />   

<Gallery
   android:id ="@+id/gallery"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="bottom"
   />   
</LinearLayout>

xcallmejudasx
Freshman
Freshman
 
Posts: 3
Joined: Tue Oct 06, 2009 8:25 pm
Location: Houston, TX

Top

Postby chimaera » Thu Oct 15, 2009 9:11 pm

It looks like mGalleryItemBackground is not initialized.

Don't you have and exception showing in the logcat ?
chimaera
Junior Developer
Junior Developer
 
Posts: 22
Joined: Fri Jun 19, 2009 1:42 am

Postby qlimax » Thu Oct 15, 2009 9:51 pm

try to go (in eclipse) in the debug perspective...

there is a tab called LogCat, this tab will show you what's the exception you have to deal with


cya!
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Top

Return to Other Coding-Problems

Who is online

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