我在gridview中显示数据。但它给了我nullpointerexception。当我不通过
显示数据时oid.setText(sdata.getOid()); then it is giving me values and not any exception.
但是当我想在textview中显示数据时。它给了我nullpointer异常。
这是代码: -
griddata gdata = new griddata(search.this,R.layout.griditem,r);
gridview.setAdapter(gdata);
public class griddata extends ArrayAdapter<searchdata>
{
Context context;
private ArrayList<searchdata> items;
public griddata(Context context, int textViewResourceId, ArrayList<searchdata> items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public int getCount()
{
//return numbers of element u want on the grid
return items.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View v;
if (convertView == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = new View(context);
v = vi.inflate(R.layout.griditem, null);
} else
{
v = convertView;
}
searchdata sdata = items.get(position);
System.out.println("The data is "+sdata.getOid());
if(sdata != null)
{
TextView oid = (TextView) findViewById(R.id.oid);
oid.setText(sdata.getOid());
/* TextView odate = (TextView) findViewById(R.id.odate);
TextView osdate = (TextView) findViewById(R.id.osdate);
TextView semail = (TextView) findViewById(R.id.semail);
TextView ssaddress = (TextView) findViewById(R.id.ssaddress);
TextView shipzip = (TextView) findViewById(R.id.shipzip);
TextView scost = (TextView) findViewById(R.id.scost);
TextView noofi = (TextView) findViewById(R.id.noofi);
TextView noofc = (TextView) findViewById(R.id.noofc);
TextView handfee = (TextView) findViewById(R.id.handfee);
oid.setText(sdata.getOid());
/*odate.setText(sdata.getOdate());
osdate.setText(sdata.getOsdate());
semail.setText(sdata.getSemail());
ssaddress.setText(sdata.getSsaddress());
shipzip.setText(sdata.getSzip());
scost.setText(sdata.getScost());
noofi.setText(sdata.getNoofi());
noofc.setText(sdata.getNoofc());
handfee.setText(sdata.getHannlgfee());*/
}
return v;}
@Override
public searchdata getItem(int arg0) {
return items.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;} }
和我的getitem.xml: -
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<LinearLayout
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingBottom="5dip"
android:paddingTop="5dip"
android:orientation="horizontal">
<TextView
android:id="@+id/oid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="17dp"
android:text="TextView" />
<TextView
android:id="@+id/odate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="17dp"
android:text="TextView" />
<TextView
android:id="@+id/osdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="17dp"
android:text="TextView" />
<TextView
android:id="@+id/semail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="17dp"
android:text="TextView" />
<TextView
android:id="@+id/ssaddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="17dp"
android:text="TextView" />
<TextView
android:id="@+id/shipzip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="17dp"
android:text="TextView" />
<TextView
android:id="@+id/scost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="17dp"
android:text="TextView" />
<TextView
android:id="@+id/noofi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="17dp"
android:text="TextView" />
<TextView
android:id="@+id/noofc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="17dp"
android:text="TextView" />
<TextView
android:id="@+id/handfee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="17dp"
android:text="TextView" />
</LinearLayout>
05-15 15:58:48.646: E/AndroidRuntime(2500): java.lang.NullPointerException
05-15 15:58:48.646: E/AndroidRuntime(2500): at android.view.ViewConfiguration.get(ViewConfiguration.java:332)
05-15 15:58:48.646: E/AndroidRuntime(2500): at android.view.View.<init>(View.java:3236)
05-15 15:58:48.646: E/AndroidRuntime(2500): at com.example.ship2impress.search$griddata.getView(search.java:297)
05-15 15:58:48.646: E/AndroidRuntime(2500): at android.widget.AbsListView.obtainView(AbsListView.java:2271)
05-15 15:58:48.646: E/AndroidRuntime(2500): at android.widget.GridView.onMeasure(GridView.java:1030)
05-15 15:58:48.646: E/AndroidRuntime(2500): at android.view.View.measure(View.java:15172)
05-15 15:58:48.646: E/AndroidRuntime(2500): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
05-15 15:58:48.646: E/AndroidRuntime(2500): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
05-15 15:58:48.646: E/AndroidRuntime(2500): at android.view.View.measure(View.java:15172)
05-15 15:58:48.646: E/AndroidRuntime(2500): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
05-15 15:58:48.646: E/AndroidRuntime(2500): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
05-15 15:58:48.646: E/AndroidRuntime(2500): at android.view.View.measure(View.java:15172)
05-15 15:58:48.646: E/AndroidRuntime(2500): at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
05-15 15:58:48.646: E/AndroidRuntime(2500): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
05-15 15:58:48.646: E/AndroidRuntime(2500): at android.view.View.measure(View.java:15172)
05-15 15:58:48.646: E/AndroidRuntime(2500): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
05-15 15:58:48.646: E/AndroidRuntime(2500): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
05-15 15:58:48.646: E/AndroidRuntime(2500): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
05-15 15:58:48.646: E/AndroidRuntime(2500): at android.view.View.measure(View.java:15172)
05-15 15:58:48.646: E/AndroidRuntime(2500): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1850)
05-15 15:58:48.646: E/AndroidRuntime(2500): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1102)
05-15 15:58:48.646: E/AndroidRuntime(2500): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1275)
05-15 15:58:48.646: E/AndroidRuntime(2500): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
05-15 15:58:48.646: E/AndroidRuntime(2500): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
05-15 15:58:48.646: E/AndroidRuntime(2500): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
05-15 15:58:48.646: E/AndroidRuntime(2500): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
05-15 15:58:48.646: E/AndroidRuntime(2500): at android.view.Choreographer.doFrame(Choreographer.java:525)
05-15 15:58:48.646: E/AndroidRuntime(2500): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
05-15 15:58:48.646: E/AndroidRuntime(2500): at android.os.Handler.handleCallback(Handler.java:615)
05-15 15:58:48.646: E/AndroidRuntime(2500): at android.os.Handler.dispatchMessage(Handler.java:92)
05-15 15:58:48.646: E/AndroidRuntime(2500): at android.os.Looper.loop(Looper.java:137)
05-15 15:58:48.646: E/AndroidRuntime(2500): at android.app.ActivityThread.main(ActivityThread.java:4745)
05-15 15:58:48.646: E/AndroidRuntime(2500): at java.lang.reflect.Method.invokeNative(Native Method)
05-15 15:58:48.646: E/AndroidRuntime(2500): at java.lang.reflect.Method.invoke(Method.java:511)
05-15 15:58:48.646: E/AndroidRuntime(2500): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-15 15:58:48.646: E/AndroidRuntime(2500): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-15 15:58:48.646: E/AndroidRuntime(2500): at dalvik.system.NativeStart.main(Native Method)
我认为问题可能出在getitem.xml中。 请帮帮我。谢谢你的时间安排。
答案 0 :(得分:1)
使用以下更改
public griddata(Context context, int textViewResourceId, ArrayList<searchdata> items) {
super(context, textViewResourceId, items);
this.items = items;
this.context = context ;
}
在获取视图中
if (convertView == null) {
convertView = LayoutInflater.from(conetxt).inflate(R.layout.griditem, null);
}
和
TextView oid = (TextView) convertView.findViewById(R.id.oid);
答案 1 :(得分:0)
更改此
TextView oid = (TextView) findViewById(R.id.oid);
oid.setText(sdata.getOid());
到这个
TextView oid = (TextView) v.findViewById(R.id.oid);
oid.setText(sdata.getOid());