Вопрос

I'm trying to do a drag and drop but the app crashes when i try to open up the screen. i'm not sure where the problem is but my log cat points me to this code

ImageView one = (ImageView) findViewById(R.id.one5);
TextView two = (TextView) findViewById(R.id.one6);
one.setOnLongClickListener(longlisten);
two.setOnLongClickListener(longlisten);

but there isn't anything wrong with that . the main body of the class is below along with the log cat. i just want an answer as to why this doesn't work even it is just a stupid mistake

@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 layout

<?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
Это было полезно?

Решение

You need to make a call to setContentView(your_layout.xml) before you try making references to views in it for e.g. TextView two = (TextView) findViewById(R.id.one6);

Since you make a call to setContentView later on, the views one and two are null. And hence cause an exception when you set the listeners.

EDIT:

Modifying your code a bit

@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);
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top