2012-11-07 27 views
0

我正在实现带有图像和框架布局(Button和LinearLayout)的列表视图,但是当我多次滚动列表时,应用程序自动崩溃...以下是我的代码... Main.java应用程序自动崩溃

public class Main extends ListActivity { 
/** Called when the activity is first created. */ 
ImageView img, img1; 
private AdapterClass adapter; 
private String reviewImageLink; 

private static final String TAG = "PRANJAL"; 
private boolean isImage = false; 

String fileName[] = new String[10]; 
private String imgurl; 

ArrayList<HashMap<String, String>> searchResult; 
private EditText edtSearch; 
private Button btnNext; 
private ListView list; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.listplaceholder); 
    edtSearch = (EditText) findViewById(R.id.edtSearch); 
    btnNext = (Button) findViewById(R.id.btnNext); 
    list = (ListView) findViewById(android.R.id.list); 
    // list = (LinearLayout) findViewById(R.id.list); 
    final ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>(); 

    /* 
    * JSONObject json = JSONfunctions 
    * .getJSONfromURL("http://23.21.228.8/PlutoApp_Beta/consumer_dash/" + 
    * "checkIn_confirmation_related_record.php?lat=18.535787&lng=73.891889" 
    *); 
    */ 
    JSONObject json = JSONfunctions 
      .getJSONfromURL("http://23.21.228.8/PlutoApp_Beta/consumer_dash/checkIn_confirmation_name.php?lat=40.739974&lng=-73.994293&name=shop"); 

    try { 

     JSONArray earthquakes = json.getJSONArray("Display"); 
     for (int i = 0; i < earthquakes.length(); i++) { 
      HashMap<String, String> map = new HashMap<String, String>(); 

      JSONObject e = earthquakes.getJSONObject(i); 

      map.put("Name", e.getString("name")); 
      map.put("Vicinity", e.getString("vicinity")); 

      map.put("logo", e.getString("logo")); 

      imgurl = e.getString("logo"); 

      String filename = "a" + i; 
      imgurl = e.getString("logo"); 

      mylist.add(map); 
      adapter = new AdapterClass(Main.this, 
        R.layout.my_custom_layout, mylist); 
     } 
    } catch (JSONException e) { 
     Log.e("log_tag", "Error parsing data " + e.toString()); 
    } 

    searchResult = new ArrayList<HashMap<String, String>>(mylist); 

    // adapter = new AdapterClass(Main.this, R.layout.my_custom_layout, 
    // mylist); 

    list.setTextFilterEnabled(true); 

    setListAdapter(adapter); 

    edtSearch.setOnKeyListener(new OnKeyListener() { 

     @Override 
     public boolean onKey(View v, int keyCode, KeyEvent event) { 
      // TODO Auto-generated method stub 
      if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) { 
       edtSearch.clearFocus(); 
       edtSearch.requestFocus(edtSearch.FOCUS_DOWN); 

       String searchText = edtSearch.getText().toString(); 
       final ArrayList<HashMap<String, String>> mySearchlist = new ArrayList<HashMap<String, String>>(); 

       JSONObject json = JSONfunctions 
         .getJSONfromURL("http://23.21.228.8/PlutoApp_Beta/consumer_dash/checkIn_confirmation_name.php?lat=40.739974&lng=-73.994293&name=" 
           + searchText); 
       try { 

        JSONArray Search = json.getJSONArray("Display"); 
        for (int i = 0; i < Search.length(); i++) { 
         HashMap<String, String> mapSearch = new HashMap<String, String>(); 

         JSONObject e = Search.getJSONObject(i); 
         String filename = "b" + i; 
         mapSearch.put("Name", e.getString("name")); 
         mapSearch.put("Vicinity", e.getString("vicinity")); 

         mapSearch.put("logo", e.getString("logo")); 

         imgurl = e.getString("logo"); 

         imgurl = e.getString("logo"); 

         mySearchlist.add(mapSearch); 
        } 
       } catch (JSONException e) { 
        Log.e("log_tag", "Error parsing data " + e.toString()); 
       } 

       AdapterClass adapter = new AdapterClass(Main.this, 
         R.layout.my_custom_layout, mySearchlist); 
       setListAdapter(adapter); 

       list.setTextFilterEnabled(true); // list.setAdapter(adapter); 

      } 

      return false; 
     } 
    }); 

    btnNext.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 


      searchResult = new ArrayList<HashMap<String, String>>(mylist); 

      // adapter = new AdapterClass(Main.this, R.layout.main, mylist); 
      adapter = new AdapterClass(Main.this, 
        R.layout.my_custom_layout, mylist); 

      list.setTextFilterEnabled(true); 

      setListAdapter(adapter); 
     } 
    }); 

} 

} 

AdapterClass.java

public class AdapterClass extends BaseAdapter { 

int images[]; 
Context context; 
String string[]; 
private LayoutInflater inflater; 
private int resourceId; 
private ArrayList<HashMap<String, String>> myList; 
private View view; 
private Animation anim_left_right; 
private Animation anim_right_left; 
private FrameLayout frame; 
private LinearLayout ll; 
private int width; 
private int height; 
private String tag; 
private boolean flag; 

private boolean firstRun = true; 
public int closedPosition; 
private boolean flagset = true; 
private boolean firstRunTouch; 
private boolean flag3 = false; 
public boolean ff = true; 
private View ll1; 
private View ll2; 
private boolean flag4 = false; 
private int j = 1; 
private boolean flag5 = false; 
private boolean flag6 = false; 
private boolean flag2; 
private boolean flag0; 
private boolean flag7= true; 
private boolean flag8 = true; 
private Animation MyAnimation; 
public ImageLoader imageLoader; 

public AdapterClass(Context context, int resourceId, 
     ArrayList<HashMap<String, String>> myList) { 

    this.context = context; 
    this.resourceId = resourceId; 
    this.myList = myList; 

    inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    imageLoader = new ImageLoader(context.getApplicationContext()); 
} 

@Override 
public int getCount() { 

    return myList.size(); 
} 

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 

    System.out.println("Getview Method is called"); 

    HashMap<String, String> map = getItem(position); 



    /*if(flag==true){ 
     Toast.makeText(context, "Inside Flag= true"+position, 1000).show(); 
     ll2.startAnimation(anim_right_left); 
     ll2.setLayoutParams(new LayoutParams(130, height)); 
     flag=false; 
    }*/ 


    //if (convertView == null) { 

     convertView = inflater.inflate(R.layout.my_custom_layout, parent, 
       false); 

// } 

    ImageView imageView = (ImageView) convertView 
      .findViewById(R.id.imageView1); 

    System.out.println("hi"); 

    ll = (LinearLayout) convertView.findViewById(R.id.ll1); 
    width = ll.getLayoutParams().width; 
    height = ll.getLayoutParams().height; 

    frame = (FrameLayout) convertView.findViewById(R.id.frame); 

    ll.setOnClickListener(new MyClickListener()); 

    ll.setOnTouchListener(new MyTouchListener()); 

    TextView from_user = (TextView) convertView 
      .findViewById(R.id.from_user); 

    TextView from_user_id = (TextView) convertView 
      .findViewById(R.id.from_user_id); 

    Button landed = (Button) convertView.findViewById(R.id.button1); 
    System.out.println("Tag in side getView :" + ll.getTag()); 

    imageView.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 

      Toast.makeText(context, "Inside Method startAnimation", 1000).show(); 
      anim_left_right = AnimationUtils.loadAnimation(context, 
        R.anim.anim_left_to_right1); 
      anim_right_left = AnimationUtils.loadAnimation(context, 
        R.anim.anim_left_to_right2); 

      ll.startAnimation(anim_right_left); 
      ll.setLayoutParams(new LayoutParams(130, height)); 
      /*AnimationUtils AnimAnimationUtils = null; 
      // TODO Auto-generated method stub 
      MyAnimation=AnimAnimationUtils.loadAnimation(ll.getContext(),R.anim.anim_left_to_right1); 
      //startAnimation(ll); 
      ll.startAnimation(MyAnimation);*/ 
      return true; 
     } 


    }); 

    landed.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      Toast.makeText(context, 
        "Button Clicked at position " + position, 
        Toast.LENGTH_SHORT).show(); 

     } 
    }); 

    from_user.setText(map.get("Name")); 
    from_user_id.setText(map.get("Vicinity")); 
    imageLoader.DisplayImage(map.get("logo"), imageView); 

    System.out.println("pos" + position + " closedPositon : " 
      + closedPosition + " tag : " + ll.getTag()); 

    String imageUrl = map.get("logo"); 

    System.out.println("ClosedPosition in getView Method : " 
      + closedPosition); 
    //if(flag==true){ 
    // convertView=null; 
    //} 
    return convertView; 
} 

protected void startAnimation(LinearLayout ll3) { 
    // TODO Auto-generated method stub 
    Toast.makeText(context, "Inside Method startAnimation", 1000).show(); 
    anim_left_right = AnimationUtils.loadAnimation(context, 
      R.anim.anim_left_to_right1); 
    anim_right_left = AnimationUtils.loadAnimation(context, 
      R.anim.anim_left_to_right2); 

    ll3.startAnimation(anim_right_left); 
    ll3.setLayoutParams(new LayoutParams(130, height)); 

} 

@Override 
public HashMap<String, String> getItem(int arg0) { 
    // TODO Auto-generated method stub 
    return myList.get(arg0); 
} 

@Override 
public long getItemId(int arg0) { 
    // TODO Auto-generated method stub 
    return arg0; 
} 

class MyTouchListener implements OnTouchListener { 
    float xstart, xend; 

    @Override 
    public boolean onTouch(final View v, MotionEvent event) { 
     anim_left_right = AnimationUtils.loadAnimation(context, 
       R.anim.anim_left_to_right1); 
     anim_right_left = AnimationUtils.loadAnimation(context, 
       R.anim.anim_left_to_right2); 
     System.out.println("OnTouch Called.."); 
     flag = true; 
     switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
      xstart = event.getX(); 
      firstRun = false; 
      flag6 = true; 
      break; 
     case MotionEvent.ACTION_UP: 
      xend = event.getX(); 
      if (xend < xstart) { 
       flag6 = true; 
      } 

      if (xstart < xend) { 
       System.out.println("inside xstart<xend..."); 
       flag6 = false; 
       flag8=true; 
       System.out.println("flag6 : false"); 
       // if (flag5 == true) { 
       // if(flag2==true){ 
       if (flag4 == true && flag5 == true) { // first time left to 

        flag0 = false;// right is 

        break; 
       } 
       if(flag7==false){ 
        flag8 = false; 
        flag7=true; 
       } 
       else{ 
        flag8=true; 
        //flag6 = true; 

       } 
       if (flag4 == true && flag0 == true && flag8==true) { // first time left to 
        System.out.println("IF after break is executed.."); // right 
                     // is 
        // disabled.... 
        System.out.println("first time left to right"); 
        System.out.println(" xend :" + xend + ", xstart :" 
          + xstart); 
        v.setBackgroundResource(R.drawable.a); 
        v.setLayoutParams(new LayoutParams(width, height)); 
        v.startAnimation(anim_left_right); 
        // ll1 = null; 
        flag4 = false; 
        // flag6=false; 
        ff = true; 
       } 
       // } 
       // } 

       //ff = true; 
      } 

      break; 

     } 
     return false; 
    } 
} 

class MyClickListener implements OnClickListener { 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     Animation anim_left_right = AnimationUtils.loadAnimation(context, 
       R.anim.anim_left_to_right1); 
     Animation anim_right_left = AnimationUtils.loadAnimation(context, 
       R.anim.anim_left_to_right2); 
     System.out.println("OnClick Called.."); 
     // if (flag4 == true || j==1) { 
     if (flag6 == true) { 
      System.out.println("InSide OnClick.."); 
      if (ff) { 
       System.out.println("InSide OnClick.."); 
       // closedPosition = (Integer) v.getTag(); 
       System.out 
         .println("MyTouchListener is Called before getview.."); 
       System.out.println("closdeposiotion : " + closedPosition); 


       v.startAnimation(anim_right_left); 
       v.setLayoutParams(new LayoutParams(130, height)); 
       v.setBackgroundColor(Color.BLACK); 
       ll1 = v; 
       ll2 = v; 
       System.out.println("Tag of ll1 in if statement : " 
         + ll1.getTag() + "Tag of ll1 in if statement : " 
         + v.getTag()); 
       ff = false; 
       flag4 = true; 
       flag6 = false; 
       flag5 = false; 
       flag0 = true; 
       flag = true; 
      } else { 
       flag4 = true; 
       flag5 = true; 
       System.out.println("InSide OnClick.."); 



       v.startAnimation(anim_right_left); 
       v.setLayoutParams(new LayoutParams(130, height)); 
       v.setBackgroundColor(Color.BLACK); 
       System.out.println("Tag of ll1 in else statement : " 
         + v.getTag()); 
       //ll1 = v; 
       // flag4 = true; 
       flag6 = false; 
       // flag5 = true; 
       if (ll1 != null) { 

        ll1.setBackgroundResource(R.drawable.a); 
        ll1.setLayoutParams(new LayoutParams(width, height)); 
        ll1.startAnimation(anim_left_right); 
        System.out.println("Tag of ll1 in else statement : " 
          + ll1.getTag()); 
        // ll1 = null; 
        // ff = true; 
        flag5 = false; 
        // flag4 = false; 
        flag7 = false; 
        flag6 = false; 
        flag=true; 
       } 
       ll1 = v; 
       ll2 = v; 
      } 
     } 
    } 

    // } 

} 

} 

Listpalceholder.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <EditText 
     android:id="@+id/edtSearch" 
     android:layout_width="230dp" 
     android:layout_height="wrap_content" 
     android:hint="Search" 
     android:singleLine="true" /> 

    <Button 
     android:id="@+id/btnNext" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Clear" /> 
</LinearLayout> 

<ListView 
    android:id="@id/android:list" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:drawSelectorOnTop="false" /> 

<TextView 
    android:id="@id/android:empty" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Sorry..No Match Found.." 
    android:textSize="20dp"/> 

</LinearLayout> 

My_Custom_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" 
android:padding="2dp" > 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="70dp" 
    android:layout_height="70dp" 
    android:src="@drawable/icon" /> 

<FrameLayout 
    android:id="@+id/frame" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:text="Landed" 
     /> 

    <LinearLayout 
     android:id="@+id/ll1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="@drawable/a" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/from_user" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="#000000" /> 

     <TextView 
      android:id="@+id/from_user_id" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="#000000" /> 
    </LinearLayout> 
</FrameLayout> 

</LinearLayout> 

请帮我....

+0

你能发布logcat错误吗? –

回答

0

问题,是最有可能的OutOfMemoryError,这个问题已经在所以在这里回答了很多次,请搜索内存不足的问题,或图像内存不足问题,或虚拟现实的预算超过,你会得到一套很好的指导方针。 看到这个问题,了解更多关于这个问题。

Strange out of memory issue while loading an image to a Bitmap object

而且,疏通if块,重用convertView,它会使已经被绘制的意见正确重用。

if (convertView == null) { 

     convertView = inflater.inflate(R.layout.my_custom_layout, parent, 
       false); 

}