2013-04-28 255 views
2

除了當用戶停止使用足夠長的屏幕鎖定時,我的應用程序運行良好,當他們解鎖它時,應用程序崩潰,我有點不知道爲什麼。以下是錯誤:屏幕解鎖時崩潰

enter image description here

這裏是懶惰適配器:

package com.buhz.helpers; 

import java.util.ArrayList;  
import java.util.HashMap; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 


import com.buhz.login.R; 
import com.buhz.login.TabHostFragmentActivity; 
import com.loopj.android.image.SmartImageView; 



public class LazyAdapter extends BaseAdapter { 

    private Activity activity; 
    private ArrayList<HashMap<String, String>> data; 
    private static LayoutInflater inflater=null; 
    public ImageLoader imageLoader; 

    public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) { 
     activity = a; 
     data=d; 
     inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     imageLoader=new ImageLoader(activity.getApplicationContext()); 
    } 

    public int getCount() { 
     return data.size(); 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     View vi=convertView; 
     if(convertView==null) 
      vi = inflater.inflate(R.layout.list_row, null); 

     TextView name = (TextView)vi.findViewById(R.id.name); // title 
     TextView message = (TextView)vi.findViewById(R.id.message); // artist name 
     TextView created = (TextView)vi.findViewById(R.id.created); // duration 
     SmartImageView thumb_image = (SmartImageView) vi.findViewById(R.id.list_image); 



     HashMap<String, String> update = new HashMap<String, String>(); 
     update = data.get(position); 

     // Setting all values in listview 
     name.setText(update.get("name")); 
     message.setText(update.get("message")); 
     created.setText(update.get("created")); 
     thumb_image.setImageUrl(update.get("thumb_img")); 
     name.setOnClickListener(new myOnClickListener(position)); 
     thumb_image.setOnClickListener(new myOnClickListener(position)); 
     return vi; 
    } 

    public class myOnClickListener implements OnClickListener{ 
     private int position; 
     private String clicked_uid; 
     public myOnClickListener(int position){ 
      this.position=position; 
     } 
     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 


      HashMap<String, String> update = new HashMap<String, String>(); 
      update = data.get(position); 
      Log.i("Update Position:", update.toString()); 
      clicked_uid=update.get("uid"); 
      Log.d("Clicked UID:", clicked_uid+""); 
      Intent i = new Intent(activity.getApplicationContext() , TabHostFragmentActivity.class); 
      i.putExtra("profile_uid", clicked_uid); 
      activity.startActivity(i); 
      activity.finish(); 

     } 

    } 
} 
+2

從您粘貼的logcat中,它看起來像運行Activity onCreate之前引發的異常。所以你的LazyAdapter獲得了一個空值。確保您的生命週期事件按順序觸發,並且您沒有嘗試在正確創建活動之前實例化適配器。 – 2013-04-28 22:57:43

+0

你是什麼意思,當你說「確保你的生命週期事件順序觸發」...截至到現在我使用FragmentActivity和一個片段,我有代碼的唯一生命週期方法是onCreate截至目前。如果我沒有實現它們,爲我處理生命週期方法? – ChuckKelly 2013-04-28 23:27:14

+1

我沒有清楚地說明它 - 基本上你必須確保你在生命週期的正確時間實例化LazyAdapter。 – 2013-04-29 00:22:35

回答

0

你必須確保你正在處理所有可能的狀態查看申請狀態。當您的應用程序狀態進入未處理狀態(如onResume或onStart)時,會發生此問題。

本文將幫助你瞭解每個狀態,當它被調用

http://www.android-app-market.com/android-activity-lifecycle.html

+0

當涉及到碎片時,這非常有用,這是問題所在。 ive發現它的bc片段出於某種原因加載速度比活動onResume快,所以我的活動上下文尚未準備好,我只是不知道該怎麼辦 – ChuckKelly 2013-04-29 02:13:49

2

由於the doc說:

When a configuration change occurs at runtime, the activity is shut down 
and restarted by default, but declaring a configuration with this attribute 
will prevent the activity from being restarted. 

所以我改變AndroidManifest.xml中包括所有配置。

android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|screenLayout|fontScale|uiMode|orientation|screenSize|smallestScreenSize|layoutDirection" 

它適用於我。