无法在自定义微调器Android中设置文本

时间:2014-05-08 06:02:33

标签: android android-spinner

在这里我只想在spinner android中的文本视图中设置文本,文本是从数据库获取的,但它总是出错java.lang.NullPointerExeception。 这是我的活动。 Erorr是以下登录代码。

   public class Front_end_akreditasi extends Activity {

    private DBDataSource dataSource;
MyAdapter spn;
    // Spinner element
 Spinner spinner;
 ArrayList<Akreditasi> dataArr = new ArrayList<Akreditasi>();
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.front_end_akreditasi);

          dataSource = new DBDataSource(this);


          dataSource.open();

          spinner   = (Spinner) findViewById(R.id.spinner_show);


          ArrayList<Akreditasi> values = dataSource.getnama_Akreditasi();
          for (Akreditasi cn : values) 
          {
                String log =  "ID :" + cn.getId() + "Name: " + cn.getNama();
                Log.d("Result: ", log);
                dataArr.add(cn);

              spn = new MyAdapter(this, R.layout.custom_spinner, dataArr);
              spinner.setAdapter(spn);
          }
         }
         public class MyAdapter extends ArrayAdapter<Akreditasi> {
        Context context;
        int layoutResourceId; 
        ArrayList<Akreditasi> data = new ArrayList<Akreditasi>();
    public MyAdapter(Context ctx, int txtViewResourceId, ArrayList<Akreditasi> objects)  
            {
              super(ctx, txtViewResourceId, objects);
            this.layoutResourceId = txtViewResourceId;
            this.context = ctx;
            this.data = objects;
    }
    @Override
    public View getDropDownView(int position, View cnvtView, ViewGroup prnt) {
        return getCustomView(position, cnvtView, prnt);
    }

    @Override
    public View getView(int pos, View cnvtView, ViewGroup prnt) {
        return getCustomView(pos, cnvtView, prnt);

    }
            public View getCustomView(int position, View convertView, ViewGroup parent) 
    {
        LayoutInflater inflater = getLayoutInflater();
        View mySpinner = inflater.inflate(R.layout.custom_spinner, parent,false);

        ImageHolder holder = null;

        if(mySpinner == null)
        {
            holder = new ImageHolder();
            holder.txtTitle     = (TextView)mySpinner.findViewById(R.id.text_main_seen);
            holder.imgIcon      = (ImageView)mySpinner.findViewById(R.id.left_pic);
            holder.txtName      = (TextView)mySpinner.findViewById(R.id.sub_text_seen);
            mySpinner.setTag(holder);
        }
        else
        {

            holder = (ImageHolder)mySpinner.getTag();
        }
        Akreditasi a = data.get(position);
                    //When i add this code below, then the program is error, it's says java.lang.NullPointerExeception
        holder.txtTitle.setText("coba");
        return mySpinner;

    }
   class ImageHolder
    {
        ImageView imgIcon;
        TextView txtTitle;
        TextView txtName;
    }

这是我的custom_spinner.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="wrap_content"
android:orientation="vertical"
android:padding="3px" >

<ImageView
    android:id="@+id/left_pic"
    android:layout_width="100px"
    android:layout_height="80px"
    android:background="@drawable/ic_launcher" />

<TextView
    android:id="@+id/text_main_seen"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5px"
    android:layout_marginTop="2px"
    android:layout_toRightOf="@+id/left_pic"
    android:padding="3px"
    android:text="JMD Group"
    android:textColor="#0022ee"
    android:textSize="22px"
    android:textStyle="bold" />

<TextView
    android:id="@+id/sub_text_seen"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/text_main_seen"
    android:layout_marginLeft="5dip"
    android:layout_toRightOf="@+id/left_pic"
    android:padding="2px"
    android:text="beyond the expectations..."
    android:textColor="#777777" />

 </RelativeLayout>

这是我的日志

  05-08 13:06:31.889: E/AndroidRuntime(16634): FATAL EXCEPTION: main
  05-08 13:06:31.889: E/AndroidRuntime(16634): java.lang.NullPointerException
  05-08 13:06:31.889: E/AndroidRuntime(16634):  at       com.example.frontend.category.search.Front_end_akreditasi$MyAdapter.getCustomView(Front_end      _akreditasi.java:165)
  05-08 13:06:31.889: E/AndroidRuntime(16634):  at       com.example.frontend.category.search.Front_end_akreditasi$MyAdapter.getView(Front_end_akred      itasi.java:138)
  05-08 13:06:31.889: E/AndroidRuntime(16634):  at       android.widget.AbsSpinner.onMeasure(AbsSpinner.java:193)
  05-08 13:06:31.889: E/AndroidRuntime(16634):  at android.widget.Spinner.onMeasure(Spinner.java:439)
  05-08 13:06:31.889: E/AndroidRuntime(16634):  at android.view.View.measure(View.java:15592)
  05-08 13:06:31.889: E/AndroidRuntime(16634):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
  05-08 13:06:31.889: E/AndroidRuntime(16634):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
  05-08 13:06:31.889: E/AndroidRuntime(16634):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
  05-08 13:06:31.889: E/AndroidRuntime(16634):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
  05-08 13:06:31.889: E/AndroidRuntime(16634):  at android.view.View.measure(View.java:15592)
  05-08 13:06:31.889: E/AndroidRuntime(16634):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
  05-08 13:06:31.889: E/AndroidRuntime(16634):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
  05-08 13:06:31.889: E/AndroidRuntime(16634):  at android.view.View.measure(View.java:15592)
  05-08 13:06:31.889: E/AndroidRuntime(16634):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
  05-08 13:06:31.889: E/AndroidRuntime(16634):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
  05-08 13:06:31.889: E/AndroidRuntime(16634):  at android.view.View.measure(View.java:15592)
  05-08 13:06:31.889: E/AndroidRuntime(16634):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
  05-08 13:06:31.889: E/AndroidRuntime(16634):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
  05-08 13:06:31.889: E/AndroidRuntime(16634):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2180)
  05-08 13:06:31.889: E/AndroidRuntime(16634):  at android.view.View.measure(View.java:15592)
  05-08 13:06:31.889: E/AndroidRuntime(16634):  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1878)
  05-08 13:06:31.889: E/AndroidRuntime(16634):  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
  05-08 13:06:31.889: E/AndroidRuntime(16634):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
  05-08 13:06:31.889: E/AndroidRuntime(16634):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
  05-08 13:06:31.889: E/AndroidRuntime(16634):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4356)
  05-08 13:06:31.889: E/AndroidRuntime(16634):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
  05-08 13:06:31.889: E/AndroidRuntime(16634):  at android.view.Choreographer.doCallbacks(Choreographer.java:562)
  05-08 13:06:31.889: E/AndroidRuntime(16634):  at android.view.Choreographer.doFrame(Choreographer.java:532)
  05-08 13:06:31.889: E/AndroidRuntime(16634):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
  05-08 13:06:31.889: E/AndroidRuntime(16634):  at android.os.Handler.handleCallback(Handler.java:725)
  05-08 13:06:31.889: E/AndroidRuntime(16634):  at android.os.Handler.dispatchMessage(Handler.java:92)
  05-08 13:06:31.889: E/AndroidRuntime(16634):  at android.os.Looper.loop(Looper.java:137)
  05-08 13:06:31.889: E/AndroidRuntime(16634):  at android.app.ActivityThread.main(ActivityThread.java:5099)
  05-08 13:06:31.889: E/AndroidRuntime(16634):  at java.lang.reflect.Method.invokeNative(Native Method)
  05-08 13:06:31.889: E/AndroidRuntime(16634):  at java.lang.reflect.Method.invoke(Method.java:511)
  05-08 13:06:31.889: E/AndroidRuntime(16634):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:803)
  05-08 13:06:31.889: E/AndroidRuntime(16634):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:570)
  05-08 13:06:31.889: E/AndroidRuntime(16634):  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:3)

尝试以下代码。我认为这应该有帮助

      holder = new ImageHolder();
                holder.txtTitle     = (TextView)mySpinner.findViewById(R.id.text_main_seen);
                holder.imgIcon      = (ImageView)mySpinner.findViewById(R.id.left_pic);
                holder.txtName      = (TextView)mySpinner.findViewById(R.id.sub_text_seen);      

            if(mySpinner == null)
            {

                mySpinner.setTag(holder); 
            }
            else
            {


                holder = (ImageHolder)mySpinner.getTag();
            }
            Akreditasi a = data.get(position);

            holder.txtTitle.setText("coba");
            return mySpinner;

        }

问题可能是holder.txtTitle在其他情况下仍为null,因为您没有按ID查找其视图。

答案 1 :(得分:0)

我认为它没有获取上下文所以请使用下面的代码创建LayoutInflater

LayoutInflater mInflater = LayoutInflater.from(context);

而不是这个,

LayoutInflater inflater = getLayoutInflater();