Array Adapter强制关闭我的Android应用程序

时间:2012-11-24 01:51:49

标签: android eclipse listview android-arrayadapter

所以我正在尝试实现自定义数组适配器。到目前为止,我已经成功到了将其设置为ListFragment的地步。然后问题开始发生。我在我的主xml布局中有一个FrameLayout,我使用FragmentTransaction添加了我的ListFragment(它还没有任何代码)。这是我的代码:

ArrayAdapter:

package tj.apps.files;

import java.util.ArrayList;

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;
import android.widget.TextView;

public class ItemAdapter extends ArrayAdapter<Item> {

int resource; 
Context context;
ArrayList<Item> items;

public ItemAdapter(Context context, int resource, ArrayList<Item> items) {
    super(context, resource, items);
    this.resource = resource;
    this.context = context;
    this.items = items;
}

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
    ItemHolder holder = null;

    if (convertView == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        convertView = inflater.inflate(resource, parent, false);

        holder = new ItemHolder();

        convertView.setClickable(true);
        convertView.setFocusable(true);

        holder.image= (ImageView) convertView.findViewById(R.id.item_image);
        holder.text = (TextView) convertView.findViewById(R.id.item_text);

        convertView.setTag(holder);

    } else {
        holder = (ItemHolder) convertView.getTag();
    }

    holder.image.setImageResource(R.drawable.search_icon);
    holder.text.setText(items.get(position).getName());

    return convertView;
}

static class ItemHolder {
    ImageView image;
    TextView text;
}
}

主要活动:

package tj.apps.files;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

public class FilesList extends SherlockFragmentActivity {

private ItemAdapter aa; 
private ArrayList<Item> array; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.files_list);

    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.add(R.id.fragment_listview, new ListFragment());
    ft.commit();

    array = new ArrayList<Item>();
    array.add(new Item("Help"));
    aa = new ItemAdapter(this, R.layout.list_item, array);

    ListFragment listFragment = (ListFragment)     fm.findFragmentById(R.id.fragment_listview);
    listFragment.setListAdapter(aa);
}

@Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.files_list, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_search:
        return true;
    case R.id.menu_edit_mode:
        return true; 
    case R.id.menu_help:
        return true;
    case R.id.menu_about: 
        return true; 
    default: 
        return super.onOptionsItemSelected(item);
    }
}

}

自定义行布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/item_image" 
    android:layout_width="@dimen/item_size"
    android:layout_height="@dimen/item_size"
    />

<TextView
    android:id="@+id/item_text"
    android:layout_width="match_parent"
    android:layout_height="@dimen/item_size"
    android:padding="15sp"
    android:textSize="@dimen/item_text_size"
    />

</LinearLayout>

这是我的logcat:

11-23 17:26:22.983: E/AndroidRuntime(30582): FATAL EXCEPTION: main
11-23 17:26:22.983: E/AndroidRuntime(30582): java.lang.RuntimeException: Unable to start activity ComponentInfo{tj.apps.files/tj.apps.files.FilesList}: java.lang.NullPointerException
11-23 17:26:22.983: E/AndroidRuntime(30582):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
11-23 17:26:22.983: E/AndroidRuntime(30582):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-23 17:26:22.983: E/AndroidRuntime(30582):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-23 17:26:22.983: E/AndroidRuntime(30582):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-23 17:26:22.983: E/AndroidRuntime(30582):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-23 17:26:22.983: E/AndroidRuntime(30582):    at android.os.Looper.loop(Looper.java:137)
11-23 17:26:22.983: E/AndroidRuntime(30582):    at android.app.ActivityThread.main(ActivityThread.java:4745)
11-23 17:26:22.983: E/AndroidRuntime(30582):    at java.lang.reflect.Method.invokeNative(Native Method)
11-23 17:26:22.983: E/AndroidRuntime(30582):    at java.lang.reflect.Method.invoke(Method.java:511)
11-23 17:26:22.983: E/AndroidRuntime(30582):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
11-23 17:26:22.983: E/AndroidRuntime(30582):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
11-23 17:26:22.983: E/AndroidRuntime(30582):    at dalvik.system.NativeStart.main(Native Method)
11-23 17:26:22.983: E/AndroidRuntime(30582): Caused by: java.lang.NullPointerException
11-23 17:26:22.983: E/AndroidRuntime(30582):    at tj.apps.files.FilesList.onCreate(FilesList.java:33)
11-23 17:26:22.983: E/AndroidRuntime(30582):    at android.app.Activity.performCreate(Activity.java:5008)
11-23 17:26:22.983: E/AndroidRuntime(30582):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-23 17:26:22.983: E/AndroidRuntime(30582):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
11-23 17:26:22.983: E/AndroidRuntime(30582):    ... 11 more

如果我在主活动中添加setListAdapter方法,应用程序将开始强制关闭。如果我将其移除,则没有力量关闭。这是什么问题?

0 个答案:

没有答案