2011-07-10 48 views
0

我想實現詮釋他的下面幾行的Android庫屬性

無法加載屬性文件的項目「HelloGallery」

的應用HelloGallery從http://developer.android.com/resources/tutorials/views/hello-gallery.html

Android圖庫例子,我得到這個錯誤(進程com.hellogallery)意外停止,請再試一次。

如何解決此問題。代碼如下所示。

HelloGallery.java

package com.HelloGallery; 

import android.app.Activity; 
import android.content.Context; 
import android.content.res.TypedArray; 
import android.os.Bundle; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.BaseAdapter; 
import android.widget.Gallery; 
import android.widget.ImageView; 
import android.widget.Toast; 
import android.widget.AdapterView.OnItemClickListener; 
import android.R.drawable; 

public class HelloGalleryActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 

Gallery g = (Gallery) findViewById(R.id.gallery); 
g.setAdapter(new ImageAdapter(this)); 

g.setOnItemClickListener(new OnItemClickListener() { 
    public void onItemClick(AdapterView parent, View v, int position, long id) { 
    Toast.makeText(HelloGalleryActivity.this, "" + position, Toast.LENGTH_SHORT).show(); 
    } 
}); 


    } 

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

private Integer[] mImageIds = { 
    R.drawable.sample_1, 
    R.drawable.sample_2, 
    R.drawable.sample_3, 
    R.drawable.sample_4, 
    R.drawable.sample_5, 
    R.drawable.sample_6, 
    R.drawable.sample_7 
}; 

public ImageAdapter(Context c) { 
    mContext = c; 
    TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery); 
    mGalleryItemBackground = a.getResourceId(
     R.styleable.HelloGallery_android_galleryItemBackground, 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(mContext); 

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

    return i; 
} 
    } 
} 

main.mxml

<?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 xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gallery" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
/> 
</LinearLayout> 

attrs.mxml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="HelloGallery"> 
<attr name="android:galleryItemBackground" /> 
    </declare-styleable> 
</resources> 

R.java

/* AUTO-GENERATED FILE. DO NOT MODIFY. 
    * 
* This class was automatically generated by the 
* aapt tool from the resource data it found. It 
* should not be modified by hand. 
*/ 

package com.HelloGallery; 

public final class R { 
    public static final class attr { 
    } 
    public static final class drawable { 
public static final int icon=0x7f020000; 
    } 
    public static final class id { 
public static final int gallery=0x7f050000; 
    } 
    public static final class layout { 
public static final int main=0x7f030000; 
    } 
    public static final class string { 
public static final int app_name=0x7f040001; 
public static final int hello=0x7f040000; 
    } 
    public static final class styleable { 
/** Attributes that can be used with a HelloGallery. 
    <p>Includes the following attributes:</p> 
    <table> 
    <colgroup align="left" /> 
    <colgroup align="left" /> 
    <tr><th>Attribute</th><th>Description</th></tr> 
    <tr><td><code>{@link #HelloGallery_android_galleryItemBackground com.HelloGallery:android_galleryItemBackground}</code></td><td></td></tr> 
    </table> 
    @see #HelloGallery_android_galleryItemBackground 
    */ 
public static final int[] HelloGallery = { 
    0x0101004c 
}; 
/** 
    <p>This symbol is the offset where the {@link com.HelloGallery.R.attr#android_galleryItemBackground} 
    attribute's value can be found in the {@link #HelloGallery} array. 
    @attr name android:android_galleryItemBackground 
*/ 
public static final int HelloGallery_android_galleryItemBackground = 0; 
    }; 
} 
+0

它正在拋出哪條線,並且是否複製/重現項目的所有資源等? – Idistic

+0

這裏沒有錯誤。我是android和eclipse的新手。錯誤日誌表示如上所示的錯誤 – Rajeev

+0

如果您使用的是eclipse,那麼錯誤將會在LogCat窗口中輸出,該窗口會告訴您問題。此外,如果使用eclipse,則應該使用項目中的「debug as」逐步完成代碼。屬性文件通常是在創建項目時生成的,並且它具有android SDK級別,而不是其他的。你是如何創建這個項目的,你是加載它還是複製/剪切/粘貼它? – Idistic

回答

0

你已經在庫中的線性佈局aswellas中給出了xmlns:android =「http://schemas.android.com/apk/res/android」。只需要在線性佈局中給出它。可能這會加載你的XML文件的問題。 如果不是,請將您的錯誤發佈在logcat窗口中。