我想在gridview项目中显示edittext和textview。我覆盖了baseadapter。当我运行它时,它是nullpointer。 我检查了数据输出。没关系,活动收到了正确的数据。
MarkViewActivity.java
package com.find.voter;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;
public class MarkViewActivity extends Activity{
private GridView gridview;
private ArrayList<String>list;
private Button reset;
private Button confirm;
private int checkNum;
private TextView topTextShow;
private int num1;
private GridAdapter mAdapter;
private boolean haveCookie;
private double []cookie;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridview2);
gridview=(GridView)findViewById(R.layout.gridview2);
reset=(Button)findViewById(R.id.reset2);
confirm=(Button)findViewById(R.id.confirm2);
topTextShow=(TextView)findViewById(R.id.checknumshow2);
list=new ArrayList<String>();
checkNum=0;
getInput();
mAdapter=new GridAdapter(list, this, num1,cookie);
//here !!!nullpointer
gridview.setAdapter(mAdapter);
}
private void getInput(){
Bundle receiveBundle=this.getIntent().getExtras();
cookie=receiveBundle.getDoubleArray("marklistcookie");
num1=receiveBundle.getInt("num1");
System.out.println(num1+"num1");
//get num1
cookie=new double[num1];
for(int i=0;i<num1;i++){
list.add((i+1)+"");
}
}
private void markCheck(){
int t=num1-checkNum;
if(t>0){
new AlertDialog.Builder(MarkViewActivity.this).setTitle("noteice").setMessage("ddddddd").setPositiveButton("ok", null).show();
}else{
Intent send=new Intent();
Bundle bundle=new Bundle();
bundle.putDoubleArray("markAnswer", cookie);
send.putExtra("ans2", bundle);
MarkViewActivity.this.setResult(2,send);
MarkViewActivity.this.finish();
}
}
private void markState(){
int t=num1-checkNum;
if(t>0)
topTextShow.setText("test1");
else
topTextShow.setText("test2");
}
private void dataChanged(){
mAdapter.notifyDataSetChanged();
markState();
}
GridAdapter.java
package com.find.voter;
import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
public class GridAdapter extends BaseAdapter{
private ArrayList<String>list;
private static double isMarked[];
private Context context;
private LayoutInflater infalter=null;
private int num1;
public GridAdapter(ArrayList<String>list,Context context,int num1,double[]xx) {
this.context=context;
this.list=list;
infalter=LayoutInflater.from(context);
isMarked=xx;
this.num1=num1;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public static double [] getIsMarked(){return isMarked;}
public static void setIsMarked(double [] m){
GridAdapter.isMarked=m;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder=null;
if(convertView==null){
holder=new ViewHolder();
convertView=infalter.inflate(R.layout.itemgrid2, null);
holder.tv=(TextView)convertView.findViewById(R.id.grid_tv);
holder.et=(EditText)convertView.findViewById(R.id.grid_edittext);
convertView.setTag(holder);
}else{
holder=(ViewHolder)convertView.getTag();
}
holder.tv.setText(list.get(position));
holder.et.setText(getIsMarked()[position]+"");
return convertView;
}
public static class ViewHolder {
TextView tv;
EditText et;
}
}
itemgrid2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/grid_tv"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="50sp"
android:layout_weight="1" />
<EditText
android:id="@+id/grid_edittext"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
</LinearLayout>
gridview2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/checknumshow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textSize="30sp"
android:text="" />
<LinearLayout
android:id="@+id/linear2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
>
<Button
android:id="@+id/reset2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/buttonBarButtonStyle"
android:textSize="30sp"
android:text="@string/reset" />
<Button
android:id="@+id/confirm2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/buttonBarButtonStyle"
android:textSize="30sp"
android:text="@string/confirm" />
</LinearLayout>
<GridView
android:id="@+id/grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/linear2"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center" />
logcat
08-19 10:16:18.412: E/AndroidRuntime(9923): FATAL EXCEPTION: main
08-19 10:16:18.412: E/AndroidRuntime(9923): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.find.voter/com.find.voter.MarkViewActivity}: java.lang.NullPointerException
08-19 10:16:18.412: E/AndroidRuntime(9923): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-19 10:16:18.412: E/AndroidRuntime(9923): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-19 10:16:18.412: E/AndroidRuntime(9923): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-19 10:16:18.412: E/AndroidRuntime(9923): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-19 10:16:18.412: E/AndroidRuntime(9923): at android.os.Handler.dispatchMessage(Handler.java:99)
08-19 10:16:18.412: E/AndroidRuntime(9923): at android.os.Looper.loop(Looper.java:137)
08-19 10:16:18.412: E/AndroidRuntime(9923): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-19 10:16:18.412: E/AndroidRuntime(9923): at java.lang.reflect.Method.invokeNative(Native Method)
08-19 10:16:18.412: E/AndroidRuntime(9923): at java.lang.reflect.Method.invoke(Method.java:511)
08-19 10:16:18.412: E/AndroidRuntime(9923): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-19 10:16:18.412: E/AndroidRuntime(9923): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-19 10:16:18.412: E/AndroidRuntime(9923): at dalvik.system.NativeStart.main(Native Method)
08-19 10:16:18.412: E/AndroidRuntime(9923): Caused by: java.lang.NullPointerException
08-19 10:16:18.412: E/AndroidRuntime(9923): at com.find.voter.MarkViewActivity.onCreate(MarkViewActivity.java:44)
08-19 10:16:18.412: E/AndroidRuntime(9923): at android.app.Activity.performCreate(Activity.java:5104)
08-19 10:16:18.412: E/AndroidRuntime(9923): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-19 10:16:18.412: E/AndroidRuntime(9923): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-19 10:16:18.412: E/AndroidRuntime(9923): ... 11 more
答案 0 :(得分:1)
这一行:
gridview=(GridView)findViewById(R.layout.gridview2);
不正确。您想参考R.id.grid