안드로이드 개발 질문/답변
(글 수 45,052)
[소스 일부]
public class TCPsocketTest extends Activity implements OnClickListener{
private String return_msg;
Button send, connect;
EditText ip, port, et;
TextView tv;
Context context;
View layout;
private String server_ip;
private Integer server_port;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
send = (Button)findViewById(R.id.send);
connect = (Button)findViewById(R.id.connect);
et = (EditText)findViewById(R.id.editText1);
tv = (TextView)findViewById(R.id.textView1);
context = getApplicationContext();
LayoutInflater inflate = (LayoutInflater)context.getSystemService(LAYOUT_INFLATER_SERVICE);
layout = inflate.inflate(R.layout.ipsetting, null);
ip = (EditText)layout.findViewById(R.id.editIp);
port = (EditText)layout.findViewById(R.id.editPort);
send.setOnClickListener(this);
connect.setOnClickListener(this);
} //onCreate
@Override
public void onClick(View v) {
AlertDialog.Builder aDialog = new AlertDialog.Builder(this);
aDialog.setTitle("서버 접속");
aDialog.setView(layout);
if(v.getId()==R.id.send){
if(et.getText().toString() != null || !et.getText().toString().equals("")){
TCPclient tp = new TCPclient(et.getText().toString());
tp.run();
Toast t = Toast.makeText(getApplicationContext(), return_msg, Toast.LENGTH_LONG);
t.show();
tv.setText(return_msg);
}
}
else if(v.getId()==R.id.connect){
aDialog.setPositiveButton("접속", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
if(ip.getText().toString().equals("") || port.getText().toString().equals("")){
server_ip = "0.0.0.0";
server_port = 0000;
Toast pop = Toast.makeText(getApplicationContext(), "입력을 하지 않으셨습니다", 1500);
pop.show();
}
else{
server_ip = ip.getText().toString();
server_port = Integer.parseInt(""+port.getText());
}
}
});
aDialog.setNegativeButton("취소", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
AlertDialog ad = aDialog.create();
ad.show();
}
else{
}
}
[main xml]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<EditText
android:id="@+id/editText1"
android:hint="내용을 입력하세요"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:id="@+id/send"
android:layout_height="wrap_content"
android:text="Send"/>
<Button
android:layout_width="wrap_content"
android:id="@+id/connect"
android:layout_height="wrap_content"
android:text="설정"/>
</LinearLayout>
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
[ipsetting xml]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:text="IP : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/editIp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:text="Port : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/editPort"
android:inputType="number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
위 소스는 자바로 구동되는 서버와 소켓 통신하는 프로그램입니다.
소켓 통신 부분은 제외 했구요..
질문하고자 하는바는..
어플을 실행했을때 설정 버튼을 눌르면 AlertDialog 창이 뜨고 ip세팅과 port세팅 하고 서버와 통신하면 잘 이루어집니다.
그런데 설정버튼을 한번 더 누르게 되면 예상치 못한 오류로 어플을 강제 종료 하게 되는데..
->>>>>> 어플시작 - 설정버튼 클릭 - 접속이나 취소 버튼 누름 - 설정버튼 한번 더 클릭 - 강제종료...!!!! (도대체 왜???ㅠㅠ)
도대체 왜 그런지 알 수가 없어서요.. 도움좀 주세요~
public class TCPsocketTest extends Activity implements OnClickListener{
private String return_msg;
Button send, connect;
EditText ip, port, et;
TextView tv;
Context context;
View layout;
private String server_ip;
private Integer server_port;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
send = (Button)findViewById(R.id.send);
connect = (Button)findViewById(R.id.connect);
et = (EditText)findViewById(R.id.editText1);
tv = (TextView)findViewById(R.id.textView1);
context = getApplicationContext();
LayoutInflater inflate = (LayoutInflater)context.getSystemService(LAYOUT_INFLATER_SERVICE);
layout = inflate.inflate(R.layout.ipsetting, null);
ip = (EditText)layout.findViewById(R.id.editIp);
port = (EditText)layout.findViewById(R.id.editPort);
send.setOnClickListener(this);
connect.setOnClickListener(this);
} //onCreate
@Override
public void onClick(View v) {
AlertDialog.Builder aDialog = new AlertDialog.Builder(this);
aDialog.setTitle("서버 접속");
aDialog.setView(layout);
if(v.getId()==R.id.send){
if(et.getText().toString() != null || !et.getText().toString().equals("")){
TCPclient tp = new TCPclient(et.getText().toString());
tp.run();
Toast t = Toast.makeText(getApplicationContext(), return_msg, Toast.LENGTH_LONG);
t.show();
tv.setText(return_msg);
}
}
else if(v.getId()==R.id.connect){
aDialog.setPositiveButton("접속", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
if(ip.getText().toString().equals("") || port.getText().toString().equals("")){
server_ip = "0.0.0.0";
server_port = 0000;
Toast pop = Toast.makeText(getApplicationContext(), "입력을 하지 않으셨습니다", 1500);
pop.show();
}
else{
server_ip = ip.getText().toString();
server_port = Integer.parseInt(""+port.getText());
}
}
});
aDialog.setNegativeButton("취소", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
AlertDialog ad = aDialog.create();
ad.show();
}
else{
}
}
[main xml]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<EditText
android:id="@+id/editText1"
android:hint="내용을 입력하세요"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:id="@+id/send"
android:layout_height="wrap_content"
android:text="Send"/>
<Button
android:layout_width="wrap_content"
android:id="@+id/connect"
android:layout_height="wrap_content"
android:text="설정"/>
</LinearLayout>
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
[ipsetting xml]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:text="IP : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/editIp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:text="Port : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/editPort"
android:inputType="number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
위 소스는 자바로 구동되는 서버와 소켓 통신하는 프로그램입니다.
소켓 통신 부분은 제외 했구요..
질문하고자 하는바는..
어플을 실행했을때 설정 버튼을 눌르면 AlertDialog 창이 뜨고 ip세팅과 port세팅 하고 서버와 통신하면 잘 이루어집니다.
그런데 설정버튼을 한번 더 누르게 되면 예상치 못한 오류로 어플을 강제 종료 하게 되는데..
->>>>>> 어플시작 - 설정버튼 클릭 - 접속이나 취소 버튼 누름 - 설정버튼 한번 더 클릭 - 강제종료...!!!! (도대체 왜???ㅠㅠ)
도대체 왜 그런지 알 수가 없어서요.. 도움좀 주세요~
2011.02.14 20:11:51
AlertDialog.Builder aDialog = new AlertDialog.Builder(this);
aDialog.setTitle("서버 접속");
aDialog.setView(layout);
버튼을 누를때마다 AlertDialog를 만들어서 거기에 layout을 넣으시는데요...
layout은 1개인데 AlertDialog는 버튼 누를때마다 만들면서
layout이 Parent가 여러개가 되는 상황이 된겁니다.
제가 권해드리는것은 showDialog()를 이용하는 것을 권해드립니다.
onCreateDialog를 오버라이드 하셔서 거기서 AlertDialog를 만드시면
생성 및 show 에 관련된 관리는 Activity에서 알아서 합니다.
aDialog.setTitle("서버 접속");
aDialog.setView(layout);
버튼을 누를때마다 AlertDialog를 만들어서 거기에 layout을 넣으시는데요...
layout은 1개인데 AlertDialog는 버튼 누를때마다 만들면서
layout이 Parent가 여러개가 되는 상황이 된겁니다.
제가 권해드리는것은 showDialog()를 이용하는 것을 권해드립니다.
onCreateDialog를 오버라이드 하셔서 거기서 AlertDialog를 만드시면
생성 및 show 에 관련된 관리는 Activity에서 알아서 합니다.
2011.02.14 20:13:52
빨간색으로 에러나는 부분을 위주로 보시면 됩니다.
비슷한 질문이 올라왔던 적이 있군요.
http://www.androidpub.com/406067
02-14 10:55:06.819: ERROR/AndroidRuntime(788): FATAL EXCEPTION: main
02-14 10:55:06.819: ERROR/AndroidRuntime(788): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at android.view.ViewGroup.addViewInner(ViewGroup.java:1970)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at android.view.ViewGroup.addView(ViewGroup.java:1865)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at android.view.ViewGroup.addView(ViewGroup.java:1845)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at com.android.internal.app.AlertController.setupView(AlertController.java:364)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at com.android.internal.app.AlertController.installContent(AlertController.java:205)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at android.app.AlertDialog.onCreate(AlertDialog.java:251)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at android.app.Dialog.dispatchOnCreate(Dialog.java:307)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at android.app.Dialog.show(Dialog.java:225)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at com.android.TCPsocketTest.TCPsocketTest.onClick(TCPsocketTest.java:103)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at android.view.View.performClick(View.java:2408)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at android.view.View$PerformClick.run(View.java:8816)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at android.os.Handler.handleCallback(Handler.java:587)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at android.os.Handler.dispatchMessage(Handler.java:92)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at android.os.Looper.loop(Looper.java:123)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at java.lang.reflect.Method.invokeNative(Native Method)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at java.lang.reflect.Method.invoke(Method.java:521)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at dalvik.system.NativeStart.main(Native Method)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at android.view.ViewGroup.addViewInner(ViewGroup.java:1970)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at android.view.ViewGroup.addView(ViewGroup.java:1865)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at android.view.ViewGroup.addView(ViewGroup.java:1845)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at com.android.internal.app.AlertController.setupView(AlertController.java:364)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at com.android.internal.app.AlertController.installContent(AlertController.java:205)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at android.app.AlertDialog.onCreate(AlertDialog.java:251)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at android.app.Dialog.dispatchOnCreate(Dialog.java:307)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at android.app.Dialog.show(Dialog.java:225)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at com.android.TCPsocketTest.TCPsocketTest.onClick(TCPsocketTest.java:103)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at android.view.View.performClick(View.java:2408)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at android.view.View$PerformClick.run(View.java:8816)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at android.os.Handler.handleCallback(Handler.java:587)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at android.os.Handler.dispatchMessage(Handler.java:92)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at android.os.Looper.loop(Looper.java:123)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at java.lang.reflect.Method.invokeNative(Native Method)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at java.lang.reflect.Method.invoke(Method.java:521)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-14 10:55:06.819: ERROR/AndroidRuntime(788): at dalvik.system.NativeStart.main(Native Method)
비슷한 질문이 올라왔던 적이 있군요.
http://www.androidpub.com/406067



