我正在尝试一个示例程序来理解动态GridView
的行为。当我尝试执行该程序时,我在.java文件中的texts.setText(str[position]);
处获得了运行时异常。
package GridView_dynamic.grid;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
public class GridView_dynamic extends Activity {
/** Called when the activity is first created. */
TextView select;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
select = (TextView)findViewById(R.id.text);
GridView grid = (GridView)findViewById(R.id.grid);
grid.setAdapter(new my_Adapter(this));
}
public class my_Adapter extends BaseAdapter{
String[] str = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N"};
public Context context;
public my_Adapter(Context context) {
// TODO Auto-generated constructor stub
this.context = context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return str.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView == null){
Toast.makeText(context,"convertView is NULL", Toast.LENGTH_LONG).show();
}
TextView texts;
texts = (TextView)convertView;
//texts = (TextView)findViewById(R.id.text);
texts.setText(str[position]);
return texts;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#0000aa"
android:textColor="#00aa00"
/>
<GridView
android:id="@+id/grid"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#aaaaaa"
/>
</LinearLayout>
答案 0 :(得分:1)
你得到NullPointerException
,因为当你得到一个空convertView
(列表是第一次建立的)时,你不会创建一个新的TextView
。您的getView()
方法应为:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView == null){
Toast.makeText(context,"convertView is NULL", Toast.LENGTH_LONG).show();
//if convertView is null is your job to make a new View
convertView = new TextView(context);
}
((TextView)convertView).setText(str[position]);
return convertView;
}
答案 1 :(得分:1)
从外观上看,你实际上并没有在getView中实例化TextView。如果convertView为null,则以下代码将创建一个新的textView。
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null)
convertView = new TextView(GridView_dynamic.this);
((TextView)convertView).setText(str[position]);
return convertView;
}
希望这有帮助!