[英]Android Studio: New activity opens as blank screen
你好!
我刚开始使用 Android Studio。 我搜索了一个匹配的问题,但没有任何乐趣,如果您已经看过这个问题,请大喊大叫!
我的主要活动有一个按钮可以打开第二个活动,该按钮起作用并打开。 但是第二个活动显示为空白屏幕,而不是应该在那里的文本。
对任何不相关的复制/粘贴表示歉意!
显现:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.NoActionBar" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".fiveThreeOne"
android:label="@string/title531">
</activity>
</application>
主要活动:
<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:textColor="#ffdedede"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fiveThreeOne"
android:textAllCaps="false"
android:id="@+id/open531btn"
android:layout_below="@+id/mainTitle"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="38dp" />
</RelativeLayout>
主类中的按钮代码
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button open531button = (Button) findViewById(R.id.open531btn);
open531button.setOnClickListener(new OnClickListener(){
public void onClick(View v){
startActivity(new Intent(MainActivity.this, fiveThreeOne.class));
}
});
}
第二个活动xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="@string/title531"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:editable="false"
android:textSize="35sp"
android:id="@+id/title531"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
谢谢!
它有时发生在而不是重载时
protected void onCreate(Bundle savedInstanceState) {
你最终会超载
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState)
反而。 确保仅以savedInstanceState 作为参数的onCreate 已重载。
您是否像这样在 setContentView() 行中为您的活动设置了布局。 这里的activity_second是我的SecondActivity的Activity布局
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
如果没有,那么您的活动将显示为空白。 希望这能解决您的问题。
检查天气,您已为第二个活动的 setContentView() 方法设置了正确的布局。
您可以转到 build.gradle 文件中的依赖项并在那里添加这一行..& 尝试。
实现 'com.android.support:appcompat-v7:28.0.0-alpha1'
就我而言,我在 Android Virtual Divice Manager 中更改了设备并解决了这个问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.