1

从我的 MainActivity 切换到 secondActivity 时,会导致“意外错误”。

这是.java

public class AddDebt extends Activity implements OnClickListener {

    Button home, addDebt;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        home = (Button) findViewById(R.id.home);
        addDebt = (Button) findViewById(R.id.addDebt);

        home.setOnClickListener(this); // line 20
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.home:
                //Switch to Debtor
                startActivity(new Intent(this, MainActivity.class));
                break;

            case R.id.addDebt:
                //Add New Debt Info

                break;
        }
    }
}

这是我的 activity_main.xml - 当我删除 .java 的第 20 行时,它会更改活动的名称,但布局不会更改为新布局。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textDebtor"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="30dp"
    android:layout_marginTop="10dp"
    android:text="Debtor"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textSize="12sp" />

<TextView
    android:id="@+id/textBalance"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textDebtor"
    android:layout_marginLeft="15dp"
    android:layout_toRightOf="@+id/textDebtor"
    android:text="Balance"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textSize="12sp" />

<TextView
    android:id="@+id/textPayment"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textBalance"
    android:layout_marginLeft="15dp"
    android:layout_toRightOf="@+id/textBalance"
    android:text="Payment"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textSize="12sp" />

<TextView
    android:id="@+id/textOrder"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textPayment"
    android:layout_marginLeft="15dp"
    android:layout_toRightOf="@+id/textPayment"
    android:text="Paydown Order"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textSize="12sp" />

<TextView
    android:id="@+id/textDebtor1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textDebtor"
    android:layout_marginLeft="5dp"
    android:layout_marginTop="7dp"
    android:text="" />

<TextView
    android:id="@+id/textBalance1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textBalance"
    android:layout_below="@+id/textBalance"
    android:layout_marginTop="7dp"
    android:text="" />

<TextView
    android:id="@+id/textPayment1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textPayment"
    android:layout_below="@+id/textPayment"
    android:layout_marginTop="7dp"
    android:text="" />








<Button
    android:id="@+id/addDebtor"
    style="?android:attr/buttonStyleSmall"
    android:layout_marginLeft="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:text="New Debt" />

<Button
    android:id="@+id/Menu"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/addDebtor"
    android:layout_toRightOf="@+id/addDebtor"
    android:text="Menu" />

<Button
    android:id="@+id/About"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/Menu"
    android:layout_toRightOf="@+id/Menu"
    android:text="About" />

<Button
    android:id="@+id/Calculator"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/About"
    android:layout_toRightOf="@+id/About"
    android:text="Calc" />

</RelativeLayout>
4

0 回答 0