我在运行程序时得到了膨胀异常二进制XML文件行#9。我不知道这里有什么问题。片段类显示在我的主要活动中。我在运行程序时遇到了这个异常。
这是我的代码:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
public class MenuItems extends Fragment {
ArrayList<Contact> imageArry = new ArrayList<Contact>();
int[] img = { R.drawable.ice, R.drawable.idle, R.drawable.pongal };
String[] name = { "ice cream", "idle", "pongal" };
String sol = "";
int idnum;
DataBaseHandler db;
public static MenuItems newInstance() {
MenuItems f = new MenuItems();
return f;
}
@SuppressLint("NewApi")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
for (int i = 0; i < img.length; i++) {
sol = name[i];
// idnum=idnumber[i];
//
db = new DataBaseHandler(getActivity());
// get image from drawable
Bitmap image = BitmapFactory.decodeResource(getResources(),
setImageResource(img[i]));
// convert bitmap to byte
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte imageInByte[] = stream.toByteArray();
/**
* CRUD Operations
* */
// Inserting Contacts
Log.d("Insert: ", "Inserting ..");
db.addContact(new Contact(sol, imageInByte));
// display main List view bcard and contact name
// Reading all contacts from database
System.out.println(sol);
System.out.println(setImageResource(img[i]));
}
List<Contact> contacts = db.getAllContacts();
for (Contact cn : contacts) {
String log = "ID:" + cn.getID() + " Name: " + cn.getName()
+ " ,Image: " + cn.getImage();
// Writing Contacts to log
Log.d("Result: ", log);
// add contacts data in arrayList
imageArry.add(cn);
}
View rootView = inflater
.inflate(R.layout.firstscreen, container, false);
ListView lv = (ListView) rootView.findViewById(R.id.listView1);
MenuItemsListAdapter adapter = new MenuItemsListAdapter(getActivity(),
imageArry);
for (int a = 0; a < imageArry.size(); a++) {
System.out.println("IndeXXXXXXXXXXXXXXXX");
System.out.println(imageArry.get(a));
}
return container;
}
private int setImageResource(int i) {
// TODO Auto-generated method stub
return i;
}
}
// //////////////////////////////////////////////////////////
public class MenuItemsListAdapter extends BaseAdapter {
ArrayList<Contact> data = new ArrayList<Contact>();
private Context context;
// private String[]number;
// private int[]imageid;
public MenuItemsListAdapter(Context c, ArrayList<Contact> data) {
context = c;
// this.imageid=imageid;
// this.number=number;
this.data = data;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View list;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
list = new View(context);
list = inflater.inflate(R.layout.firstsub, null);
TextView textView = (TextView) list.findViewById(R.id.text);
ImageView imageView = (ImageView) list
.findViewById(R.id.imageView1);
Contact picture = data.get(position);
textView.setText(picture._name);
// convert byte to bitmap take from contact class
byte[] outImage = picture._image;
ByteArrayInputStream imageStream = new ByteArrayInputStream(
outImage);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
imageView.setImageBitmap(theImage);
} else {
list = (View) convertView;
}
return convertView;
}
}
/////////////////////////////////////////////////////
My xml file here
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="27dp"
android:layout_marginTop="14dp"
android:text="TextView" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="35dp"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
main activity xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="10"
android:orientation="vertical" >
<fragment
android:layout_weight="8"
android:name="com.example.balakrishnan.MenuItems"
android:id="@+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="0dp" />
</LinearLayout>
答案 0 :(得分:0)
在为xml填充xml之后写出所有代码,即在此行之后
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater
.inflate(R.layout.firstscreen, container, false);
// Now write your code
for (int i = 0; i < img.length; i++) {
sol = name[i];
// idnum=idnumber[i];