即時製作一個多活動應用程序和前兩個活動我做了很好的沒有強制關閉,但只要我添加第三個活動,一個註冊觸摸顯示,當它的列表項被點擊時,我會得到一個關閉的力量。無法獲得意圖與另一活動正常工作
我已經完成了和我的其他活動完全一樣的工作,但不知道哪裏出了什麼問題,所有活動也都包含在AndroidManifest文件中。
與列表視圖主要活動:
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/Androi dRuntime(20683):致命異常:主 01-02 16:08:05.302:E/AndroidRuntime(20683):了java.lang.RuntimeException:無法實例活動ComponentInfo {com.reddicliffe.usefultools/com.reddicliffe.usefultools。 Multitouch}:java.lang.InstantiationException:無法實例化類com.reddicliffe.usefultools.Multitouch;沒有空構造函數 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):在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):在android.app.ActivityThread.main(ActivityThread的.java:4424) 01-02 16:08:05.302:E/AndroidRuntime(20683):在java.lang.reflect.Method.invokeNative(本機方法) 01-02 16:08:05.302:E/AndroidRuntime( 20683):在java.lang.reflect.Method.invoke(Method.java:511) 01-02 16:08:05.302:E/AndroidRuntime(20683):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller。運行(ZygoteInit.java:784) 01-02 16:08:05.302:E/AndroidRuntime(20683):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 01-02 16: 08:05.302:E/AndroidRuntime(20683):在dalvik.system.NativeStart.main(本機方法) 01-02 16:08:05.302:E/AndroidRuntime(20683):引起:java.lang.InstantiationException:can實例化類com.reddicliffe.usefultools.Multitouch;沒有空的構造 01-02 16:08:05.302:E/AndroidRuntime(20683):在java.lang.Class.newInstanceImpl(本機方法) 01-02 16:08:05.302:E/AndroidRuntime(20683):在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):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871) 01-02 16:08:05.302:E/AndroidRuntime(20683):...11多
修多點觸控碼
public class Multitouch extends Activity {
public class MultitouchView extends View {
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
我如何能得到這個工作呢?不太確定 – 2012-01-02 16:23:30
好吧,我得到它停止forceclosing,但觸摸代碼似乎並沒有工作。我會補充到目前爲止我所掌握的信息 – 2012-01-02 16:28:48