我想在所有拖动结束后刷新。但是,即使我有号码,我也会NullPointerException
。另外,我setContentView()
要求一个。我想我有不同的问题。 Fragment
的代码如下:
case DragEvent.ACTION_DRAG_ENDED:
System.out.println(String.valueOf(newGame.getPlayer(1).getHand().getHandSize()));
kyleNum.setText(String.valueOf(newGame.getPlayer(1).getHand().getHandSize()));
break;
我的崩溃日志是:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.example.com.sevens.GameActivity$MyDragListener.onDrag(GameActivity.java:413)
at android.view.View.dispatchDragEvent(View.java:19809)
at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1423)
at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1423)
at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1423)
at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1423)
at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1423)
at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1423)
at android.view.ViewRootImpl.handleDragEvent(ViewRootImpl.java:5488)
at android.view.ViewRootImpl.access$1000(ViewRootImpl.java:108)
at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3624)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5845)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
答案 0 :(得分:0)
如果您的TextView
生活在Fragment
内,请执行此操作以使代码正常工作:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
TextView tv = (TextView) rootView.findViewById(R.id.textView1);
tv.setText("Teststring");
return rootView;
}