更新:所以我忘了澄清这一点,但是按钮首先在App UNTIL动态添加时不存在。一旦布局用按钮充气,按钮就会导致崩溃。我为这一发展带来的问题道歉。
因此,无论如何都有一个按钮不断崩溃我的程序(除非我删除了onClick)。我测试了我的Java代码中的每一行,看看是什么导致了它,但即使把它留空,它仍然会崩溃。
从我的MainActivity.Java中,导致崩溃的方法:
public void changeState(View v) {
String owe = "O";
String debt = "D";
button = (Button) findViewById(R.id.decl);
if (button.getText().toString().equals("D")) {
button.setText("O");
} else {
button.setText(("D"));
}
}
同样来自Java文件的布局膨胀方法,它创建导致崩溃的按钮:
public void create(MenuItem v) {
newContact = new LinearLayout(this);
newContact.setOrientation(LinearLayout.HORIZONTAL);
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = vi.inflate(R.layout.newcontact, null);
view.setOnTouchListener(new OnSwipeTouchListener(this) {
public void onSwipeRight() {
}
});
// insert into main view
ViewGroup insertPoint = (ViewGroup) findViewById(R.id.insert);
insertPoint.addView(view);
}
上面的Java代码会膨胀这个xml文件:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#CCFFFF"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="81dp"
android:orientation="horizontal">
<EditText
android:id="@+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="12dp"
android:layout_weight="2"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName"
android:padding="14dp"
android:shadowColor="#0066FF"
android:textColor="#000000"
android:textColorHint="#8F8F8F" />
<Button
android:id="@+id/decl"
android:layout_width="0dp"
android:layout_weight=".75"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="D"
android:textColor="#FFFFFF"
android:textSize="20dp"/>
<!--android:onClick="changeState"-->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="vertical">\
<EditText
android:id="@+id/amount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="(Ex 123.25)"
android:inputType="numberDecimal"
android:shadowColor="#0066FF"
android:textColor="#000000"
android:textColorHint="#8F8F8F"
android:textSize="16dp" />
<EditText
android:id="@+id/owed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="(Ex $, coffee)"
android:shadowColor="#0066FF"
android:textColor="#000000"
android:textColorHint="#8F8F8F"
android:textSize="16dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="3dp"
android:orientation="vertical"
android:background="#CFCFCF"
/>
按钮:
<Button
android:id="@+id/decl"
android:layout_width="0dp"
android:layout_weight=".75"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="D"
android:textColor="#FFFFFF"
android:textSize="20dp"
android:onClick="changeState"
/>
崩溃日志
08-09 22:44:22.792 1951-1951/com.xephos.detra E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.xephos.detra, PID: 1951
java.lang.IllegalStateException: Could not find a method changeState(View) in the activity class android.app.Application for onClick handler on view class android.widget.Button with id 'decl'
at android.view.View$1.onClick(View.java:4007)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at ...
答案 0 :(得分:0)
修改强> 在setContentView()之后将它放在MainActivity中:
public void create(MenuItem v) {
newContact = new LinearLayout(this);
newContact.setOrientation(LinearLayout.HORIZONTAL);
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = vi.inflate(R.layout.newcontact, null);
Button b = (Button)view.findViewById(R.id.decl);
if (b != null) {
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Button b = (Button)v;
if ("D".equals(b.getText().toString())) {
b.setText("O");
} else {
b.setText(("D"));
}
}
});
}
view.setOnTouchListener(new OnSwipeTouchListener(this) {
public void onSwipeRight() {
}
});
// insert into main view
ViewGroup insertPoint = (ViewGroup) findViewById(R.id.insert);
insertPoint.addView(view);
}