无法弄清楚这个空指针异常

时间:2013-11-16 17:40:56

标签: android nullpointerexception textview onclicklistener onlongclicklistener

我将onClick侦听器添加到适配器中的文本字段。我只想将该字段中的文本发布到我的文本输入框(edittext1)。我正在尝试下面的代码,但它在edttext1的行指向给我一个NullPoint异常。请帮忙

public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        LayoutInflater inflater2 = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater2.inflate(R.layout.activity_discuss, parent, false);

        if (row == null) {
            LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if(type==1 || type==3)
                row = inflater.inflate(R.layout.listitem_discuss, parent, false);
            else if(type==4 || type==5)
                row = inflater.inflate(R.layout.listitem_users, parent, false);
            else
                row = inflater.inflate(R.layout.listitem_messages, parent, false);
        }

    //  


        final OneComment coment = getItem(position);

        userComment = (TextView) row.findViewById(R.id.comment);
        userImage = (ImageView) row.findViewById(R.id.place_img);
        userName = (TextView) row.findViewById(R.id.place_name);
        userOnlineImage = (ImageView) row.findViewById(R.id.pe_profile_pic);
        commentDate = (TextView) row.findViewById(R.id.place_distance);
        msgPic = (ImageView) row.findViewById(R.id.msgPic);

        userComment.setText(coment.comment);
        editText1 = (EditText) row.findViewById(R.id.cr_room_name);

        userName.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                editText1.setText("testing");
                Log.v("response","username selected");
                // TODO Auto-generated method stub

            }
            });

我实际上更喜欢将其设为Longclick。设置是否存在差异。

堆栈跟踪

11-16 12:46:29.100: E/AndroidRuntime(8047): FATAL EXCEPTION: main
11-16 12:46:29.100: E/AndroidRuntime(8047): java.lang.NullPointerException
11-16 12:46:29.100: E/AndroidRuntime(8047):     at com.peekatucorp.peekatu.DiscussArrayAdapter$1.onClick(DiscussArrayAdapter.java:129)
11-16 12:46:29.100: E/AndroidRuntime(8047):     at android.view.View.performClick(View.java:4240)
11-16 12:46:29.100: E/AndroidRuntime(8047):     at android.view.View$PerformClick.run(View.java:17721)
11-16 12:46:29.100: E/AndroidRuntime(8047):     at android.os.Handler.handleCallback(Handler.java:730)
11-16 12:46:29.100: E/AndroidRuntime(8047):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-16 12:46:29.100: E/AndroidRuntime(8047):     at android.os.Looper.loop(Looper.java:137)
11-16 12:46:29.100: E/AndroidRuntime(8047):     at android.app.ActivityThread.main(ActivityThread.java:5103)
11-16 12:46:29.100: E/AndroidRuntime(8047):     at java.lang.reflect.Method.invokeNative(Native Method)
11-16 12:46:29.100: E/AndroidRuntime(8047):     at java.lang.reflect.Method.invoke(Method.java:525)
11-16 12:46:29.100: E/AndroidRuntime(8047):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-16 12:46:29.100: E/AndroidRuntime(8047):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-16 12:46:29.100: E/AndroidRuntime(8047):     at dalvik.system.NativeStart.main(Native Method)

Actvity_discuss.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="vertical" 
    >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/form"
        android:layout_alignParentTop="true"
        android:choiceMode="none"
        android:divider="#00000000"
        android:dividerHeight="@dimen/zerosize"
        android:footerDividersEnabled="false"
        android:headerDividersEnabled="false" 
        android:listSelector="@android:color/transparent">

    </ListView>

    <RelativeLayout
        android:id="@+id/form"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:background="@drawable/inputbg" >

            <!-- <Button
                android:id="@+id/postpic"
                android:layout_width="30dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/attachbtn" />  -->
  <!-- android:background="@drawable/sendbutton"  -->
            <EditText
                android:id="@+id/cr_room_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_toLeftOf="@+id/sendMessage"
                android:layout_weight="3"
                android:ems="10"
                android:hint="Enter Message"
                android:inputType="text" />

            <ImageView
                android:id="@+id/postpic"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:src="@drawable/picattach" />

            <Button
                android:id="@+id/sendMessage"
                style="@style/SingleGradient"
                android:layout_width="35dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

        </LinearLayout>

</RelativeLayout>

</RelativeLayout>

4 个答案:

答案 0 :(得分:0)

static class ViewHolder {
       TextView textView;
        TextView timeView;
        ImageView imageView;
        View divider;
        ImageButton but;
    }    


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

                LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                appPrefs = new AppPreferences(context.getApplicationContext());     
                final ViewHolder holder;
                final UserModel s = (UserModel) values.get(position);

                if (convertView == null) {

                holder = new ViewHolder();
                convertView = inflater.inflate(R.layout.list_user_item, parent, false);
                holder.textView = (TextView) convertView.findViewById(R.id.label);
                holder.timeView = (TextView) convertView.findViewById(R.id.list_time);
                holder.imageView = (ImageView) convertView.findViewById(R.id.imageView1);
                holder.divider = (View) convertView.findViewById(R.id.side_divider);

               holder.but = (ImageButton) convertView.findViewById(R.id.details_list_button);
               convertView.setTag(holder);

                }
                else
                {holder = (ViewHolder) convertView.getTag();}

                holder.timeView.setText(s.getTimePosted());
                holder.textView.setText(s.getTitle());

                return convertView;

    }

这是一个有效的getView示例,供您查看

答案 1 :(得分:0)

如果没有logcat信息,它将更难说,但我只是猜测,你能否确保cr_room_name实际上在activity_discuss.xml文件中。如果它在activity_discuss布局文件中,那么只有在看到logcat之后我们才能找出可能存在的问题。

在看到你的堆栈跟踪之后,我脑海中只有一件事。请确保所有这些视图ID都在其相应的布局文件中。

正如我从您的布局和代码中看到的那样,您的Actvity_discuss.xml文件中没有place_name textview。

由于您使用不同的布局用于不同的目的,因此请确保在实际引用getView中的视图之前进行一些空检查,特别是在为特定视图设置侦听器时。

答案 2 :(得分:0)

似乎有点混乱:您要求在cr_room_name内找到editText1:listitem_messages.xml内,但它显然位于Actvity_discuss.xml内。

Activity activity = (Activity) getContext();
editText1 = (EditText) activity.findViewById(R.id.cr_room_name);

但是,即使这样可行,但是将视图和活动结合起来真的是一种丑陋的方式,你最好选择一种观察者可观的设计模式。

答案 3 :(得分:-1)

空点是一个很容易解决的问题,有时是因为未初始化对象,因此应进行调试以找出原因。很多时候该对象为空,从而导致问题