setcontentview中的android(4.2)异常

时间:2013-01-31 08:16:32

标签: android exception

我在更高版本的Android版本中获得了一个例外(> = 4.0)

这是我的 Favourites.java:

package com.SCHOOLinSITES.SiS.Connect;




import java.util.ArrayList;
import java.util.List;



import android.R.color;
import android.app.Activity;
import android.content.Intent;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;





public class Favorites extends Activity implements OnItemClickListener {
    /** Called when the activity is first created. */
    /**GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[] {Color.RED,Color.GREEN});
    View title = getWindow().findViewById(android.R.id.title);
    View titleBar = (View) title.getParent();
    titleBar.setBackgroundDrawable(gd);**/

    int b;
    ArrayList<Item> items = new ArrayList<Item>();
    String[]   aaaa,aaaa1;
    List<String> list1=new ArrayList<String>();
    List<String> dlist=new ArrayList<String>();
    List<String> slist=new ArrayList<String>();
    List<String> did=new ArrayList<String>();
    List<String> web=new ArrayList<String>();
    List<String> sid=new ArrayList<String>();
    List<String> sweb=new ArrayList<String>();
    List<String> clist=new ArrayList<String>();
    List<String> cid=new ArrayList<String>();
    List<String> cweb=new ArrayList<String>();
    ListView l;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.main1);
       // setTitle("My Favorites");
       // setTitleColor(Color.argb(1,88,0,4));
      // setTitleColor(Color.parseColor("#580004"));
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);
       TextView t=(TextView)findViewById(R.id.myTitle);
       t.setText("My Favorites");


       //       SQLiteDatabase db=null;
//       String[] ar=null;
//       DBHelper op=new DBHelper(this,"pc", null, 1);
//       db=op.getReadableDatabase();
//       Cursor cursor= db.rawQuery("SELECT name FROM SCHOOLinSITES", null);
//       while (cursor.moveToNext()) {
//         String name=cursor.getString(cursor.getColumnIndex("name"));
//         String[] split=name.split(",");
//       if(split.length==4)
//       {
//          dlist.add(split[0]+","+split[1]);
//          did.add(split[2]);
//          web.add(split[3]);
//          
//       }
//       else{
//          
//          slist.add(split[0]+","+split[1]+","+split[2]);
//          sid.add(split[3]);
//          sweb.add(split[4]);
//          
//          
//       }
//      
//   }
//       db.close();
//       cursor.close();
//       if(dlist.size()>0 || slist.size()>0){
//          ImageView img1=(ImageView)findViewById(R.id.imgst6);
//          ImageView img2=(ImageView)findViewById(R.id.imgst7);
//          TextView txt1=(TextView)findViewById(R.id.starttext);
//          TextView txt2=(TextView)findViewById(R.id.starttext1);
//          TextView txt3=(TextView)findViewById(R.id.starttext3);
//          TextView txt4=(TextView)findViewById(R.id.starttext4);
//          img1.setVisibility(View.GONE);
//          img2.setVisibility(View.GONE);
//          txt1.setVisibility(View.GONE);
//          txt2.setVisibility(View.GONE);
//          txt3.setVisibility(View.GONE);
//          txt4.setVisibility(View.GONE);
//       items.clear();
//       if(dlist.size()!=0)
//           items.add(new SectionItem("Districts"));
//       for(int i=0;i<dlist.size();i++)
//       {
//           items.add(new EntryItem(dlist.get(i), null));
//       }
//       if(slist.size()!=0)
//           items.add(new SectionItem("Schools"));
//       for(int i=0;i<slist.size();i++)
//       {
//           items.add(new EntryItem(slist.get(i), null));
//       }
//         l=(ListView)findViewById(R.id.list1);
//       l.setCacheColorHint(color.transparent);
//       l.requestFocus(0);
//       l.setScrollingCacheEnabled(false);
//       l.setBackgroundColor(color.transparent);
//       EntryAdapter adapter = new EntryAdapter(this, items);
//       l.setAdapter(adapter);
//       l.setOnItemClickListener(this);
//       for(int i=0;i<dlist.size()+1;i++)
//       {
//          if(i==0)
//          {
//              clist.add("");
//              cid.add("");
//              cweb.add("");
//          }
//          else{
//              clist.add(dlist.get(i-1));
//              cid.add(did.get(i-1));
//              cweb.add(web.get(i-1));
//              
//  }
//          
//       }
//       for(int i=0;i<slist.size()+1;i++)
//       {
//          if(i==0)
//          {
//              clist.add("");
//              cid.add("");
//              cweb.add("");
//          }
//          else{
//              clist.add(slist.get(i-1));
//              cid.add(sid.get(i-1));
//              cweb.add(sweb.get(i-1));
//              
//  }
//          
//       }
//       }
//       else{
//           items.clear();
//           l=(ListView)findViewById(R.id.list1);
//            l.setCacheColorHint(color.transparent);
//            l.requestFocus(0);
//            l.setScrollingCacheEnabled(false);
//            l.setBackgroundColor(color.transparent);
//            EntryAdapter adapter = new EntryAdapter(this, items);
//            l.setAdapter(adapter);
//            l.setOnItemClickListener(this);
//          ImageView img1=(ImageView)findViewById(R.id.imgst6);
//          ImageView img2=(ImageView)findViewById(R.id.imgst7);
//          TextView txt1=(TextView)findViewById(R.id.starttext);
//          TextView txt2=(TextView)findViewById(R.id.starttext1);
//          TextView txt3=(TextView)findViewById(R.id.starttext3);
//          TextView txt4=(TextView)findViewById(R.id.starttext4);
//          img1.setVisibility(View.VISIBLE);
//          img2.setVisibility(View.VISIBLE);
//          txt1.setVisibility(View.VISIBLE);
//          txt2.setVisibility(View.VISIBLE);
//          txt3.setVisibility(View.VISIBLE);
//          txt4.setVisibility(View.VISIBLE);
//       }

    }
    public void method1(View v)
    {
        Intent start=new Intent(this, Find.class);
        startActivity(start);
    }
    public void method2(View v)
    {


    }
    public void method3(View v)
    {

        Intent start=new Intent(this,Resources.class );
        startActivity(start);
    }
   public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
   {

        b=arg2;
        myClickHandler();
    }
      public void myClickHandler() {

            String[] split;
            split=clist.get(b).split(",");
            if(split.length==2){
            Intent gotoLIst=new Intent(this,District.class);
            gotoLIst.putExtra("name", clist.get(b));   
            gotoLIst.putExtra("did",cid.get(b));
            gotoLIst.putExtra("web", cweb.get(b));
            startActivity(gotoLIst);
            }
            else{
                 Intent gotoLIst=new Intent(this,schooldetails.class);
                gotoLIst.putExtra("name", clist.get(b));   
                gotoLIst.putExtra("did",cid.get(b));
                gotoLIst.putExtra("web", cweb.get(b));
                startActivity(gotoLIst);


            }


        }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        dlist.clear();
        slist.clear();
        sid.clear();
        did.clear();
        web.clear();
        sweb.clear();
        clist.clear();
        cid.clear();
        cweb.clear();
        list1.clear();
        SQLiteDatabase db=null;
        String[] ar=null;
        DBHelper op=new DBHelper(this,"pc", null, 1);
        db=op.getReadableDatabase();
        Cursor cursor= db.rawQuery("SELECT name FROM SCHOOLinSITES", null);
        while (cursor.moveToNext()) {
           String name=cursor.getString(cursor.getColumnIndex("name"));

           String[] split=name.split(",");
        if(split.length==4)
        {
            dlist.add(split[0]+","+split[1]);
            did.add(split[2]);
            web.add(split[3]);

        }
        else{

            slist.add(split[0]+","+split[1]+","+split[2]);
            sid.add(split[3]);
            sweb.add(split[4]);


        }

    }
        db.close();
        cursor.close();
        if(dlist.size()>0 || slist.size()>0){
            ImageView img1=(ImageView)findViewById(R.id.imgst6);
            ImageView img2=(ImageView)findViewById(R.id.imgst7);
            TextView txt1=(TextView)findViewById(R.id.starttext);
            //TextView txt2=(TextView)findViewById(R.id.starttext1);
            TextView txt3=(TextView)findViewById(R.id.starttext3);
            //TextView txt4=(TextView)findViewById(R.id.starttext4);
            img1.setVisibility(View.GONE);
            img2.setVisibility(View.GONE);
            txt1.setVisibility(View.GONE);
            //txt2.setVisibility(View.GONE);
            txt3.setVisibility(View.GONE);
            //txt4.setVisibility(View.GONE);

        items.clear();
        if(dlist.size()!=0)
             items.add(new SectionItem("Districts"));
        for(int i=0;i<dlist.size();i++)
        {
             items.add(new EntryItem(dlist.get(i), null));
        }
        if(slist.size()!=0)
             items.add(new SectionItem("Schools"));
        for(int i=0;i<slist.size();i++)
        {
             items.add(new EntryItem(slist.get(i), null));
        }
        l=(ListView)findViewById(R.id.list1);
        l.setCacheColorHint(color.transparent);
        l.requestFocus(0);
        l.setScrollingCacheEnabled(false);
        l.setBackgroundColor(color.transparent);
        EntryAdapter adapter = new EntryAdapter(this, items);
        l.setAdapter(adapter);
        l.setOnItemClickListener(this);

       if(dlist.size()>0){
        for(int i=0;i<dlist.size()+1;i++)
        {
            if(i==0)
            {
                clist.add("");
                cid.add("");
                cweb.add("");
            }
            else{
                clist.add(dlist.get(i-1));
                cid.add(did.get(i-1));
                cweb.add(web.get(i-1));

   }
        }
        }
       if(slist.size()>0){
        for(int i=0;i<slist.size()+1;i++)
        {
            if(i==0)
            {
                clist.add("");
                cid.add("");
                cweb.add("");
            }
            else{
                clist.add(slist.get(i-1));
                cid.add(sid.get(i-1));
                cweb.add(sweb.get(i-1));

   }

        }
       }
        }
else{
     items.clear();
     l=(ListView)findViewById(R.id.list1);
     l.setCacheColorHint(color.transparent);
     l.requestFocus(0);
     l.setScrollingCacheEnabled(false);
     l.setBackgroundColor(color.transparent);
     EntryAdapter adapter = new EntryAdapter(this, items);
     l.setAdapter(adapter);
     l.setOnItemClickListener(this);

            ImageView img1=(ImageView)findViewById(R.id.imgst6);
            ImageView img2=(ImageView)findViewById(R.id.imgst7);
            TextView txt1=(TextView)findViewById(R.id.starttext);
            //TextView txt2=(TextView)findViewById(R.id.starttext1);
            TextView txt3=(TextView)findViewById(R.id.starttext3);
            //TextView txt4=(TextView)findViewById(R.id.starttext4);
            img1.setVisibility(View.VISIBLE);
            img2.setVisibility(View.VISIBLE);
            txt1.setVisibility(View.VISIBLE);
            //txt2.setVisibility(View.VISIBLE);
            txt3.setVisibility(View.VISIBLE);
            //txt4.setVisibility(View.VISIBLE);
        }
    }
    public void Add(View v)
      {
            SQLiteDatabase db=null;
            DBHelper helper=new DBHelper(this,"pc",null,1);
            db=helper.getWritableDatabase();
            //db.delete("SCHOOLinSITES", null, null);
            Integer i=(Integer)v.getTag();
            String str= clist.get((Integer)v.getTag())+","+cid.get((Integer)v.getTag())+","+cweb.get((Integer)v.getTag());

            String whereClause ="name=?";

            db.delete("SCHOOLinSITES", whereClause, new String[]{str});
           db.close();
            onResume();
       }
}

当我尝试在Android 4.0及更高版本上执行它时,我在 setcontentview(r.layout.main1)处获得异常。

例外详细信息:

01-31 13:34:30.261: D/AndroidRuntime(689): Shutting down VM
01-31 13:34:30.261: W/dalvikvm(689): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
01-31 13:34:30.281: E/AndroidRuntime(689): FATAL EXCEPTION: main
01-31 13:34:30.281: E/AndroidRuntime(689): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.SCHOOLinSITES.SiS.Connect/com.SCHOOLinSITES.SiS.Connect.Favorites}: android.view.InflateException: Binary XML file line #5: Error inflating class <unknown>
01-31 13:34:30.281: E/AndroidRuntime(689):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
01-31 13:34:30.281: E/AndroidRuntime(689):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
01-31 13:34:30.281: E/AndroidRuntime(689):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
01-31 13:34:30.281: E/AndroidRuntime(689):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
01-31 13:34:30.281: E/AndroidRuntime(689):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-31 13:34:30.281: E/AndroidRuntime(689):  at android.os.Looper.loop(Looper.java:137)
01-31 13:34:30.281: E/AndroidRuntime(689):  at android.app.ActivityThread.main(ActivityThread.java:4424)
01-31 13:34:30.281: E/AndroidRuntime(689):  at java.lang.reflect.Method.invokeNative(Native Method)
01-31 13:34:30.281: E/AndroidRuntime(689):  at java.lang.reflect.Method.invoke(Method.java:511)
01-31 13:34:30.281: E/AndroidRuntime(689):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-31 13:34:30.281: E/AndroidRuntime(689):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-31 13:34:30.281: E/AndroidRuntime(689):  at dalvik.system.NativeStart.main(Native Method)
01-31 13:34:30.281: E/AndroidRuntime(689): Caused by: android.view.InflateException: Binary XML file line #5: Error inflating class <unknown>
01-31 13:34:30.281: E/AndroidRuntime(689):  at android.view.LayoutInflater.createView(LayoutInflater.java:606)
01-31 13:34:30.281: E/AndroidRuntime(689):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
01-31 13:34:30.281: E/AndroidRuntime(689):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
01-31 13:34:30.281: E/AndroidRuntime(689):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
01-31 13:34:30.281: E/AndroidRuntime(689):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
01-31 13:34:30.281: E/AndroidRuntime(689):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-31 13:34:30.281: E/AndroidRuntime(689):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-31 13:34:30.281: E/AndroidRuntime(689):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-31 13:34:30.281: E/AndroidRuntime(689):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
01-31 13:34:30.281: E/AndroidRuntime(689):  at android.app.Activity.setContentView(Activity.java:1835)
01-31 13:34:30.281: E/AndroidRuntime(689):  at com.SCHOOLinSITES.SiS.Connect.Favorites.onCreate(Favorites.java:57)
01-31 13:34:30.281: E/AndroidRuntime(689):  at android.app.Activity.performCreate(Activity.java:4465)
01-31 13:34:30.281: E/AndroidRuntime(689):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-31 13:34:30.281: E/AndroidRuntime(689):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
01-31 13:34:30.281: E/AndroidRuntime(689):  ... 11 more
01-31 13:34:30.281: E/AndroidRuntime(689): Caused by: java.lang.reflect.InvocationTargetException
01-31 13:34:30.281: E/AndroidRuntime(689):  at java.lang.reflect.Constructor.constructNative(Native Method)
01-31 13:34:30.281: E/AndroidRuntime(689):  at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
01-31 13:34:30.281: E/AndroidRuntime(689):  at android.view.LayoutInflater.createView(LayoutInflater.java:586)
01-31 13:34:30.281: E/AndroidRuntime(689):  ... 24 more
01-31 13:34:30.281: E/AndroidRuntime(689): Caused by: java.lang.OutOfMemoryError
01-31 13:34:30.281: E/AndroidRuntime(689):  at android.graphics.Bitmap.nativeCreate(Native Method)
01-31 13:34:30.281: E/AndroidRuntime(689):  at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
01-31 13:34:30.281: E/AndroidRuntime(689):  at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
01-31 13:34:30.281: E/AndroidRuntime(689):  at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
01-31 13:34:30.281: E/AndroidRuntime(689):  at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:524)
01-31 13:34:30.281: E/AndroidRuntime(689):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:499)
01-31 13:34:30.281: E/AndroidRuntime(689):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
01-31 13:34:30.281: E/AndroidRuntime(689):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773)
01-31 13:34:30.281: E/AndroidRuntime(689):  at android.content.res.Resources.loadDrawable(Resources.java:1935)
01-31 13:34:30.281: E/AndroidRuntime(689):  at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
01-31 13:34:30.281: E/AndroidRuntime(689):  at android.view.View.<init>(View.java:2785)
01-31 13:34:30.281: E/AndroidRuntime(689):  at android.view.View.<init>(View.java:2722)
01-31 13:34:30.281: E/AndroidRuntime(689):  at android.view.ViewGroup.<init>(ViewGroup.java:379)
01-31 13:34:30.281: E/AndroidRuntime(689):  at android.widget.RelativeLayout.<init>(RelativeLayout.java:174)
01-31 13:34:30.281: E/AndroidRuntime(689):  ... 27 more
01-31 13:34:30.591: I/dalvikvm(689): threadid=3: reacting to signal 3
01-31 13:34:30.602: I/dalvikvm(689): Wrote stack traces to '/data/anr/traces.txt'
01-31 13:34:30.871: I/dalvikvm(689): threadid=3: reacting to signal 3
01-31 13:34:30.881: I/dalvikvm(689): Wrote stack traces to '/data/anr/traces.txt'

我该如何解决这个问题。

layoutfile:

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:layout_width="match_parent" android:id="@+id/relativeLayout1" >


 <RelativeLayout
        android:id="@+id/rel_bottom"
        android:layout_width="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_height="fill_parent"
        android:background="@drawable/mainbg" >

      <ListView  android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/list1" android:divider="@null"
android:dividerHeight="0dp" android:layout_alignParentLeft="true" android:paddingTop="15dp" android:paddingLeft="10dp" android:listSelector="@android:color/transparent"> </ListView>


        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:id="@+id/imgst6"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="30dp" android:background="@drawable/discticts" android:visibility="invisible"/>
       <TextView android:text="" android:onClick="method1" android:background="@drawable/search"
            android:clickable="true"  android:layout_width="wrap_content" android:id="@+id/starttext" android:visibility="invisible"
            android:layout_height="wrap_content"  android:layout_marginLeft="30dp" android:layout_below="@+id/imgst6" android:layout_marginTop="8dip" ></TextView>
<!--        <TextView android:text="Districts to add them to your Favorites" android:visibility="invisible" android:id="@+id/starttext1" android:layout_toRightOf="@+id/starttext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/imgst6" android:layout_marginTop="10dp" android:layout_marginLeft="5dp" android:textColor="#4D4D4D" android:textSize="12sp"></TextView>    -->
      <ImageView android:id="@+id/imgst7"  android:layout_width="wrap_content" android:visibility="invisible"
            android:layout_height="wrap_content"  android:layout_marginLeft="20dp" android:background="@drawable/schools" android:layout_below="@+id/starttext" android:layout_marginTop="10dp"></ImageView> 
      <TextView android:text="" android:onClick="method1" android:background="@drawable/search"
            android:clickable="true" android:visibility="invisible" android:layout_width="wrap_content" android:id="@+id/starttext3"
            android:layout_height="wrap_content"  android:layout_marginLeft="30dp" android:layout_below="@+id/imgst7" android:layout_marginTop="8dp" ></TextView>
<!--          <TextView android:text="Schools to add them to your Favorites" android:visibility="invisible" android:id="@+id/starttext4" android:layout_toRightOf="@+id/starttext3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/imgst7" android:layout_marginTop="10dp" android:layout_marginLeft="5dp" android:textColor="#4D4D4D" android:textSize="12sp"></TextView>    -->
</RelativeLayout>

 <RelativeLayout
        android:id="@+id/rel_bottom"
        android:layout_width="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_height="38dp"
        android:background="@drawable/bottom_bar" 
         android:paddingTop="5dp"
        >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:background="@drawable/findbtn"
            android:id="@+id/img1"
            android:onClick="method1"
            android:clickable="true"
            android:layout_marginLeft="15dp"
            />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/resourcesbtn"
            android:layout_alignParentRight="true" android:onClick="method3"
             android:clickable="true"
            android:id="@+id/img2"
             android:layout_marginRight="15dp"
            />
           <ImageView
            android:id="@+id/img3"
            android:layout_width="40dp"
            android:layout_height="fill_parent"
            android:background="@drawable/icon"
            android:layout_centerInParent="true"
            android:visibility="invisible"
             />

    </RelativeLayout>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

引发java.lang.OutOfMemoryError似乎你的布局中的图像太大而且无法加载到设备(或模拟器)内存中。

尝试加载缩小版图片(see android documentation)或发布您的版面以获取更多信息。