0

I am adding an onClick listener to a text field in my adapter. I simply want the text in that field to me posted to my Text input box (edittext1). I am trying the code below however it s giving me a NullPoint Exception at the line pointng to edttext1. Help please

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

            }
            });

I would actually prefer to make it a Longclick. Is there an difference in the setup.

Stack trace

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>
TWeeKeD
  • 119
  • 1
  • 2
  • 16
  • Maybe the editText hasn't the id passed to findViewById, log its value or simply check the value on xml – dafi Nov 16 '13 at 17:42
  • If you had read any existing questions similar to this, you would see that the first comment is nearly always the same. Please post the stack trace from logcat. – Simon Nov 16 '13 at 17:43
  • where does this **type** come from? – waqaslam Nov 16 '13 at 17:45
  • i guess you are want something similar to http://stackoverflow.com/questions/18868194/android-xml-layout-for-a-listview-with-different-items. inflate different layout for row items – Raghunandan Nov 16 '13 at 17:47
  • XML and stacktrace. "type" s passed in from the main activity to track with Tab is currently being shown in the app. – TWeeKeD Nov 16 '13 at 17:55
  • The logcat tells you that the error is occurring at DiscussArrayAdapter.java:129. Could you show that code please and highlight the light please? – Neil Townsend Nov 16 '13 at 18:15

4 Answers4

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;

    }

Here's an example of a working getView for you to look over

Broak
  • 4,161
  • 4
  • 31
  • 54
0

Without logcat information it will be harder to tell but i am just guessing, can you make sure cr_room_name is actually in the activity_discuss.xml file. If it is in the activity_discuss layout file, then only after seeing the logcat we can figure out what might be the problem.

After seeing you stacktrace there is only one thing coming to my mind. Please make sure all of those view id's are in it's respective layout file.

As i can see from your layout and code, there is no place_name textview in your Actvity_discuss.xml file.

Since you are using different layout for different purposes make sure you do some null checking before actually referencing the view from getView and specially when you are setting a listener for a particular view.

saiful103a
  • 1,109
  • 7
  • 13
  • place_name is in R.layout.listitem_discuss which is also inflated. – TWeeKeD Nov 16 '13 at 18:03
  • getView will be called as many row as you have. That means userName.setOnClickListener(new View.OnClickListener() { will be called for every getView call. Though you have place_name in R.layout.listitem_discuss but for other layout? – saiful103a Nov 16 '13 at 18:07
  • I think you have got the idea what's actually happening here.Though getView will find userName in R.layout.listitem_discuss but when it comes to other layout like Actvity_discuss it will not be able to find it.Thus nullpointerexception. – saiful103a Nov 16 '13 at 18:11
  • As i am seeing your code i think exception might happen for every view you have. If you are not willing to do whole lot of UI change for your custom row i think null reference checking is the way to go. – saiful103a Nov 16 '13 at 18:24
0

There seem to be a little confusion : you ask to find editText1 under the id cr_room_name : inside listitem_messages.xml, but it is apprently inside Actvity_discuss.xml.

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

But even if this works, it's really an ugly way to couple a view and an activity, you should better go for an observer-obersable design pattern.

Snicolas
  • 37,840
  • 15
  • 114
  • 173
  • this worked but can you tell me more about the better solution. Thank you! – TWeeKeD Nov 16 '13 at 21:43
  • A view should not be activity-dependant. You don't expect a button to behave in some way in activity A and differently in activity B. A button will always be a button.. Same here, your view should not require your activity to be of class A. There are 2 solutions for that : either you implement the obersvable - observer design pattern (please google that), or you could use a bus (like otto or eventbus or roboguice). But please, take all this with a bit of distance, if you just start Android, don't worry about my comment, and go ahead, what I am talking about is more advanced in Android design. – Snicolas Nov 17 '13 at 07:03
-1

Nullpoint is an easy problem to solve, sometimes because an object is not initialized, and you should debug to find out why. Many times the object is null, causing problems

jiawei.guo
  • 21
  • 1