【问题标题】:Can not figure out this NullPoint Exception无法弄清楚这个 NullPoint 异常
【发布时间】:2013-11-16 17:40:56
【问题描述】:

我正在向适配器中的文本字段添加一个 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)

活动讨论.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>

【问题讨论】:

  • 也许editText没有将id传递给findViewById,记录它的值或者只是检查xml上的值
  • 如果您阅读过任何与此类似的现有问题,您会发现第一条评论几乎总是相同的。请从 logcat 发布堆栈跟踪。
  • 这个类型从何而来?
  • 我猜你想要类似于stackoverflow.com/questions/18868194/… 的东西。为行项目膨胀不同的布局
  • XML 和堆栈跟踪。从主 Activity 传入以使用 Tab 进行跟踪的“类型”当前正在应用中显示。

标签: android nullpointerexception textview onclicklistener onlongclicklistener


【解决方案1】:

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

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

但即使这样可行,它确实是一种将视图和活动耦合起来的丑陋方式,您最好采用观察者可观察的设计模式。

【讨论】:

  • 这行得通,但你能告诉我更多关于更好的解决方案的信息吗?谢谢!
  • 视图不应依赖于活动。您不希望按钮在活动 A 中以某种方式表现,而在活动 B 中表现不同。按钮将始终是按钮。同样在这里,您的视图不应要求您的活动属于 A 类。有两种解决方案为此:要么您实现 obersvable - 观察者设计模式(请用谷歌搜索),或者您可以使用总线(如 otto 或 eventbus 或 roboguice)。但是,请大家保持距离,如果您刚开始使用 Android,请不要担心我的评论,继续前进,我要说的是更高级的 Android 设计。
【解决方案2】:
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 的示例供您查看

【讨论】:

    【解决方案3】:

    如果没有 logcat 信息,这将更难判断,但我只是在猜测,您能否确保 cr_room_name 实际上在 activity_discuss.xml 文件中。如果是在activity_discuss布局文件中,那么只有在看到logcat之后我们才能弄清楚可能是什么问题。

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

    从您的布局和代码中我可以看出,您的 Actvity_discuss.xml 文件中没有 place_name 文本视图。

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

    【讨论】:

    • place_name 在 R.layout.listitem_discuss 中,它也被夸大了。
    • getView 将被调用尽可能多的行。这意味着 userName.setOnClickListener(new View.OnClickListener() { 将为每个 getView 调用调用。虽然您在 R.layout.listitem_discuss 中有 place_name 但对于其他布局?
    • 我想你已经知道这里实际发生了什么。虽然 getView 会在 R.layout.listitem_discuss 中找到 userName 但当涉及到其他布局时,如 Actvity_discuss 它将无法找到它。因此空指针异常。
    • 当我看到您的代码时,我认为您拥有的每个视图都可能发生异常。如果您不愿意为自定义行进行大量 UI 更改,我认为空引用检查是可行的方法。
    【解决方案4】:

    Nullpoint 是一个很容易解决的问题,有时是因为一个对象没有初始化,你应该调试找出原因。很多时候对象为空,导致问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多