ListView与ImageButton和文本

时间:2016-03-08 21:06:52

标签: java android listview

我尝试使用imagebutton和text制作listview。不知怎的,当我运行它时,我的应用程序崩溃了。我需要有人可以通过我的日志来查看我所犯的错误。请帮忙,因为我没有编码背景。

这是我的main_java = IngredientChoices.java

public class IngredientChoices extends AppCompatActivity {

ListView lv;

String[] category_titles={"Baking Goods", "Beverages/Alcohol", "Dairy", "Fruit", "Herbs & Spices", "Meat", "Nuts & Seeds", "Pasta, Breads & Grains", "Poultry", "Sauces, Soup & Condiments", "Seafood", "Vegetables"};
int [] category_icons={R.drawable.bakingood,R.drawable.beverages, R.drawable.dairy, R.drawable.apple, R.drawable.herbs, R.drawable.meat, R.drawable.nuts, R.drawable.bread, R.drawable.poultry, R.drawable.soup, R.drawable.seafood, R.drawable.vegetable};

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

    lv = (ListView) findViewById(R.id.listView);

    //Adapter

    IngredientCategoryAdapter adapter = new IngredientCategoryAdapter(this, category_titles, category_icons);
    lv.setAdapter(adapter);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getApplicationContext(), category_titles[position], Toast.LENGTH_SHORT).show();

        }
    });



}
}

这是我的adapter.java

public class IngredientCategoryAdapter extends ArrayAdapter<String> {

// Declarations
int[] category_icons={};
String[] category_titles={};
Context c;
LayoutInflater inflater;

public IngredientCategoryAdapter(Context context, String[]      category_title, int[] category_icon) {
    super(context, R.layout.ingcategory_list_view, category_title);

    this.c=context;
    this.category_icons=category_icons;
    this.category_titles=category_titles;

}

public class ViewHolder {

    TextView category_title;
    ImageButton category_icon;
}

@SuppressLint("InflateParams")
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if(convertView==null){
        inflater= (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView=inflater.inflate(R.layout.ingcategory_list_view, null);
    }

    //our viewholder object

    final ViewHolder holder = new ViewHolder();

    // Initialize our views
    holder.category_title= (TextView) convertView.findViewById(R.id.category_title);
    holder.category_icon=(ImageButton) convertView.findViewById(R.id.category_icon);

    //Assign data

    holder.category_icon.setImageResource(category_icons[position]);
    holder.category_title.setText(category_titles[position]);


    return convertView;
}
}

谢谢!

崩溃日志

  

03-08 15:23:17.545 8166-8224 /? E / BooksSync:软错误                                               com.google.android.apps.books.net.HttpHelper $ AuthIoException:403:Forbidden                                               请求:获取https://encrypted.google.com/books?cp_activate=2%3AC4q8DQZi0PFAsa4LWbLehD8AAAA6gAd6-baW_r-qDKNVNPTjsycPW8Iqe-VhNJcXNKhi02FNTv6sb_oW-B67bwycWnmqttWYrKqXuC56--BtZlv5 HTTP / 1.1   03-08 15:23:17.555 8166-8224 /? E / BooksSync:同步错误                                               com.google.android.apps.books.net.HttpHelper $ AuthIoException:403:Forbidden                                               请求:获取https://encrypted.google.com/books?cp_activate=2%3AC4q8DQZi0PFAsa4LWbLehD8AAAA6gAd6-baW_r-qDKNVNPTjsycPW8Iqe-VhNJcXNKhi02FNTv6sb_oW-B67bwycWnmqttWYrKqXuC56--BtZlv5 HTTP / 1.1   03-08 15:23:20.418 1281-1281 /? E / LSO:LSO服务还没有准备好!!!   03-08 15:23:20.518 1281-1281 /? E / SoundPool:加载错误/system/media/audio/ui/lens_flare_tap.ogg   03-08 15:23:20.518 1281-1281 /? E / SoundPool:加载错误/system/media/audio/ui/lens_flare_unlock.ogg   03-08 15:23:20.758 300-18933 /? E / AwesomePlayer:LPAPlayer ::不支持小于30秒的剪辑持续时间设置,默认为60秒   03-08 15:23:20.758 300-18933 /? E / AwesomePlayer:LPAPlayer ::不支持小于30秒的剪辑持续时间设置,默认为60秒   03-08 15:23:20.958 300-19477 /? E / AwesomePlayer:LPAPlayer ::不支持小于30秒的剪辑持续时间设置,默认为60秒   03-08 15:23:20.958 300-19477 /? E / AwesomePlayer:LPAPlayer ::不支持小于30秒的剪辑持续时间设置,默认为60秒   03-08 15:23:22.050 8338-8338 /? E / Diag_Lib:Diag_LSM_Init:无法打开diag驱动程序的句柄,错误= 2   03-08 15:23:23.571 8267-8267 /? E / SMD:smd_init开始   03-08 15:23:23.571 8267-8267 /? E / SMD:smd接口打开失败errno是2 -1   03-08 15:23:24.332 884-1422 /? E /看门狗:!@Sync 10416   03-08 15:23:24.572 8267-8267 /? E / SMD:smd接口打开失败errno是2 -1   03-08 15:23:25.573 8267-8267 /? E / SMD:smd接口打开失败errno是2 -1   03-08 15:23:26.574 8267-8267 /? E / SMD:smd接口打开失败errno是2 -1   03-08 15:23:27.505 8339-8339 /? E / Diag_Lib:Diag_LSM_Init:无法打开diag驱动程序的句柄,错误= 2   03-08 15:23:27.565 8267-8267 /? E /   03-08 15:24:00.677 884-2563 /? E / PersonaManagerService:inState():stateMachine为null !!   03-08 15:24:01.058 8434-8434 /? E / AndroidRuntime:致命异常:主要                                                    处理:com.example.cassieleong.delishcart,PID:8434                                                    java.lang.ArrayIndexOutOfBoundsException:length = 0;索引= 0                                                        在com.example.cassieleong.delishcart.IngredientCategoryAdapter.getView(IngredientCategoryAdapter.java:59)                                                        在android.widget.AbsListView.obtainView(AbsListView.java:2823)                                                        在android.widget.ListView.measureHeightOfChildren(ListView.java:1290)                                                        在android.widget.ListView.onMeasure(ListView.java:1202)                                                        在android.view.View.measure(View.java:18596)                                                        在android.widget.RelativeLayout.measureChildHorizo​​ntal(RelativeLayout.java:728)                                                        在android.widget.RelativeLayout.onMeasure(RelativeLayout.java:464)                                                        在android.view.View.measure(View.java:18596)                                                        在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)                                                        在android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:610)                                                        在android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:677)                                                        在android.view.View.measure(View.java:18596)                                                        在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)                                                        在android.widget.FrameLayout.onMeasure(FrameLayout.java:430)                                                        在android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)                                                        在android.view.View.measure(View.java:18596)                                                        在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)                                                        在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1435)                                                        在android.widget.LinearLayout.measureVertical(LinearLayout.java:721)                                                        在android.widget.LinearLayout.onMeasure(LinearLayout.java:612)                                                        在android.view.View.measure(View.java:18596)                                                        在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)                                                        在android.widget.FrameLayout.onMeasure(FrameLayout.java:430)                                                        在android.view.View.measure(View.java:18596)                                                        在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)                                                        在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1435)                                                        在android.widget.LinearLayout.measureVertical(LinearLayout.java:721)                                                        在android.widget.LinearLayout.onMeasure(LinearLayout.java:612)                                                        在android.view.View.measure(View.java:18596)                                                        在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)                                                        在android.widget.FrameLayout.onMeasure(FrameLayout.java:430)                                                        在com.android.internal.policy.impl.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2897)                                                        在android.view.View.measure(View.java:18596)                                                        在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2248)                                                        在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1306)                                                        在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1548)                                                        在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1191)                                                        在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6642)                                                        在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:777)                                                        在android.view.Choreographer.doCallbacks(Choreographer.java:590)                                                        在android.view.Choreographer.doFrame(Choreographer.java:560)                                                        在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:763)                                                        在android.os.Handler.handleCallback(Handler.java:739)                                                        在android.os.Handler.dispatchMessage(Handler.java:95)                                                        在android.os.Looper.loop(Looper.java:145)                                                        在android.app.ActivityThread.main(ActivityThread.java:5942)                                                        at java.lang.reflect.Method.invoke(Native Method)                                                        在java.lang.reflect.Method.invoke(Method.java:372)                                                        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1399)                                                        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)   03-08 15:24:01.448 884-8480 /? E / android.os.Debug:!@ Rollstate&gt; sdumpstate -k -t -z -d -o / data / log / dumpstate_app_error   03-08 15:24:01.578 8267-8267 /? E / SMD:smd接口打开失败errno是2 -1   03-08 15:24:02.579 8267-8267 /? E / SMD:smd接口打开失败errno是2 -1   03-08 15:24:02.639 8503-8503 /? E / Diag_Lib:Diag_LSM_Init:无法打开diag驱动程序的句柄,错误= 2   03-08 15:24:03.580 8267-8267 /? E / SMD:smd接口打开失败errno是2 -1   03-08 15:24:04.581 8267-8267 /? E / SMD:smd接口打开失败errno是2 -1   03-08 15:24:05.592 8267-8267 /? E / SMD:smd接口打开失败errno是2 -1   03-08 15:24:06.593 8267-8267 /? E / SMD:smd接口打开失败errno是2 -1   03-08 15:24:07.594 8267-8267 /? E / SMD:smd接口打开失败errno是2 -1

2 个答案:

答案 0 :(得分:0)

有时在android中获取没有上下文的资源是一个问题。尝试改变:

holder.category_icon.setImageResource(category_icons[position]);

holder.category_icon.setImageDrawable(c.getResources().getDrawable(category_icons[position]));
编辑:好吧,你的数组中的一个(或两个)似乎是空的。在onCreate(Bundle savedInstanceState)

中初始化适配器之前,请尝试填充数据

ListView lv;

String[] category_titles;
int [] category_icons;

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

    lv = (ListView) findViewById(R.id.listView);

    category_titles = {"Baking Goods", "Beverages/Alcohol", "Dairy", "Fruit", "Herbs & Spices", "Meat", "Nuts & Seeds", "Pasta, Breads & Grains", "Poultry", "Sauces, Soup & Condiments", "Seafood", "Vegetables"};
    category_icons={R.drawable.bakingood,R.drawable.beverages, R.drawable.dairy, R.drawable.apple, R.drawable.herbs, R.drawable.meat, R.drawable.nuts, R.drawable.bread, R.drawable.poultry, R.drawable.soup, R.drawable.seafood, R.drawable.vegetable}
    //Adapter

    IngredientCategoryAdapter adapter = new IngredientCategoryAdapter(this, category_titles, category_icons);
    lv.setAdapter(adapter);

答案 1 :(得分:0)

适配器构造函数存在问题。 将您的适配器完全更改为:

public IngredientCategoryAdapter(Context context, String[] category_title, int[] category_icon) {
    super(context, R.layout.ingcategory_list_view, category_title);

    this.c=context;
    this.category_icons=category_icon;
    this.category_titles=category_title;
}