通过单击按钮更改屏幕(活动)

时间:2016-10-18 12:39:42

标签: java android

目标是改变屏幕。每当我尝试这个时,一切都崩溃了。我不确定我做错了什么。请记住,我仍然是Android移动开发的新手。任何意见,将不胜感激。

清单文件

Height="50"

主要活动java

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.example.shanesescott.segway">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".rent"></activity>
  </application>

</manifest>

租用java文件

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.content.Intent;

public class MainActivity extends AppCompatActivity {

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

}

public void onButtonClick(View v) {
    if (v.getId() == R.id.Bdisplay) {
        Intent i = new Intent(MainActivity.this, rent.class);
        startActivity(i);
    }

   }
}

租借活动(按钮点击时显示的第二项活动)

 import android.app.Activity;
    import android.os.Bundle;

    /**
    * 
    */

    public class rent extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.rent);
     }
   }

主要活动(XML)

e<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:weightSum="1"
android:orientation="vertical">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="350dp"
    app:srcCompat="@drawable/team"
    android:id="@+id/imageView6" />

<TextView
    android:text="The Segway Tour is only  $52 for an amazing 2-hour journey."

    android:layout_width="match_parent"
    android:layout_height="178dp"
    android:id="@+id/textView13"
    android:textSize="30sp" />
   </LinearLayout>

2 个答案:

答案 0 :(得分:0)

这是可行的代码

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ((Button)findViewById(R.id.Bdisplay)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,Rent.class));
            }
        });

    }
}


public class Rent extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rent);

            }
}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sysfore.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Rent"/>

    </application>

</manifest>


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    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">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="84dp"
        android:layout_marginStart="84dp"
        android:text="Segway Rental"
        android:textAlignment="textStart"
        android:textColor="@color/colorPrimary"
        android:textColorLink="?attr/actionModeSplitBackground"
        android:textSize="30sp"
        android:textStyle="normal|bold"
        android:typeface="sans" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/textView"
        android:layout_marginLeft="15dp"
        android:layout_marginStart="15dp"
        android:layout_marginTop="13dp"
        android:text="Experience the wonder of this future advancement of transportation for a city group tour starting at noon each day  "
        android:textSize="21sp" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="14dp" />

    <Button
        android:id="@+id/Bdisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView4"
        android:layout_centerHorizontal="true"
        android:onClick="onClickButton"
        android:text="RENT A SEGWAY"
        android:textSize="12sp" />
</RelativeLayout>


<?xml version="1.0" encoding="utf-8"?><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:weightSum="1">

    <ImageView
        android:id="@+id/imageView6"
        android:layout_width="match_parent"
        android:layout_height="350dp"
        />

    <TextView
        android:id="@+id/textView13"

        android:layout_width="match_parent"
        android:layout_height="178dp"
        android:text="The Segway Tour is only  $52 for an amazing 2-hour journey."
        android:textSize="30sp" />
</LinearLayout>

答案 1 :(得分:0)

删除字母'e',这是您在rent.xml文件中的第一个字母。