我正在构建一个使用按钮显示文本的应用程序,然后在某个时刻,相同的按钮将显示图像。
按钮以编程方式创建,具有不同的大小和位置。 我使用了相对布局按钮。
我需要拖放按钮。 我找到了一个管教程,向您展示如何制作可以拖放的文本视图。我完成了第一部分,可以让它拖动。我也让它适用于一个按钮。
当我将此代码添加到我的应用程序时 - 使用相对布局中的按钮它不起作用 - 应用程序崩溃。
下面是我的代码,我已经标记了导致应用程序崩溃的行。
package com.example.button;
import com.example.dragproject.MainActivity.DragShadow;
import android.os.Build;
import android.os.Bundle;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.ClipData;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.DragEvent;
import android.view.Gravity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
RelativeLayout layout;
Button newButton;
Button buttonByXml;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonByXml = (Button)findViewById(R.id.button1);
layout = (RelativeLayout)findViewById(R.id.RL);
buttonByXml.setVisibility(View.GONE);
button(500,300, (float) 230.2,(float) 230.2, 1, "untitled");
button(200,400, (float) 430.2,(float) 630.2, 2, "untitled1");
button(250,150, (float) 130.2,(float) 730.2, 3, "untitled2");
button(250,150, (float) 330.2,(float) 30.2, 3);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void button(int x, int y, float xx, float yy, int devno, String img) {
String a = Integer.toString(devno);
newButton = new Button(this);
newButton.setOnLongClickListener((OnLongClickListener) this);//PROBLEM
layout.addView(newButton);
newButton.setX((float) xx);
newButton.setY((float) yy);
newButton.getLayoutParams().width = x;
newButton.getLayoutParams().height = y;
String mDrawableName = img;
int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());
newButton.setBackgroundResource(resID);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void button(int x, int y, float xx, float yy, int devno) {
String a = Integer.toString(devno);
newButton = new Button(this);
newButton.setOnLongClickListener((OnLongClickListener) this);//PROBLEM
layout.addView(newButton);
newButton.setX((float) xx);
newButton.setY((float) yy);
newButton.getLayoutParams().width = x;
newButton.getLayoutParams().height = y;
newButton.setText(a);
}
OnLongClickListener Click = new OnLongClickListener(){
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public boolean onLongClick(View v) {
DragShadow dragshadow = new DragShadow(v);
ClipData data = ClipData.newPlainText("", "");
v.startDrag(data, dragshadow, v, 0);
return false;
}
};
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private class DragShadow extends View.DragShadowBuilder{
ColorDrawable greyBox;
public DragShadow(View view){
super(view);
greyBox = new ColorDrawable(Color.LTGRAY);
}
@Override
public void onDrawShadow(Canvas canvas){
greyBox.draw(canvas);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint){
View v = getView();
int height = (int)v.getHeight();
int width = (int)v.getWidth();
greyBox.setBounds(0,0,width,height);
shadowSize.set(width, height);
shadowTouchPoint.set((int)width/2, (int)height/2);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这是日志猫:
01-19 09:06:31.949: E/AndroidRuntime(19928): FATAL EXCEPTION: main
01-19 09:06:31.949: E/AndroidRuntime(19928): Process: com.example.button, PID: 19928
01-19 09:06:31.949: E/AndroidRuntime(19928): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.button/com.example.button.MainActivity}: java.lang.ClassCastException: com.example.button.MainActivity cannot be cast to android.view.View$OnLongClickListener
01-19 09:06:31.949: E/AndroidRuntime(19928): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
01-19 09:06:31.949: E/AndroidRuntime(19928): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
01-19 09:06:31.949: E/AndroidRuntime(19928): at android.app.ActivityThread.access$700(ActivityThread.java:135)
01-19 09:06:31.949: E/AndroidRuntime(19928): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
01-19 09:06:31.949: E/AndroidRuntime(19928): at android.os.Handler.dispatchMessage(Handler.java:102)
01-19 09:06:31.949: E/AndroidRuntime(19928): at android.os.Looper.loop(Looper.java:137)
01-19 09:06:31.949: E/AndroidRuntime(19928): at android.app.ActivityThread.main(ActivityThread.java:4998)
01-19 09:06:31.949: E/AndroidRuntime(19928): at java.lang.reflect.Method.invokeNative(Native Method)
01-19 09:06:31.949: E/AndroidRuntime(19928): at java.lang.reflect.Method.invoke(Method.java:515)
01-19 09:06:31.949: E/AndroidRuntime(19928): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
01-19 09:06:31.949: E/AndroidRuntime(19928): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
01-19 09:06:31.949: E/AndroidRuntime(19928): at dalvik.system.NativeStart.main(Native Method)
01-19 09:06:31.949: E/AndroidRuntime(19928): Caused by: java.lang.ClassCastException: com.example.button.MainActivity cannot be cast to android.view.View$OnLongClickListener
01-19 09:06:31.949: E/AndroidRuntime(19928): at com.example.button.MainActivity.button(MainActivity.java:57)
01-19 09:06:31.949: E/AndroidRuntime(19928): at com.example.button.MainActivity.onCreate(MainActivity.java:44)
01-19 09:06:31.949: E/AndroidRuntime(19928): at android.app.Activity.performCreate(Activity.java:5243)
01-19 09:06:31.949: E/AndroidRuntime(19928): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-19 09:06:31.949: E/AndroidRuntime(19928): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
01-19 09:06:31.949: E/AndroidRuntime(19928): ... 11 more
答案 0 :(得分:0)
newButton.setOnLongClickListener((OnLongClickListener) this);//PROBLEM
this
是您的活动,而不是OnLongClickListener
,转换失败。如果您的MainActivity implements onLongClickListener
,它将起作用(即使没有演员表)。
您在此处初始化onLongClickListener:
OnLongClickListener Click = new OnLongClickListener(){
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public boolean onLongClick(View v) {
DragShadow dragshadow = new DragShadow(v);
ClipData data = ClipData.newPlainText("", "");
v.startDrag(data, dragshadow, v, 0);
return false;
}
};
所以使用它:
newButton.setOnLongClickListener(Click);
为了样式,您应该将初始化移动到其他位置(类的开头,而不是在方法之间的中间)并使用小写名称。