我是Android开发新手。我试图通过单击按钮打开第二个活动,但当我尝试我的apk停止。谁能帮助我弄清楚我的错误。以下是我的代码
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pradeep.findme">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true">
<activity
android:name=".MainActivity"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainPlayActivity"
android:screenOrientation="landscape"
android:parentActivityName=".MainActivity">
</activity>
</application>
</manifest>
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.pradeep.findme.MainActivity"
android:background="@drawable/uibg"
android:theme="@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" >
<Button
android:layout_width="90dp"
android:layout_height="50dp"
android:id="@+id/pbutton"
android:background="@drawable/play"
android:textColor="#0760db"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
/>
<Button
android:layout_width="90dp"
android:layout_height="50dp"
android:id="@+id/ibutton"
android:layout_gravity="bottom"
android:background="@drawable/inst"
android:textColor="#0760db"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/pbutton"
android:layout_marginLeft="10dp"/>
<Button
android:layout_width="90dp"
android:layout_height="50dp"
android:id="@+id/sbutton"
android:layout_gravity="bottom"
android:background="@drawable/story"
android:textColor="#0760db"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/ibutton"
android:layout_marginLeft="10dp"/>
<Button
android:layout_width="90dp"
android:layout_height="50dp"
android:id="@+id/ebutton"
android:layout_gravity="bottom"
android:background="@drawable/exit"
android:textColor="#0760db"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/sbutton"
android:layout_marginLeft="10dp" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity {
Button Quit,Play,Story,Ins;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.setContentView(R.layout.activity_main);
Quit = (Button)findViewById(R.id.ebutton);
Play= (Button)findViewById(R.id.pbutton);
Story= (Button)findViewById(R.id.sbutton);
Ins= (Button)findViewById(R.id.ibutton);
Quit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
Play.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent intent = new Intent(getApplicationContext(), MainPlayActivity.class);
startActivity(intent);
}
});
}
}
activity_main_play.xml
<?xml version="1.0" encoding="utf-8"?>
<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="com.example.pradeep.findme.MainPlayActivity"
android:background="@drawable/room2"
android:id="@+id/playscreen"
android:theme="@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen">
</RelativeLayout>
MainPlayActivity.java
public class MainPlayActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_play);
}
}
答案 0 :(得分:0)
我看到你使用R.layout.activity_main_play
什么是R.layout.activity_main_play
?
可能的解决方案:
将SecondActivity.xml
重命名为activity_main_play.xml
但首先你应该看看错误日志。
答案 1 :(得分:0)
你忘了申请主题。
将android:theme="@style/AppTheme"
放入清单文件中,如:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">