2015-03-19 65 views
0

我在运行程序时得到了充气异常二进制XML文件行#9。我不知道这里有什么问题。片段类显示在我的主要活动中。运行我的程序时,我得到了这个异常。充气异常二进制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

膨胀后的XML片段即可以添加firstscreen.xml后? – mustafasevgi 2015-03-19 06:41:52

回答

0

将所有的代码,您此行

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]; 
+0

Mr.Hulk我是根据你的建议更改我的代码。即使它不工作。 – Balakrishnan 2015-03-19 07:27:43

+0

还是一样的例外吗? – Hulk 2015-03-19 08:02:31

+0

是的,它显示了同样的异常仍 – Balakrishnan 2015-03-19 08:14:49

相关问题