안녕하세요 어플리케이션들을 리스트에 뿌려주면서 어플을 클릭하는 카운트를 세어 정렬해주는 프로그램을 만들고 있습니다.

제 핸드폰 갤럭시 노트2에서는 잘 돌아가는데...개발용 보드에 집어 넣거나 애뮬레이터로 실행을 하면 프로그램이 중지됩니다.

소스 코드와 log를 첨부하겠습니다....

 

////////소스코드입니다.////

package kw.ac.listlauncher;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class ListLauncherActivity extends Activity {
 /** Called when the activity is first created. */

 List<ResolveInfo> pkgAppsList; // app list
 ArrayList<AppItem> countAppsList;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
  mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

  // Retrieve all activities that can be performed for the given intent.
  pkgAppsList = getPackageManager().queryIntentActivities(mainIntent, 0);
  
  countAppsList = new ArrayList<AppItem>();

  makeCountAppsList();
  
  Collections.sort(countAppsList);  

  
  MyAdapter adapter = new MyAdapter(this, R.layout.listitem, countAppsList);
  ListView lv = (ListView) findViewById(R.id.ListLauncher);
  lv.setAdapter(adapter);

  lv.setOnItemClickListener(new OnItemClickListener() {

   public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
     long arg3) {
    // TODO Auto-generated method stub
    AppItem temp = countAppsList.get(arg2);
    temp.count++;
    updateCountDB();
    Intent i = new Intent();
    i.setClassName( temp.packageName, temp.className );
    startActivity(i);
   }
  });
 }

 
 
 public class MyAdapter extends ArrayAdapter<AppItem> {

  List<AppItem> child;

  public MyAdapter(Context context, int textViewResourceId, List<AppItem> objects) {
   super(context, textViewResourceId, objects);
   // TODO Auto-generated constructor stub
   child = objects;
  }

  @Override
  public AppItem getItem(int position) {
   // TODO Auto-generated method stub
   return child.get(position);
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
   // TODO Auto-generated method stub

   if (convertView == null) {
    LayoutInflater li = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
    convertView = li.inflate(R.layout.listitem, null);
   }

   ImageView icon = (ImageView) convertView.findViewById(R.id.ivIcon);
   TextView name = (TextView) convertView.findViewById(R.id.tvName);

   AppItem temp = child.get(position);
   icon.setImageDrawable(temp.appImage);

   name.setText(temp.appName);

   return convertView;
  }
 }

 public class AppItem implements Comparable<AppItem>{

  public Drawable appImage;
  public String appName;
  public String packageName;
  public String className;
  public int count;

  public AppItem(Drawable _appImage, String _appName, String _packageName, String _className, int _count) {
   appImage = _appImage;
   appName = _appName;
   packageName = _packageName;
   className = _className;
   count = _count;
  }

  public int compareTo(AppItem another) {
   // TODO Auto-generated method stub
   return Integer.toString(another.count).compareTo(Integer.toString(this.count));
  }

 }

 private void makeCountAppsList() {

  // 먼저 AppList를 갖어 옵니다.
  // App의 아이콘과 앱의 이름을 갖고 옵니다
  for (ResolveInfo ri : pkgAppsList) {
   Drawable icon = ri.loadIcon(getPackageManager());
   ApplicationInfo ai;
   try {
    ai = getPackageManager().getApplicationInfo( ri.activityInfo.packageName, 0);
   } catch (NameNotFoundException e) {
    // TODO Auto-generated catch block
    ai = null;
    e.printStackTrace();
   }
   String appName = (String) getPackageManager().getApplicationLabel(ai);
   String packageName = ri.activityInfo.packageName;
   String className = ri.activityInfo.name;
   countAppsList.add(new AppItem(icon, appName, packageName, className, 0));
  }

  String fileName = Environment.getExternalStorageDirectory().getAbsolutePath()+"/applistdb.txt";
  File file = new File(fileName);

  // DB가 있는지 확인합니다 // 없다면 DB를 생성합니다
  if (file.exists() == false) {
   try {
    FileWriter fw = new FileWriter(file);
    BufferedWriter bw = new BufferedWriter(fw);
    PrintWriter pw = new PrintWriter(bw);

    for (AppItem ai : countAppsList) {
     pw.println(ai.appName + ": 0");
     pw.flush();
    }

    pw.close();
    bw.close();
    fw.close();

   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  
  // DB가 존재한다면 내용을 읽어와 해당 객체를 업데이트 합니다.
  else {
   FileReader fr;
   try {
    fr = new FileReader(file);
    BufferedReader br = new BufferedReader(fr);

    String str = "";
    while ((str = br.readLine()) != null) {
     String[] appData = str.split(":");
     for (AppItem ai : countAppsList) {
      if ( ai.appName.equals(appData[0]) ) {
       ai.count = Integer.parseInt(appData[1]);
       break;
      }
     }
    }
    
    br.close();
    fr.close();

   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }

  }
 }
 
 private void updateCountDB() {
  // TODO Auto-generated method stub
  
  String fileName = Environment.getExternalStorageDirectory().getAbsolutePath()+"/applistdb.txt";
  File file = new File(fileName);

  try {
   FileWriter fw = new FileWriter(file);
   BufferedWriter bw = new BufferedWriter(fw);
   PrintWriter pw = new PrintWriter(bw);

   for (AppItem ai : countAppsList) {
    pw.println(ai.appName + ":" + ai.count );
    pw.flush();
   }

   pw.close();
   bw.close();
   fw.close();

  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
    
 }
 

}

 

///////log cat 내용 ///////// 에러 메세지 입니다.

 

12-03 10:51:35.950: E/AndroidRuntime(3158): FATAL EXCEPTION: main
12-03 10:51:35.950: E/AndroidRuntime(3158): java.lang.RuntimeException: Unable to start activity ComponentInfo{kw.ac.listlauncher/kw.ac.listlauncher.ListLauncherActivity}: java.lang.NumberFormatException: Invalid int: " 0"
12-03 10:51:35.950: E/AndroidRuntime(3158): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
12-03 10:51:35.950: E/AndroidRuntime(3158): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
12-03 10:51:35.950: E/AndroidRuntime(3158): at android.app.ActivityThread.access$600(ActivityThread.java:123)
12-03 10:51:35.950: E/AndroidRuntime(3158): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
12-03 10:51:35.950: E/AndroidRuntime(3158): at android.os.Handler.dispatchMessage(Handler.java:99)
12-03 10:51:35.950: E/AndroidRuntime(3158): at android.os.Looper.loop(Looper.java:137)
12-03 10:51:35.950: E/AndroidRuntime(3158): at android.app.ActivityThread.main(ActivityThread.java:4424)
12-03 10:51:35.950: E/AndroidRuntime(3158): at java.lang.reflect.Method.invokeNative(Native Method)
12-03 10:51:35.950: E/AndroidRuntime(3158): at java.lang.reflect.Method.invoke(Method.java:511)
12-03 10:51:35.950: E/AndroidRuntime(3158): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-03 10:51:35.950: E/AndroidRuntime(3158): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-03 10:51:35.950: E/AndroidRuntime(3158): at dalvik.system.NativeStart.main(Native Method)
12-03 10:51:35.950: E/AndroidRuntime(3158): Caused by: java.lang.NumberFormatException: Invalid int: " 0"
12-03 10:51:35.950: E/AndroidRuntime(3158): at java.lang.Integer.invalidInt(Integer.java:138)
12-03 10:51:35.950: E/AndroidRuntime(3158): at java.lang.Integer.parse(Integer.java:375)
12-03 10:51:35.950: E/AndroidRuntime(3158): at java.lang.Integer.parseInt(Integer.java:366)
12-03 10:51:35.950: E/AndroidRuntime(3158): at java.lang.Integer.parseInt(Integer.java:332)
12-03 10:51:35.950: E/AndroidRuntime(3158): at kw.ac.listlauncher.ListLauncherActivity.makeCountAppsList(ListLauncherActivity.java:197)
12-03 10:51:35.950: E/AndroidRuntime(3158): at kw.ac.listlauncher.ListLauncherActivity.onCreate(ListLauncherActivity.java:54)
12-03 10:51:35.950: E/AndroidRuntime(3158): at android.app.Activity.performCreate(Activity.java:4465)
12-03 10:51:35.950: E/AndroidRuntime(3158): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-03 10:51:35.950: E/AndroidRuntime(3158): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
12-03 10:51:35.950: E/AndroidRuntime(3158): ... 11 more

 

스마트폰에서는 돌아가는 것이 왜...보드와 이클립스 애뮬에서는 돌아가지 않을까요...