リストビューに実行中のアプリのアイコンを表示するアプリを作成しています。構文の問題はありません (と思います) が、アプリを起動してアクティビティを実行しようとすると、アプリが強制終了しthreadid=1: thread exiting with uncaught exception (group=0x41088930)
、さらにエラーが発生し、このエラーを修正するにはどうすればよいですか? Asynctask を使用して画像をリスト ビューにロードする必要がありますか? または解決策はありますか?
(Logcat)
02-18 14:17:46.700: W/dalvikvm(18370): threadid=1: thread exiting with uncaught exception (group=0x41088930)
02-18 14:17:46.700: E/AndroidRuntime(18370): FATAL EXCEPTION: main
02-18 14:17:46.700: E/AndroidRuntime(18370): java.lang.RuntimeException: Unable to start activity ComponentInfo{kr.hybdms.sidepanel/kr.hybdms.sidepanel.LeftSidePanel}: java.lang.NullPointerException
02-18 14:17:46.700: E/AndroidRuntime(18370): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2309)
02-18 14:17:46.700: E/AndroidRuntime(18370): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)
02-18 14:17:46.700: E/AndroidRuntime(18370): at android.app.ActivityThread.access$600(ActivityThread.java:153)
02-18 14:17:46.700: E/AndroidRuntime(18370): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
02-18 14:17:46.700: E/AndroidRuntime(18370): at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 14:17:46.700: E/AndroidRuntime(18370): at android.os.Looper.loop(Looper.java:137)
02-18 14:17:46.700: E/AndroidRuntime(18370): at android.app.ActivityThread.main(ActivityThread.java:5204)
02-18 14:17:46.700: E/AndroidRuntime(18370): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 14:17:46.700: E/AndroidRuntime(18370): at java.lang.reflect.Method.invoke(Method.java:511)
02-18 14:17:46.700: E/AndroidRuntime(18370): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
02-18 14:17:46.700: E/AndroidRuntime(18370): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
02-18 14:17:46.700: E/AndroidRuntime(18370): at dalvik.system.NativeStart.main(Native Method)
02-18 14:17:46.700: E/AndroidRuntime(18370): Caused by: java.lang.NullPointerException
02-18 14:17:46.700: E/AndroidRuntime(18370): at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
02-18 14:17:46.700: E/AndroidRuntime(18370): at android.widget.ListView.setAdapter(ListView.java:462)
02-18 14:17:46.700: E/AndroidRuntime(18370): at kr.hybdms.sidepanel.LeftSidePanel.onCreate(LeftSidePanel.java:59)
02-18 14:17:46.700: E/AndroidRuntime(18370): at android.app.Activity.performCreate(Activity.java:5108)
02-18 14:17:46.700: E/AndroidRuntime(18370): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-18 14:17:46.700: E/AndroidRuntime(18370): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2263)
02-18 14:17:46.700: E/AndroidRuntime(18370): ... 11 more
(LeftSidePanel.java)
package kr.hybdms.sidepanel;
import java.util.ArrayList;
import java.util.List;
import kr.hybdms.sidepanel.PanelArrayAdapter;
import kr.hybdms.sidepanel.R;
import kr.hybdms.sidepanel.util.SystemUiHider;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
/**
* An example full-screen activity that shows and hides the system UI (i.e.
* status bar and navigation/system bar) with user interaction.
*
* @see SystemUiHider
*/
public class LeftSidePanel extends Activity implements
OnItemClickListener {
ListView listView;
List<PanelItemDetail> rowItems;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_left_side_panel);
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(10);
ArrayList<PanelItemDetail> iconarray = new ArrayList<PanelItemDetail>();
PackageManager pacMgr = getPackageManager();
for (ActivityManager.RunningTaskInfo runningTask: tasks)
{
try {
iconarray.add(new PanelItemDetail(pacMgr.getApplicationIcon(runningTask.topActivity.getPackageName())));
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
listView = (ListView) findViewById(R.id.panelcontents);
PanelArrayAdapter adapter = new PanelArrayAdapter(this,
R.layout.panelrow, rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast toast = Toast.makeText(getApplicationContext(),
"Item " + (position + 1) + ": " + rowItems.get(position),
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
}
(PanelArrayAdapter.java)
package kr.hybdms.sidepanel;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
public class PanelArrayAdapter extends ArrayAdapter<PanelItemDetail> {
Context context;
public PanelArrayAdapter(Context context, int resourceId,
List<PanelItemDetail> items) {
super(context, resourceId, items);
this.context = context;
}
/*private view holder class*/
private class ViewHolder {
ImageView imageView;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
PanelItemDetail rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.panelrow, null);
holder = new ViewHolder();
holder.imageView = (ImageView) convertView.findViewById(R.id.appicon);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.imageView.setImageDrawable(rowItem.getImageId());
return convertView;
}
}
(PanelItemDetail.java)
package kr.hybdms.sidepanel;
import android.graphics.drawable.Drawable;
public class PanelItemDetail {
private Drawable imageId;
public PanelItemDetail(Drawable images) {
this.imageId = images;
}
public Drawable getImageId() {
return imageId;
}
public void setImageId(Drawable imageId) {
this.imageId = imageId;
}
}