我正在制作一个多活动应用程序,我做的前两个活动没有强制关闭,但是一旦我添加了第三个活动,一个在显示器上注册触摸,当它的列表项被点击时我得到一股力量关闭。
我完成了与其他活动完全相同但无法弄清楚什么是错误的,所有活动也都包含在AndroidManifest文件中。
listview的主要活动:
public class MyCustomAdapter extends ArrayAdapter<String> {
public MyCustomAdapter(Context context, int textViewResourceId, String[] objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//return super.getView(position, convertView, parent);
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.row, parent, false);
TextView label = (TextView)row.findViewById(R.id.tool);
label.setText(Tool[position]);
ImageView icon = (ImageView)row.findViewById(R.id.icon);
if (Tool[position]=="Random # Generator") {
icon.setImageResource(R.drawable.generator);
}
else if (Tool[position]=="Converter"){
icon.setImageResource(R.drawable.converter);
}
else if (Tool[position]=="Ruler"){
icon.setImageResource(R.drawable.ruler);
}
else if (Tool[position]=="Stopwatch"){
icon.setImageResource(R.drawable.stopwatch);
}
else if (Tool[position]=="Countdown"){
icon.setImageResource(R.drawable.countdown);
}
else if (Tool[position]=="Multitouch"){
icon.setImageResource(R.drawable.touch);
}
return row;
}
}
String[] Tool = {"Stopwatch", "Countdown", "Multitouch", "Ruler", "Converter", "Random # Generator"};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
/*setListAdapter(new ArrayAdapter<String>(this, R.layout.row, R.id.weekofday, DayOfWeek));*/
setListAdapter(new MyCustomAdapter(this, R.layout.row, Tool));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
// super.onListItemClick(l, v, position, id);
switch(position) {
case 0:
Intent stopwatch = new Intent(UsefulToolsActivity.this, Stopwatch.class);
startActivity(stopwatch);
return;
case 1:
Intent countdown = new Intent(UsefulToolsActivity.this, CountdownActivity.class);
startActivity(countdown);
return;
case 2:
Intent multitouch = new Intent(UsefulToolsActivity.this, Multitouch.class);
startActivity(multitouch);
return;
default:
}
}
导致forceclose的活动:
public class Multitouch extends View {
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
final int MAX_NUMBER_OF_POINT = 10;
float[] x = new float[MAX_NUMBER_OF_POINT];
float[] y = new float[MAX_NUMBER_OF_POINT];
boolean[] touching = new boolean[MAX_NUMBER_OF_POINT];
public Multitouch(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public Multitouch(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public Multitouch(Context context) {
super(context);
init();
}
void init() {
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(1);
paint.setColor(Color.BLUE);
}
@Override
protected void onDraw(Canvas canvas) {
for(int i = 0; i < MAX_NUMBER_OF_POINT; i++) {
if(touching[i]){
canvas.drawCircle(x[i], y[i], 50f, paint);
}
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
MeasureSpec.getSize(heightMeasureSpec));
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = (event.getAction() & MotionEvent.ACTION_MASK);
int pointCount = event.getPointerCount();
for (int i = 0; i < pointCount; i++) {
int id = event.getPointerId(i);
//Ignore pointer higher than our max.
if(id < MAX_NUMBER_OF_POINT) {
x[id] = (int)event.getX(i);
y[id] = (int)event.getY(i);
if((action == MotionEvent.ACTION_DOWN)
||(action == MotionEvent.ACTION_POINTER_DOWN)
||(action == MotionEvent.ACTION_MOVE)) {
touching[id] = true;
}
else {
touching[id] = false;
}
}
}
invalidate();
return true;
}
来自LOGCAT的信息:
01-02 16:08:05.292:W / dalvikvm(20683):threadid = 1:线程退出,未捕获异常(group = 0x40a321f8) 01-02 16:08:05.302:E / AndroidRuntime(20683):致命异常:主要 01-02 16:08:05.302:E / AndroidRuntime(20683):java.lang.RuntimeException:无法实例化活动ComponentInfo {com.reddicliffe.usefultools / com.reddicliffe.usefultools.Multitouch}:java.lang.InstantiationException:can实例化类com.reddicliffe.usefultools.Multitouch;没有空的构造函数 01-02 16:08:05.302:E / AndroidRuntime(20683):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880) 01-02 16:08:05.302:E / AndroidRuntime(20683):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 01-02 16:08:05.302:E / AndroidRuntime(20683):在android.app.ActivityThread.access $ 600(ActivityThread.java:123) 01-02 16:08:05.302:E / AndroidRuntime(20683):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1147) 01-02 16:08:05.302:E / AndroidRuntime(20683):在android.os.Handler.dispatchMessage(Handler.java:99) 01-02 16:08:05.302:E / AndroidRuntime(20683):在android.os.Looper.loop(Looper.java:137) 01-02 16:08:05.302:E / AndroidRuntime(20683):在android.app.ActivityThread.main(ActivityThread.java:4424) 01-02 16:08:05.302:E / AndroidRuntime(20683):at java.lang.reflect.Method.invokeNative(Native Method) 01-02 16:08:05.302:E / AndroidRuntime(20683):at java.lang.reflect.Method.invoke(Method.java:511) 01-02 16:08:05.302:E / AndroidRuntime(20683):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784) 01-02 16:08:05.302:E / AndroidRuntime(20683):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 01-02 16:08:05.302:E / AndroidRuntime(20683):at dalvik.system.NativeStart.main(Native Method) 01-02 16:08:05.302:E / AndroidRuntime(20683):引起:java.lang.InstantiationException:无法实例化类com.reddicliffe.usefultools.Multitouch;没有空的构造函数 01-02 16:08:05.302:E / AndroidRuntime(20683):at java.lang.Class.newInstanceImpl(Native Method) 01-02 16:08:05.302:E / AndroidRuntime(20683):at java.lang.Class.newInstance(Class.java:1319) 01-02 16:08:05.302:E / AndroidRuntime(20683):在android.app.Instrumentation.newActivity(Instrumentation.java:1023) 01-02 16:08:05.302:E / AndroidRuntime(20683):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871) 01-02 16:08:05.302:E / AndroidRuntime(20683):... 11 more
修改了多点触控代码
public class Multitouch extends Activity {
public class MultitouchView extends View {
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
答案 0 :(得分:1)
简而言之:在您的情况下Multitouch
正在扩展View
而不是Activity
类。
如果您想将Multitouch
显示为活动的内容。
将其用作View
并使用xml中的布局将其添加到Activity
的{{1}}内,或者只需调用contentView