我正在尝试设置TextView
的文本,但每次尝试投射虚拟方法时,都会抛出NullPointerException
。如您所见,小部件存在,我正在使用正确的id创建TextView
变量。
<TextView
android:id="@+id/searchDispensaryName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignTop="@+id/productPicture"
android:layout_marginTop="13sp"
android:layout_marginStart="13dp"
android:text="TextView" />
TextView dispensaryName = (TextView) costumView.findViewById(R.id.searchDispensaryName);
以下是引发异常的地方:
dispensaryName.setText(object.get("dispensary_name").toString());
以下是例外文字:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.dragosandrei.greenkinexdorian.SearchCostumeAdapter.getView(SearchCostumeAdapter.java:47)
at android.widget.AbsListView.obtainView(AbsListView.java:2411)
at android.widget.ListView.makeAndAddView(ListView.java:1986)
at android.widget.ListView.fillDown(ListView.java:709)
at android.widget.ListView.fillFromTop(ListView.java:770)
at android.widget.ListView.layoutChildren(ListView.java:1749)
at android.widget.AbsListView.onLayout(AbsListView.java:2210)
at android.view.View.layout(View.java:17688)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
at android.view.View.layout(View.java:17688)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
at android.view.View.layout(View.java:17688)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
at android.view.View.layout(View.java:17688)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1193)
at android.view.View.layout(View.java:17688)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:325)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17688)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1762)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1606)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1515)
at android.view.View.layout(View.java:17688)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:325)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17688)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1762)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1606)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1515)
at android.view.View.layout(View.java:17688)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:325)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:774)
at android.view.View.layout(View.java:17688)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2513)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2228)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1366)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6768)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:926)
at android.view.Choreographer.doCallbacks(Choreographer.java:735)
at android.view.Choreographer.doFrame(Choreographer.java:667)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:912)
at android.os.Handler.handleCallback(Handler.java:761)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
实际上没有理由抛出这个异常!你能告诉我我错在哪里吗?
答案 0 :(得分:2)
costumView.findViewById你确定它是如何工作的? 尝试使用不带costumView的findViewById
TextView dispensaryName = (TextView) findViewById(R.id.searchDispensaryName);