0
子の親に)removeView(呼び出す必要があります:java.lang.IllegalStateException:指定された子にはすでに親があります。あなたがここに私のコードだ最初
public class RT extends Activity implements OnTouchListener{
DrawPanel dp;
private ArrayList<Path> pointsToDraw = new ArrayList<Path>();
private Paint mPaint;
Path path;
private int strokeNumber = 0;
double strokeLength = 0.0;
PathMeasure p;
double s1 = 0.0,s2 = 0.0 ,s3 = 0.0,s4 = 0.0;
String st1,st2,st3,st4;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
dp = new DrawPanel(this);
dp.setOnTouchListener(this);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
mPaint = new Paint();
mPaint.setDither(true);
mPaint.setColor(Color.WHITE);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(30);
final TextView tv1 = new TextView(this);
tv1.setText("Generated Text Goes Here");
tv1.setTextSize(30);
tv1.setTextColor(Color.WHITE);
tv1.setGravity(Gravity.BOTTOM);
tv1.setId(R.id.TGText);
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Button b1 = new Button(this);
b1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
b1.setTextSize(20);
b1.setText("Generate");
b1.setTextColor(Color.WHITE);
b1.setId(R.id.TGButton);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
p = new PathMeasure(path,false);
strokeLength = p.getLength();
tv1.setText("Length of stroke: " + Double.toString(strokeLength) + "stroke number = " + strokeNumber);
if(strokeNumber == 1){
s1 = strokeLength;
if(s1 < 150.0)
st1 = "S";
if(s1 > 150.0 && s1 < 250.0)
st1 = "M";
if(s1 > 250.0 && s1 < 350.0)
st1 = "L";
if(s1 > 350.0)
st1 = "VL";
}
if(strokeNumber == 2){
s2 = strokeLength;
if(s2 < 150.0)
st2 = "S";
if(s2 > 150.0 && s2 < 250.0)
st2 = "M";
if(s2 > 250.0 && s2 < 350.0)
st2 = "L";
if(s2 > 350.0)
st2 = "VL";
}
if(strokeNumber == 3){
s3 = strokeLength;
if(s3< 150.0)
st3 = "S";
if(s3 > 150.0 && s3 < 250.0)
st3 = "M";
if(s3 > 250.0 && s3 < 350.0)
st3 = "L";
if(s3 > 350.0)
st3 = "VL";
}
if(strokeNumber == 4){
s4 = strokeLength;
if(s4 < 150.0)
st4 = "S";
if(s4 > 150.0 && s4 < 250.0)
st4 = "M";
if(s4 > 250.0 && s4 < 350.0)
st4 = "L";
if(s4 > 350.0)
st4 = "VL";
}
}
});
ll.addView(b1);
Button b2 = new Button(this);
b2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
b2.setTextSize(20);
b2.setText("Back");
b2.setTextColor(Color.WHITE);
b2.setId(R.id.TBButton);
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
ll.addView(b2);
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Match to?");
alert.setMessage("Match this to which alphabet?");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();
//insertValue(value,st1,st2,st3,st4);
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
Button b3 = new Button(this);
b3.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
b3.setTextSize(20);
b3.setText("Store");
b3.setTextColor(Color.WHITE);
b3.setId(R.id.TSButton);
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
alert.show();
dp.clearCanvas();
tv1.setText(st1 + " " + st2 + " " + st3 + " " + st4);
s1 = 0.0f;
s2 = 0.0f;
s3 = 0.0f;
s4 = 0.0f;
}
});
ll.addView(b3);
Button b4 = new Button(this);
b4.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
b4.setTextSize(20);
b4.setText("Clear");
b4.setTextColor(Color.WHITE);
b4.setId(R.id.TCButton);
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dp.clearCanvas();
tv1.setText("");
}
});
ll.addView(b4);
FrameLayout fl = new FrameLayout(this);
fl.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
fl.addView(dp);
fl.addView(tv1);
fl.addView(ll);
setContentView(fl);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
dp.pause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
dp.resume();
}
private void insertValue(String alphabet, String stroke1,String stroke2,String stroke3,String stroke4) {
DatabaseHelper databaseHelper = new DatabaseHelper(this);
SQLiteDatabase db = databaseHelper.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(DatabaseHelper.Alpha, alphabet);
cv.put(DatabaseHelper.s1, stroke1);
cv.put(DatabaseHelper.s2, stroke2);
cv.put(DatabaseHelper.s3, stroke3);
cv.put(DatabaseHelper.s4, stroke4);
db.insert("capitals", null, cv);
db.close();
}
public class DrawPanel extends SurfaceView implements Runnable{
Thread t = null;
SurfaceHolder holder;
boolean isItOk = false ;
public DrawPanel(Context context) {
super(context);
// TODO Auto-generated constructor stub
holder = getHolder();
}
@Override
public void run() {
// TODO Auto-generated method stub
while(isItOk == true){
if(!holder.getSurface().isValid()){
continue;
}
Canvas c = holder.lockCanvas();
c.drawARGB(255, 0, 0, 0);
onDraw(c);
holder.unlockCanvasAndPost(c);
}
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
synchronized(pointsToDraw)
{
for (Path path : pointsToDraw) {
canvas.drawPath(path, mPaint);
}
}
}
public void pause(){
isItOk = false;
while(true){
try{
t.join();
}catch(InterruptedException e){
e.printStackTrace();
}
break;
}
t = null;
}
public void resume(){
isItOk = true;
t = new Thread(this);
t.start();
}
public void clearCanvas(){
Canvas c = holder.lockCanvas();
c.drawColor(Color.BLACK);
pointsToDraw.clear();
holder.unlockCanvasAndPost(c);
strokeNumber = 0;
}
}
@Override
public boolean onTouch(View v, MotionEvent me) {
synchronized(pointsToDraw)
{
// TODO Auto-generated method stub
if(me.getAction() == MotionEvent.ACTION_DOWN){
path = new Path();
path.moveTo(me.getX(), me.getY());
//path.lineTo(me.getX(), me.getY());
pointsToDraw.add(path);
strokeNumber++;
}else if(me.getAction() == MotionEvent.ACTION_MOVE){
path.lineTo(me.getX(), me.getY());
}else if(me.getAction() == MotionEvent.ACTION_UP){
//path.lineTo(me.getX(), me.getY());
}
}
return true;
}
@Override
public void onBackPressed() {
return;
}
}
ここでログの内容です:
03-19 19:48:19.254: E/AndroidRuntime(1185): FATAL EXCEPTION: main
03-19 19:48:19.254: E/AndroidRuntime(1185): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
03-19 19:48:19.254: E/AndroidRuntime(1185): at android.view.ViewGroup.addViewInner(ViewGroup.java:3337)
03-19 19:48:19.254: E/AndroidRuntime(1185): at android.view.ViewGroup.addView(ViewGroup.java:3208)
03-19 19:48:19.254: E/AndroidRuntime(1185): at android.view.ViewGroup.addView(ViewGroup.java:3188)
03-19 19:48:19.254: E/AndroidRuntime(1185): at com.android.internal.app.AlertController.setupView(AlertController.java:401)
03-19 19:48:19.254: E/AndroidRuntime(1185): at com.android.internal.app.AlertController.installContent(AlertController.java:241)
03-19 19:48:19.254: E/AndroidRuntime(1185): at android.app.AlertDialog.onCreate(AlertDialog.java:336)
03-19 19:48:19.254: E/AndroidRuntime(1185): at android.app.Dialog.dispatchOnCreate(Dialog.java:353)
03-19 19:48:19.254: E/AndroidRuntime(1185): at android.app.Dialog.show(Dialog.java:257)
03-19 19:48:19.254: E/AndroidRuntime(1185): at android.app.AlertDialog$Builder.show(AlertDialog.java:932)
03-19 19:48:19.254: E/AndroidRuntime(1185): at learn.myandroidapp.hr.HandwritingRecognitionTrainer$5.onClick(HandwritingRecognitionTrainer.java:200)
03-19 19:48:19.254: E/AndroidRuntime(1185): at android.view.View.performClick(View.java:3511)
03-19 19:48:19.254: E/AndroidRuntime(1185): at android.view.View$PerformClick.run(View.java:14105)
03-19 19:48:19.254: E/AndroidRuntime(1185): at android.os.Handler.handleCallback(Handler.java:605)
03-19 19:48:19.254: E/AndroidRuntime(1185): at android.os.Handler.dispatchMessage(Handler.java:92)
03-19 19:48:19.254: E/AndroidRuntime(1185): at android.os.Looper.loop(Looper.java:137)
03-19 19:48:19.254: E/AndroidRuntime(1185): at android.app.ActivityThread.main(ActivityThread.java:4424)
03-19 19:48:19.254: E/AndroidRuntime(1185): at java.lang.reflect.Method.invokeNative(Native Method)
03-19 19:48:19.254: E/AndroidRuntime(1185): at java.lang.reflect.Method.invoke(Method.java:511)
03-19 19:48:19.254: E/AndroidRuntime(1185): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-19 19:48:19.254: E/AndroidRuntime(1185): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-19 19:48:19.254: E/AndroidRuntime(1185): at dalvik.system.NativeStart.main(Native Method)
を編集して例外がどこから来ている。これは、あるライン200を強調して、コメントを追加してください。基本的には、親ビューに子ビューを追加しようとしているにも関わらず、親ビューは既に存在しています。おそらく、親を別の親に追加するか、最初に子に対してremoveViewを呼び出すかのどちらかです。 – FoamyGuy
@Tim:エラーはalert.show()からのもので、スタックトレースの残りの部分が示すとおりです – njzk2