android按钮在两个屏幕之间切换

时间:2015-02-14 20:28:35

标签: button layout android-activity android-studio

我是android应用程序的初学者。我正在使用Android Studio制作购物车列表的应用程序。首先,我正在研究在每个屏幕上创建两个按钮,称为“编辑”和“保存”。因此,如果我单击编辑按钮,它将转到screen2,如果我单击保存,它将转到screen1。但是,每当我尝试午餐时,都会出现这样的错误:

02-14 15:14:57.830 AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.jieun.hw1, PID: 1782
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jieun.hw1/com.example.jieun.hw1.MainActivityOne}: java.lang.NullPointerException

我的两个屏幕的代码和布局是这样的:

屏蔽1:

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

Button editButton = (Button) findViewById(R.id.e_button);

editButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View arg0) {
        //Starting a new Intent
        Intent editScreen = new Intent(..MainActivityTwo.class);
        startActivity(editScreen);
    }
});

}

屏蔽2:

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

        Button saveButton = (Button) findViewById(R.id.s_button);

        saveButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                //Starting a new Intent
                Intent saveScreen;
                saveScreen = new Intent(getApplicationContext(), MainActivityOne.class);
                startActivity(saveScreen);
            }
        });

   }

布局1:

<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: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=".MainActivityOne">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Edit"
    android:id="@+id/e_button"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

布局2:

<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: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=".MainActivityOne">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save"
        android:id="@+id/s_button"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

您是否尝试查看屏幕1的代码?

这里:

  //Starting a new Intent 
        Intent editScreen = new Intent(..MainActivityTwo.class);
        startActivity(editScreen);
    } 

MainActivityTwo.class

之前有两个时期