我正在尝试拖放但是当我尝试打开屏幕时应用程序崩溃了。我不确定问题出在哪里,但我的日志猫指向我这个代码
ImageView one = (ImageView) findViewById(R.id.one5);
TextView two = (TextView) findViewById(R.id.one6);
one.setOnLongClickListener(longlisten);
two.setOnLongClickListener(longlisten);
但是没有任何问题。这个班级的主体和日志猫一起在下面。我只是想要一个答案,为什么这不起作用,即使这只是一个愚蠢的错误
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ImageView one = (ImageView) findViewById(R.id.one5);
TextView two = (TextView) findViewById(R.id.one6);
one.setOnLongClickListener(longlisten);
two.setOnLongClickListener(longlisten);
setContentView(R.layout.quiz);
}
OnLongClickListener longlisten = new OnLongClickListener()
{
@Override
public boolean onLongClick(View v)
{
TextView button = (TextView) v;
DragShadow dragShadow = new DragShadow(v);
ClipData data = ClipData.newPlainText("", "");
v.startDrag(data, dragShadow, button, 0);
return false;
}
};
private class DragShadow extends View.DragShadowBuilder
{
private ColorDrawable grey;
public DragShadow(View view) {
super(view);
grey = new ColorDrawable(Color.LTGRAY);
}
@Override
public void onDrawShadow(Canvas canvas) {
grey.draw(canvas);
}
@Override
public void onProvideShadowMetrics(Point shadowSize,Point shadowTouchPoint)
{
View v = getView();
int height =(int) v.getHeight()/2;
int width = (int) v.getWidth()/2;
grey.setBounds(0,0, width, height);
shadowSize.set(width, height);
shadowTouchPoint.set((int)width/2, (int)height/2);
}
};
OnDragListener dragListener = new OnDragListener()
{
@Override
public boolean onDrag(View v, DragEvent event)
{
int dragEvent = event.getAction();
TextView dropText = (TextView) v;
switch(dragEvent)
{
case DragEvent.ACTION_DRAG_ENTERED:
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DROP:
TextView draggedText = (TextView)event.getLocalState();
dropText.setText(draggedText.getText());
break;
}
return true;
}
};
XML布局
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:id="@+id/one5"
android:background="@drawable/button"
android:layout_x="26dp"
android:layout_y="248dp"
android:layout_width="100dp"
android:layout_height="50dp"/>
<TextView
android:id="@+id/Text1"
android:layout_width="212dp"
android:layout_height="91dp"
android:layout_x="43dp"
android:layout_y="88dp"
android:ems="10"
android:text="@string/ex1"
android:textSize="50sp" />
<TextView
android:id="@+id/one6"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_x="178dp"
android:layout_y="251dp"
android:background="@drawable/button" />
logcat的
03-12 16:50:44.919: W/dalvikvm(4353): threadid=1: thread exiting with uncaught exception (group=0x40ebd450)
03-12 16:50:44.919: E/AndroidRuntime(4353): FATAL EXCEPTION: main
03-12 16:50:44.919: E/AndroidRuntime(4353): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.timestables/com.example.timestables.Quiz}: java.lang.NullPointerException
03-12 16:50:44.919: E/AndroidRuntime(4353): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2071)
03-12 16:50:44.919: E/AndroidRuntime(4353): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096)
03-12 16:50:44.919: E/AndroidRuntime(4353): at android.app.ActivityThread.access$600(ActivityThread.java:138)
03-12 16:50:44.919: E/AndroidRuntime(4353): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
03-12 16:50:44.919: E/AndroidRuntime(4353): at android.os.Handler.dispatchMessage(Handler.java:99)
03-12 16:50:44.919: E/AndroidRuntime(4353): at android.os.Looper.loop(Looper.java:213)
03-12 16:50:44.919: E/AndroidRuntime(4353): at android.app.ActivityThread.main(ActivityThread.java:4787)
03-12 16:50:44.919: E/AndroidRuntime(4353): at java.lang.reflect.Method.invokeNative(Native Method)
03-12 16:50:44.919: E/AndroidRuntime(4353): at java.lang.reflect.Method.invoke(Method.java:511)
03-12 16:50:44.919: E/AndroidRuntime(4353): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
03-12 16:50:44.919: E/AndroidRuntime(4353): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
03-12 16:50:44.919: E/AndroidRuntime(4353): at dalvik.system.NativeStart.main(Native Method)
03-12 16:50:44.919: E/AndroidRuntime(4353): Caused by: java.lang.NullPointerException
03-12 16:50:44.919: E/AndroidRuntime(4353): at com.example.timestables.Quiz.onCreate(Quiz.java:30)
03-12 16:50:44.919: E/AndroidRuntime(4353): at android.app.Activity.performCreate(Activity.java:5008)
03-12 16:50:44.919: E/AndroidRuntime(4353): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-12 16:50:44.919: E/AndroidRuntime(4353): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2035)
03-12 16:50:44.919: E/AndroidRuntime(4353): ... 11 more
答案 0 :(得分:1)
在尝试引用其中的观看次数之前,您需要拨打setContentView(your_layout.xml)
的电话。 TextView two = (TextView) findViewById(R.id.one6);
由于您稍后调用setContentView,因此视图one
和two
为空。因此,在设置侦听器时会导致异常。
修改强>
稍微修改你的代码
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz);
ImageView one = (ImageView) findViewById(R.id.one5);
TextView two = (TextView) findViewById(R.id.one6);
one.setOnLongClickListener(longlisten);
two.setOnLongClickListener(longlisten);
}