سؤال

أقوم بإضافة مستمع onClick إلى حقل نص في المحول الخاص بي.أريد ببساطة أن يتم نشر النص الموجود في هذا الحقل في مربع إدخال النص الخاص بي (تحرير النص 1).أحاول استخدام الكود أدناه ولكنه يعطيني استثناء NullPoint عند السطر الذي يشير إلى edttext1.ساعدنى من فضلك

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>
هل كانت مفيدة؟

المحلول

يبدو أن هناك القليل من الارتباك:تطلب العثور على EditText1 ضمن المعرف cr_room_name :داخل listitem_messages.xml, ، ولكن من الواضح في الداخل Actvity_discuss.xml.

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

ولكن حتى لو نجح هذا، فهي حقًا طريقة قبيحة للربط بين العرض والنشاط، فمن الأفضل أن تختار نمط تصميم يمكن ملاحظته بواسطة المراقب.

نصائح أخرى

giveacodicetagpre.

إليك مثال على وظيفة GetView لكي تنظر إليه

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.

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top