2 mr qifei Mr_qifei 于 2016.02.28 15:58 提问

Android 里面自己定义的Adapter运行崩溃

class MyAdapter extends ArrayAdapter {

    public MyAdapter(Context context, int resource) {
        super(context, resource);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

** @Override

public int getCount() {

return imgs.length;

}**

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


        View view=null;
        if(convertView==null){
            LayoutInflater inflater= (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
            inflater.inflate(R.layout.item,null);
        }else {
            view=convertView;
        }
        ImageView iv=(ImageView)view.findViewById(R.id.imageView);
        TextView tv= (TextView) view.findViewById(R.id.textView);
        iv.setImageResource(imgs[position]);
        tv.setText(names[position]);
        return view;
    }
}
    运行到加黑的getCount的时候程序就会崩溃,编程小白,望指教

7个回答

tianyaleixiaowu
tianyaleixiaowu   2016.03.02 16:43
已采纳
luochoudan
luochoudan   Ds   Rxr 2016.02.28 22:07

你敲的肯定跟人家的不一样。试一下这个,性能不太好,网上这种例子很多,多百度百度。
public View getView(int position ,View convertView,ViewGroup parent){
LayoutInflater inflater= (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
view=inflater.inflate(R.layout.item,null);
ImageView iv=(ImageView)view.findViewById(R.id.imageView);
TextView tv= (TextView) view.findViewById(R.id.textView);
iv.setImageResource(imgs[position]);
tv.setText(names[position]);
return view;
}

dongfeng9ge
dongfeng9ge   Ds   Rxr 2016.02.29 08:25

你的convertView没有初始化

u012131702
u012131702   2016.02.28 16:10

当然啦,convertView都没有初始化……………… inflater时

Mr_qifei
Mr_qifei   2016.02.28 21:00

你说的是view=inflater.inflate(R.layout.item,null);吗。。。确实没注意到这个,谢谢啦。。可是改了之后程序还是崩溃。。。。困扰我几天了。。。。

package com.example.mrzhou.gridviewprogress;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

public int[] imgs={R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
        R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};
public String[] names={"a","b","c","d","e","f","g"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    GridView gv= (GridView) this.findViewById(R.id.gridView);
    MyAdapter adapter=new MyAdapter(this,0);
    gv.setAdapter(adapter);


    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

class MyAdapter extends ArrayAdapter {

    public MyAdapter(Context context, int resource) {
        super(context, resource);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getCount() {
        return imgs.length;
    }

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


        View view=null;
        if(convertView==null){
            LayoutInflater inflater= (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
            view=inflater.inflate(R.layout.item,null);
        }else {
            view=convertView;
        }
        ImageView iv=(ImageView)view.findViewById(R.id.imageView);
        TextView tv= (TextView) view.findViewById(R.id.textView);
        iv.setImageResource(imgs[position]);
        tv.setText(names[position]);
        return view;
    }
}

}
这是整个mainactivity,我是在看视频学习,和他教的一样来的,可就是出错。。。望指教。。万分感谢

Mr_qifei
Mr_qifei 你说的是view=inflater.inflate(R.layout.item,null);吗。。。确实没注意到这个,谢谢啦。。可是改了之后程序还是崩溃。。。。困扰我几天了。。。。下面的是我的全部代码。。麻烦你了
接近 2 年之前 回复
Mr_qifei
Mr_qifei   2016.02.28 21:00

你说的是view=inflater.inflate(R.layout.item,null);吗。。。确实没注意到这个,谢谢啦。。可是改了之后程序还是崩溃。。。。困扰我几天了。。。。

package com.example.mrzhou.gridviewprogress;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

public int[] imgs={R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
        R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};
public String[] names={"a","b","c","d","e","f","g"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    GridView gv= (GridView) this.findViewById(R.id.gridView);
    MyAdapter adapter=new MyAdapter(this,0);
    gv.setAdapter(adapter);


    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

class MyAdapter extends ArrayAdapter {

    public MyAdapter(Context context, int resource) {
        super(context, resource);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getCount() {
        return imgs.length;
    }

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


        View view=null;
        if(convertView==null){
            LayoutInflater inflater= (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
            view=inflater.inflate(R.layout.item,null);
        }else {
            view=convertView;
        }
        ImageView iv=(ImageView)view.findViewById(R.id.imageView);
        TextView tv= (TextView) view.findViewById(R.id.textView);
        iv.setImageResource(imgs[position]);
        tv.setText(names[position]);
        return view;
    }
}

}
这是整个mainactivity,我是在看视频学习,和他教的一样来的,可就是出错。。。望指教。。万分感谢

qq_27578023
qq_27578023   2016.06.08 22:44
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!