Reputation: 187
Im making a multi activity application and the first two activities i made have worked fine with no force closes, but as soon as i added a third activity, one to register touches on the display, when its list item is clicked i get a force close.
I have done exactly the same as with my other activities but cant figure out at all what is wrong, all activities have also been included in the AndroidManifest file.
Main activity with 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:
}
}
The activity that causes the 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;
}
Information from LOGCAT:
01-02 16:08:05.292: W/dalvikvm(20683): threadid=1: thread exiting with uncaught exception (group=0x40a321f8) 01-02 16:08:05.302: E/AndroidRuntime(20683): FATAL EXCEPTION: main 01-02 16:08:05.302: E/AndroidRuntime(20683): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.reddicliffe.usefultools/com.reddicliffe.usefultools.Multitouch}: java.lang.InstantiationException: can't instantiate class com.reddicliffe.usefultools.Multitouch; no empty constructor 01-02 16:08:05.302: E/AndroidRuntime(20683): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880) 01-02 16:08:05.302: E/AndroidRuntime(20683): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 01-02 16:08:05.302: E/AndroidRuntime(20683): at android.app.ActivityThread.access$600(ActivityThread.java:123) 01-02 16:08:05.302: E/AndroidRuntime(20683): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 01-02 16:08:05.302: E/AndroidRuntime(20683): at android.os.Handler.dispatchMessage(Handler.java:99) 01-02 16:08:05.302: E/AndroidRuntime(20683): at android.os.Looper.loop(Looper.java:137) 01-02 16:08:05.302: E/AndroidRuntime(20683): at 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): Caused by: java.lang.InstantiationException: can't instantiate class com.reddicliffe.usefultools.Multitouch; no empty constructor 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): at android.app.Instrumentation.newActivity(Instrumentation.java:1023) 01-02 16:08:05.302: E/AndroidRuntime(20683): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871) 01-02 16:08:05.302: E/AndroidRuntime(20683): ... 11 more
Revised Multitouch code
public class Multitouch extends Activity {
public class MultitouchView extends View {
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Upvotes: 0
Views: 1065
Reputation: 37729
In short: in your case Multitouch
is extending a View
not an Activity
class.
if you want to show Multitouch
as an Activity's content.
use it as View
and add it inside Activity
's contentView
using layout from xml or simply call setContentView(anyMultitouchInstance);
Upvotes: 1