java Android Studio:新活动以空白屏幕打开

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29060027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:36:34  来源:igfitidea点击:

Android Studio: New activity opens as blank screen

javaandroidandroid-activityandroid-studio

提问by HymanEarlyDays

Hello!
I'm just starting in Android Studio. I searched for a matching question but no joy, please yell out if you've seen this one already!

你好!
我刚刚开始使用 Android Studio。我搜索了一个匹配的问题,但没有任何乐趣,如果您已经看过这个问题,请大喊大叫!

My main activity has a single button which opens the second activity, the button works and it opens. But the second activity shows as a blank screen instead of the text that should be there.

我的主要活动有一个按钮可以打开第二个活动,该按钮起作用并打开。但是第二个活动显示为一个空白屏幕,而不是应该在那里的文本。

Apologies for any irrelevant copy/paste!

对任何不相关的复制/粘贴表示歉意!

Manifest:

显现:

    <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>

Main activity:

主要活动:

<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>

Button code in main class

主类中的按钮代码

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));
            }
        });

    }

Second activity xml

第二个活动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>

Thanks!

谢谢!

回答by shekhar g h

It sometimes happens when instead of overloading

它有时发生在而不是重载时

protected void onCreate(Bundle savedInstanceState) {

you end up overloading

你最终会超载

public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) 

instead. Make sure the onCreate with only savedInstanceState as a argument is overloaded.

反而。确保仅以savedInstanceState 作为参数的onCreate 已重载。

回答by bonney

Have you set the layout for your activity in the line setContentView() like this. Here activity_second is the activity layout of my SecondActivity

您是否像这样在 setContentView() 行中为您的活动设置了布局。这里的activity_second是我的SecondActivity的Activity布局

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

if not then your activity will show up as blank. Hope this solves your problem.

如果没有,那么您的活动将显示为空白。希望这能解决您的问题。

回答by Heshan Sandeepa

Check weather you have set the correct layout for the setContentView() method of your second activity.

检查天气,您已为第二个活动的 setContentView() 方法设置了正确的布局。

回答by Ankul Abhishek Roy

You can just go to dependencies in build.gradle file and add this line there.. & try.

您可以转到 build.gradle 文件中的依赖项并在那里添加这一行..& 尝试。

implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'

实现 'com.android.support:appcompat-v7:28.0.0-alpha1'

回答by pear95

In my case, I changed device in Android Virtual Divice Manager and this fix problem

就我而言,我在 Android Virtual Divice Manager 中更改了设备并解决了这个问题