我想添加一個佈局(profile_upload)到我的自定義AlertDialog裏面的Profile.java 第一次工作正常。但是當我關閉AlertDialog並再次單擊按鈕時它崩潰sayng java.lang.IllegalStateException:指定的子項已經有父項。你必須對孩子的父母打電話removeView()第一 Profile.javaandriod Inflater /查看異常
package com.example.accer.sportsgr;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
public class Profile extends AppCompatActivity implements View.OnClickListener {
ImageButton profile_btn;
AlertDialog.Builder alert;
Bitmap bitmap;
TextView tx_img_name;
Button btn_prfil_upld,btn_pic_choose;
ImageView img_prfl;
View v;
LayoutInflater inflater;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
profile_btn=(ImageButton)findViewById(R.id.user_profile_photo);
alert=new AlertDialog.Builder(this);
inflater=Profile.this.getLayoutInflater();
if (v == null) {
v = inflater.inflate(R.layout.profile_upload, null, false);
} else {
((ViewGroup) v.getParent()).removeView(v);
}
alert.setView(v);
btn_prfil_upld=(Button)v.findViewById(R.id.btnUpload_profile_pic);
btn_pic_choose=(Button)v.findViewById(R.id.btn_choose_profile_pic);
img_prfl=(ImageView)v.findViewById(R.id.img_profile_upload);
tx_img_name=(TextView)v.findViewById(R.id.tx_img_name);
btn_prfil_upld.setOnClickListener(this);
btn_pic_choose.setOnClickListener(this);
profile_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
alert.setTitle("Update Profile Picture");
alert.show();
}
});
}
private void showFileChooser() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null)
{
Uri filepath=data.getData();
try {
bitmap= MediaStore.Images.Media.getBitmap(getContentResolver(),filepath);
tx_img_name.setText(filepath.toString());
img_prfl.setImageBitmap(bitmap);
} catch (IOException e) {
Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onClick(View view) {
int id=view.getId();
switch (id)
{
case R.id.btn_choose_profile_pic :
showFileChooser();
break;
case R.id.btnUpload_profile_pic:
break;
}
}
}
profile_upload.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Choose Image"
android:id="@+id/btn_choose_profile_pic" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/img_profile_upload" />
<TextView
android:id="@+id/tx_img_name"
android:textAlignment="center"
android:gravity="center"
android:textSize="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Upload Image"
android:id="@+id/btnUpload_profile_pic" />
</LinearLayout>
activity_profile.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/header_cover_image"
android:layout_width="match_parent"
android:layout_height="75dp"
android:scaleType="centerCrop"
android:src="@drawable/navigation_header_image"/>
<ImageButton
android:id="@+id/user_profile_photo"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_below="@+id/header_cover_image"
android:layout_centerHorizontal="true"
android:layout_marginTop="-60dp"
android:background="@drawable/circular_imageview"
android:elevation="5dp"
android:padding="20dp"
android:scaleType="centerCrop"
android:clickable="true"/>
<!-- android:src="@drawable/profile"-->
<RelativeLayout
android:id="@+id/profile_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/header_cover_image"
android:background="#ebca0707"
android:elevation="4dp"
android:paddingBottom="24dp">
<ImageView
android:id="@+id/add_friend"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="25dp"
android:src="@drawable/ic_action_user_add"
android:layout_toLeftOf="@+id/drop_down_option_menu"/>
<ImageView
android:id="@+id/drop_down_option_menu"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:src="@drawable/ic_action_overflow"/>
<TextView
android:id="@+id/user_profile_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="76dp"
android:text="Name"
android:textColor="#fff"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:id="@+id/user_profile_short_bio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/user_profile_name"
android:layout_centerHorizontal="true"
android:layout_marginTop="12dp"
android:text="Email"
android:textColor="#fff"
android:textSize="14sp" />
</RelativeLayout>
</RelativeLayout>
錯誤日誌:
2-17 16:17:47.395 13465-13465/com.example.accer.sportsgr E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.accer.sportsgr, PID: 13465
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:4454)
at android.view.ViewGroup.addView(ViewGroup.java:4295)
at android.view.ViewGroup.addView(ViewGroup.java:4267)
at android.support.v7.app.AlertController.setupCustomContent(AlertController.java:613)
at android.support.v7.app.AlertController.setupView(AlertController.java:452)
at android.support.v7.app.AlertController.installContent(AlertController.java:215)
at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:258)
at android.app.Dialog.dispatchOnCreate(Dialog.java:389)
at android.app.Dialog.show(Dialog.java:293)
at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:956)
at com.example.accer.sportsgr.Profile$1.onClick(Profile.java:98)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
顯示「activity_profile.xml」的內容? – t0mm13b