当我单击按钮以显示下一个活动时,它仅显示白屏 [英] when i click on button to showing next activity it shows only white screen
问题描述
当我单击按钮时,它仅显示白屏,而不显示下一个活动的视图.我不知道为什么会发生这种情况,请给我解决方案…… 我的整个代码在这里
When i click on button it shows onlywhite screen not the view of next activity. I can't find why it happen pls give me solution...... my whole code is here
ActivityMain.java
ActivityMain.java
package com.rahul.intentswitchingwithdata;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void go(View v){
Intent i= new Intent(this,Next.class);
startActivity(i);}
}
activity_main.xml
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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.rahul.intentswitchingwithdata.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="go"/>
</RelativeLayout>
Next.java
Next.java
public class Next extends Activity {
@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle
persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.next);
}
}
next.xml
?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#123"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"/>
</LinearLayout>
manifest.xml
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rahul.intentswitchingwithdata">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Next"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
推荐答案
您覆盖"了错误的onCreate
方法:
you have "overridden" the wrong onCreate
method:
@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle
persistentState)
您应该改用它:
@Override
public void onCreate(Bundle savedInstanceState){...code}
onCreate(Bundle)
用于初始化,意味着您的所有UI初始化(如视图)均已完成,
onCreate(Bundle)
is used for initialization mean where your all UI initialization like view are done ,docs link for depth details
onCreate(Bundle, PersistableBundle)
本身暗示,它用于持久性平均重用旧意图(此活动首次提供),同时重新创建它,您可以通过在活动中添加persistableMode
标志来启用重用,以便活动将在重新启动后持续存在.尝试使用此文档链接获取其他选项的详细信息.
onCreate(Bundle, PersistableBundle)
as it, itself suggesting , it is used for persistent mean reuse the old intent (supplied first time to this activity) while recreating it , you can enable reusing by adding a persistableMode
flag to your activity so that activity will be persisted across reboots . try this doc link for other options details .
注意::您只能在API级别21(Android Lollipop 5.0)或更高版本中使用onCreate(Bundle, PersistableBundle)
.
Note : You can only use onCreate(Bundle, PersistableBundle)
in API level 21(Android Lollipop 5.0) or above.
这篇关于当我单击按钮以显示下一个活动时,它仅显示白屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!