Question

i'm making an app that shows running apps icon on listview. there are no syntax problem (i think) but when i launch app and try to run activity, the app force closes and i got threadid=1: thread exiting with uncaught exception (group=0x41088930)and more error how can i fix this error? should i use Asynctask to load image to list view? or any solutions?

(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;
    }
}
Was it helpful?

Solution

Error is here

PanelArrayAdapter adapter = new PanelArrayAdapter(this,
        R.layout.panelrow, rowItems);
listView.setAdapter(adapter);

Because rowItems is not yet initialized.

OTHER TIPS

it seems, that your source is not able to find the leftsidepanel

try these steps:

1.First of all, check if your source is where it is mentioned in the error log.

2.If it is, check if you've made any typing mistakes

3.If it still isn't working, it would be easier (In my opinion) to create a new project and rename what is missing (I've done this several times already and it has always worked) Of course, make sure you copy all of your code

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top