我添加了两个/**
* Waits for a new file to be downloaded with a file watcher
*/
public static File WaitForNewFile(Path folder, String extension, int timeout_sec) throws InterruptedException, IOException {
long end_time = System.currentTimeMillis() + timeout_sec * 1000;
try (WatchService watcher = FileSystems.getDefault().newWatchService()) {
folder.register(watcher, StandardWatchEventKinds.ENTRY_MODIFY);
for (WatchKey key; null != (key = watcher.poll(end_time - System.currentTimeMillis(), TimeUnit.MILLISECONDS)); key.reset()) {
for (WatchEvent<?> event : key.pollEvents()) {
File file = folder.resolve(((WatchEvent<Path>)event).context()).toFile();
if (file.toString().toLowerCase().endsWith(extension.toLowerCase()))
return file;
}
}
}
return null;
}
,在这些布局上,我动态添加LinearLayouts
。最终目的是将数据存储在数据库中。
我收到此错误:
Checkboxes
你能帮我找到问题吗?感谢。
这是我的代码:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.freedomkitchen.sonali.freedomkitchen/com.freedomkitchen.sonali.freedomkitchenAndroidApp.AddRecipes}:
java.lang.IllegalStateException: The specified child already has a
parent. You must call removeView() on the child's parent first.
XML
public class AddRecipes extends AppCompatActivity {
static int a=0,b=0,c=0,d=0;
static int id=0;
public int x;
int idvalueVeg;
int idvalueFruits;
int idvalueGrains;
int idvalueDairy;
int idvalueSeaFood;
int end_of_sup_ing;
int sup_ing_id;
ArrayList<String> Main_Ingredients;
ArrayList<String> Supporting_Ingredients;
LinearLayout ll;
LinearLayout ll2;
Spinner FoodCatValue;
public String MealSelected;
EditText RecipeNameValue;
public Spinner sItems;
private static final boolean AUTO_HIDE = true;
private static final int AUTO_HIDE_DELAY_MILLIS = 3000;
private static final int UI_ANIMATION_DELAY = 300;
private final Handler mHideHandler = new Handler();
private View mContentView;
private final Runnable mHidePart2Runnable = new Runnable() {
@SuppressLint("InlinedApi")
@Override
public void run() {
mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}
};
private View mControlsView;
private final Runnable mShowPart2Runnable = new Runnable() {
@Override
public void run() {
// Delayed display of UI elements
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.show();
}
mControlsView.setVisibility(View.VISIBLE);
}
};
private boolean mVisible;
private final Runnable mHideRunnable = new Runnable() {
@Override
public void run() {
hide();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addrecipes);
ll = (LinearLayout) findViewById(R.id.chk_layout);
ll2 = (LinearLayout) findViewById(R.id.chk_layout2);
FoodCatValue = (Spinner)findViewById(R.id.spinnerFoodCat);
Resources res = getResources();
String[] Appetizers = res.getStringArray(R.array.Appetizer_Meals);
String[] Main_Course = res.getStringArray(R.array.Main_Course_Meals);
String[] Desserts = res.getStringArray(R.array.Desserts_Meals);
ArrayList<String> AppetizerList = new ArrayList<String>(Arrays.asList(Appetizers));
ArrayList<String> Main_CourseList = new ArrayList<String>(Arrays.asList(Main_Course));
ArrayList<String> DessertList = new ArrayList<String>(Arrays.asList(Desserts));
if(FoodCatValue.getSelectedItem().toString().equals("Appetizers")){
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, AppetizerList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sItems = (Spinner) findViewById(R.id.spinnerMealCat);
sItems.setAdapter(adapter);
getMealSelectedVal();
}
else if(FoodCatValue.getSelectedItem().toString().equals("Main Course"))
{
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, Main_CourseList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sItems = (Spinner) findViewById(R.id.spinnerMealCat);
sItems.setAdapter(adapter);
getMealSelectedVal();
}
else if(FoodCatValue.getSelectedItem().toString().equals("Dessert")){
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, DessertList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sItems = (Spinner) findViewById(R.id.spinnerMealCat);
sItems.setAdapter(adapter);
getMealSelectedVal();
}
RecipeNameValue =(EditText)findViewById(R.id.RecipeName);
DB_Access mydb=new DB_Access(this);
ArrayList<String> Vegetables=mydb.getIngredients(1);
ArrayList<String> Fruits=mydb.getIngredients(2);
ArrayList<String> Dairy=mydb.getIngredients(3);
ArrayList<String> Grains=mydb.getIngredients(4);
ArrayList<String> Seafood=mydb.getIngredients(5);
ArrayList<String> Sup_Ing=mydb.getSupportingIngredients();
final TextView VegTv = new TextView(this);
VegTv.setText("Vegetables");
ll.addView(VegTv);
for (x=0;x<Vegetables.size(); x++,id++) {
final CheckBox checkbox = new CheckBox(this);
checkbox.setId(id);
String Item = Vegetables.get(x);
checkbox.setText(Item);
ll.addView(checkbox);
idvalueVeg=id;
}
TextView FruitsTv = new TextView(this);
FruitsTv.setText("Fruits");
ll.addView(FruitsTv);
for (x=0;x<Fruits.size(); x++,id++) {
CheckBox checkbox = new CheckBox(this);
checkbox.setId(id);
String Item = Fruits.get(x);
checkbox.setText(Item);
ll.addView(checkbox);
idvalueFruits=id;
}
TextView GrainsTv = new TextView(this);
GrainsTv.setText("Grains");
ll.addView(GrainsTv);
for (x=0;x<Grains.size(); x++,id++) {
CheckBox checkbox = new CheckBox(this);
checkbox.setId(id);
String Item = Grains.get(x);
checkbox.setText(Item);
ll.addView(checkbox);
idvalueGrains=id;
}
TextView DairyTv = new TextView(this);
DairyTv.setText("Dairy");
ll.addView(DairyTv);
for(x=0;x<Dairy.size(); x++,id++) {
CheckBox checkbox = new CheckBox(this);
checkbox.setId(id);
String Item = Dairy.get(x);
checkbox.setText(Item);
ll.addView(checkbox);
idvalueDairy=id;
}
TextView SeafoodTv = new TextView(this);
SeafoodTv.setText("Seafood");
ll.addView(DairyTv);
for(x=0;x<Seafood.size(); x++,id++) {
CheckBox checkbox = new CheckBox(this);
checkbox.setId(id);
String Item = Seafood.get(x);
checkbox.setText(Item);
ll2.addView(checkbox);
idvalueSeaFood=id;
}
TextView supIng = (TextView)findViewById(R.id.sup_ingTextView);
DairyTv.setText("Supporting Ingredients");
ll.addView(supIng);
sup_ing_id = idvalueSeaFood;
for(x=0;x<Sup_Ing.size(); x++,id++) {
CheckBox checkbox = new CheckBox(this);
checkbox.setId(id);
String Item = Sup_Ing.get(x);
checkbox.setText(Item);
ll.addView(checkbox);
end_of_sup_ing=id;
}
}
public void getMealSelectedVal(){
MealSelected= sItems.getSelectedItem().toString();
}
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
}
private void hide() {
// Hide UI first
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}
mControlsView.setVisibility(View.GONE);
mVisible = false;
// Schedule a runnable to remove the status and navigation bar after a delay
mHideHandler.removeCallbacks(mShowPart2Runnable);
mHideHandler.postDelayed(mHidePart2Runnable, UI_ANIMATION_DELAY);
}
@SuppressLint("InlinedApi")
private void show() {
// Show the system bar
mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
mVisible = true;
// Schedule a runnable to display UI elements after a delay
mHideHandler.removeCallbacks(mHidePart2Runnable);
mHideHandler.postDelayed(mShowPart2Runnable, UI_ANIMATION_DELAY);
}
public void btnAddRecipes(View v){
GetInfo();
DB_Access mydb=new DB_Access(this);
EditText ins =(EditText)findViewById(R.id.RecipeTextArea);
mydb.adduserRecipes(FoodCatValue.getSelectedItem().toString(),MealSelected,RecipeNameValue.getText().toString(), Main_Ingredients,Supporting_Ingredients,ins.getText().toString());
}
public void GetInfo(){
Main_Ingredients=new ArrayList<String>(200);
Supporting_Ingredients=new ArrayList<String>(200);
for(x=0;x<id;x++){
CheckBox cb=(CheckBox)findViewById(x);
if(cb.isChecked()){
{
String item=cb.getText().toString();
Main_Ingredients.add(item);
}
}
}
//disp main ing
for(int i=0;i<Main_Ingredients.size();i++){
Log.i("Main_Ing:",Main_Ingredients.get(i));
}
for(x=sup_ing_id;x<=end_of_sup_ing;x++){
CheckBox cb=(CheckBox)findViewById(x);
if(cb.isChecked()){
{
String item=cb.getText().toString();
Supporting_Ingredients.add(item);
}
}
}
for(int i=0;i<Supporting_Ingredients.size();i++){
Log.i("Sup_Ing:",Supporting_Ingredients.get(i));
}
}
}
答案 0 :(得分:0)
当您尝试将两次视图添加到布局时,会触发此异常。
您复制了代码,但未在此处更改
stringtempA = stringtemp.Replace("%0D%0A%0D%0A", "%0D%0A")
我相信它应该是
TextView SeafoodTv = new TextView(this);
SeafoodTv.setText("Seafood");
ll.addView(DairyTv);
为避免此类错误,最好将静态视图放在xml布局中。