我试图通过按下子视图内的按钮将子视图添加到主视图,但代码不起作用。任何想法是什么问题。我是安卓初学者
这是主线性布局,子线性布局与添加的 + 和 - 按钮一致。我想通过按 + 在此下方添加另一行,然后按 - 将其删除。
这是主要布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/trainer_main"
android:layout_width="768dp"
android:layout_height="1280dp"
android:background="#121617"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
tools:context=".TrainingProgrammer" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="25dip"
android:textColor="#ffffff"
android:textStyle="bold"
android:text="@string/title_activity_training_programmer" />
<LinearLayout
android:id="@+id/trainer_headline"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/interval_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:text="@string/training_programmer_interval" />
<TextView
android:id="@+id/exercise_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dip"
android:paddingRight="95dip"
android:textColor="#ffffff"
android:text="@string/training_programmer_exercise" />
<TextView
android:id="@+id/second_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="25dip"
android:textColor="#ffffff"
android:text="@string/training_programmer_time" />
<TextView
android:id="@+id/weight_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dip"
android:textColor="#ffffff"
android:text="@string/training_programmer_weight" />
</LinearLayout>
这是子布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/trainer_progcontent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/interval_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="14sp"
android:ems="1"
android:text="1" />
<EditText
android:id="@+id/exercise_content"
android:layout_width="127dp"
android:layout_height="wrap_content"
android:textSize="14sp"
android:singleLine="true"
android:textColor="#ffffff"
android:hint="@string/training_programmer_exercise_edit"
android:ems="10" />
<EditText
android:id="@+id/time_content"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:hint="@string/training_programmer_time_edit"
android:inputType="number"
android:maxLength="2"
android:textColor="#ffffff" />
<EditText
android:id="@+id/weight_content"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:hint="@string/training_programmer_weight_edit"
android:inputType="number"
android:maxLength="2"
android:textColor="#ffffff" />
<Button
android:id="@+id/button_minus"
style="?android:attr/buttonStyleSmall"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="20dp"
android:background="@drawable/minus2" />
<Button
android:id="@+id/button_plus"
style="?android:attr/buttonStyleSmall"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="20dp"
android:background="@drawable/plus2" />
这是代码:
package ca.cruxtec.cruxtectrainer;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
public class ProgrammerActivity extends ActionBarActivity {
private Button button_plus_define;
private Button button_minus_define;
static private final int MAX_NO_EXER = 30;
private int i = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_programmer);
final LinearLayout programmer_main_layout = (LinearLayout)findViewById(R.id.trainer_main);
//LinearLayout sequence_layout = (LinearLayout)findViewById(R.id.trainer_progcontent);
// Layout inflation for addition of the child view to the main view
final View adding_child = getLayoutInflater().
inflate(R.layout.sequence, programmer_main_layout, false);
programmer_main_layout.addView(adding_child);
// Plus button
button_plus_define = (Button)findViewById(R.id.button_plus);
button_plus_define.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
i++;
programmer_main_layout.addView(adding_child);
/* Toast.makeText(getApplicationContext(),
"i is :" + i, Toast.LENGTH_LONG).show();*/
}
});
// minus button
button_minus_define = (Button)findViewById(R.id.button_minus);
button_minus_define.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
/*if(i>2) i--;
else {
Toast.makeText(getApplicationContext(),
"i is less than 2:" , Toast.LENGTH_LONG).show();
}
Toast.makeText(getApplicationContext(),
"i is :" +i , Toast.LENGTH_LONG).show();*/
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_programmer, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
该应用程序由于拥有而崩溃
programmer_main_layout.addView(adding_child);
+ 按钮内
这是主要错误:
30799-30799/ca.cruxtec.cruxtectrainer E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.