无法实例化活动ComponentInfo java.lang.NullPointerException

时间:2012-07-23 09:04:15

标签: java android android-listview android-arrayadapter

  

07-23 05:01:10.925:E / AndroidRuntime(4520):致命异常:主要   07-23 05:01:10.925:E / AndroidRuntime(4520):   java.lang.RuntimeException:无法实例化活动   ComponentInfo {} nsixty.crew.app/nsixty.crew.app.gallery:   java.lang.NullPointerException 07-23 05:01:10.925:   E / AndroidRuntime(4520):at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)   07-23 05:01:10.925:E / AndroidRuntime(4520):at   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1680)   07-23 05:01:10.925:E / AndroidRuntime(4520):at   android.app.ActivityThread.access $ 1500(ActivityThread.java:117)07-23   05:01:10.925:E / AndroidRuntime(4520):at   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)   07-23 05:01:10.925:E / AndroidRuntime(4520):at   android.os.Handler.dispatchMessage(Handler.java:99)07-23   05:01:10.925:E / AndroidRuntime(4520):at   android.os.Looper.loop(Looper.java:130)07-23 05:01:10.925:   E / AndroidRuntime(4520):at   android.app.ActivityThread.main(ActivityThread.java:3703)07-23   05:01:10.925:E / AndroidRuntime(4520):at   java.lang.reflect.Method.invokeNative(Native Method)07-23   05:01:10.925:E / AndroidRuntime(4520):at   java.lang.reflect.Method.invoke(Method.java:507)07-23 05:01:10.925:   E / AndroidRuntime(4520):at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:841)   07-23 05:01:10.925:E / AndroidRuntime(4520):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)07-23   05:01:10.925:E / AndroidRuntime(4520):at   dalvik.system.NativeStart.main(Native Method)07-23 05:01:10.925:   E / AndroidRuntime(4520):引起:java.lang.NullPointerException   07-23 05:01:10.925:E / AndroidRuntime(4520):at   nsixty.crew.app.gallery。(gallery.java:49)07-23 05:01:10.925:   E / AndroidRuntime(4520):at java.lang.Class.newInstanceImpl(Native   方法)07-23 05:01:10.925:E / AndroidRuntime(4520):at   java.lang.Class.newInstance(Class.java:1409)07-23 05:01:10.925:   E / AndroidRuntime(4520):at   android.app.Instrumentation.newActivity(Instrumentation.java:1021)   07-23 05:01:10.925:E / AndroidRuntime(4520):at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1578)   07-23 05:01:10.925:E / AndroidRuntime(4520):... 11 more

public class gallery extends ListActivity {

private List<String> item = null;

private List<String> path = null;

private String root = "sdcard/Crews/";

private TextView myPath;
VideoView videoView;

String[] itemArr;

String[] itemArr2 = item.toArray(new String[item.size()]);
ListView list;

File folder = new File("sdcard/Crews/Videos/");

MyThumbnaildapter fileList;

/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.gallery);
    myPath = (TextView) findViewById(R.id.path);

    getDir(root);

    videoView = (VideoView) this.findViewById(R.id.videoView);

}

private void getDir(String dirPath)

{

    myPath.setText("Location: " + dirPath);

    item = new ArrayList<String>();

    path = new ArrayList<String>();

    File f = new File(dirPath);

    File[] files = f.listFiles();

    if (!dirPath.equals(root))

    {

        item.add(root);

        path.add(root);

        path.add(f.getParent());

    }

    for (int i = 0; i < files.length; i++)

    {

        File file = files[i];

        path.add(file.getPath());

        if (file.isDirectory()) {

            item.add(file.getName() + "/");
        }

        else {

            item.add(file.getName());
        }

    }

    fileList = new MyThumbnaildapter(this, R.layout.row, itemArr2);
    setListAdapter(fileList);
    list = (ListView) findViewById(android.R.id.list);

    list.setAdapter(fileList);
    registerForContextMenu(list);

}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    final File file = new File(path.get(position));

    if (file.isDirectory())

    {

        if (file.canRead()) {

            getDir(path.get(position));
            // imageThumbnail.setImageBitmap(bmThumbnail);
        }

        else

        {

            new AlertDialog.Builder(this)

            .setIcon(R.drawable.icon)

            .setTitle("[" + file.getName() + "] folder can't be read!")

            .setPositiveButton("OK",

            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                    // TODO Auto-generated method stub

                }

            }).show();

        }

    }

    else

    {

        new AlertDialog.Builder(this)

        // .setIcon(R.drawable.icon)
        // .setView(getCurrentFocus())
                .setTitle("[" + file.getName() + "]")

                .setPositiveButton("OK",

                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        if (file.getPath().contains(".3gp")) {

                            Intent intent = new Intent();
                            intent.setAction(Intent.ACTION_VIEW);

                            intent.setDataAndType(
                                    Uri.fromFile(new File(file.getPath())),
                                    "video/*");

                            startActivity(intent);

                        } else if (file.getPath().contains(".jpg")) {
                            Intent intent = new Intent();
                            intent.setAction(Intent.ACTION_VIEW);

                            intent.setDataAndType(
                                    Uri.fromFile(new File(file.getPath())),
                                    "image/*");

                            startActivity(intent);
                        } else {
                            Toast displayError = Toast.makeText(
                                    gallery.this, "unsupported media type",
                                    Toast.LENGTH_SHORT);
                            displayError.show();
                        }

                    }

                }).show();
        // });
    }

}

}

public class MyThumbnaildapter extends ArrayAdapter<String> {

    public MyThumbnaildapter(Context context, int textViewResourceId,
            String[] objects) {
        super(context, textViewResourceId, objects);
        // TODO Auto-generated constructor stub
    }

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

        View row = convertView;
        if (row == null) {
            LayoutInflater inflater = getLayoutInflater();
            row = inflater.inflate(R.layout.row, parent, false);
        }



        ImageView imageThumbnail = (ImageView) row
                .findViewById(R.id.Thumbnail);


        Bitmap bmThumbnail;
        bmThumbnail = ThumbnailUtils.createVideoThumbnail(
                itemArr2[position], Thumbnails.MICRO_KIND);
        imageThumbnail.setImageBitmap(bmThumbnail);

        return row;
    }

}

1 个答案:

答案 0 :(得分:0)

盲目假设,

fileList = new MyThumbnaildapter(this, R.layout.row, itemArr2);

您的 itemArr2为空,因为您尝试在填充项目数组之前全局启动它。 等,

String[] itemArr2 = item.toArray(new String[item.size()]);

解决方案(可能):

String[] itemArr2 = item.toArray(new String[item.size()]); 填充item字符串数组后的行,

所以它应该在这里,

String[] itemArr2 = item.toArray(new String[item.size()]);
fileList = new MyThumbnaildapter(this, R.layout.row, itemArr2);
setListAdapter(fileList);

同时删除这两行,

list = (ListView) findViewById(android.R.id.list);
list.setAdapter(fileList);

调试应用程序代码,找出问题..