当我点击按钮显示下一个活动时,它只显示白色屏幕

时间:2016-10-30 08:21:37

标签: android

当我点击按钮时,它只显示白色屏幕而不显示下一个活动的视图。我找不到为什么会发生请给我解决方案...... 我的整个代码都在这里

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中

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

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

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

1 个答案:

答案 0 :(得分:9)

您已“覆盖”了错误的onCreate方法:

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

你应该使用它:

 @Override
    public void onCreate(Bundle savedInstanceState){...code}

onCreate(Bundle)用于初始化意味着您完成所有UI初始化视图,docs link for depth details

onCreate(Bundle, PersistableBundle)正如它本身所暗示的,它用于持久平均重用旧的意图(第一次提供给此活动),同时重新创建它,您可以通过添加persistableMode标志来启用重用您的活动,以便在重新启动后保持活动。试试这个other options details 的文档链接。

注意:您只能在API级别21(Android Lollipop 5.0)或更高版本中使用onCreate(Bundle, PersistableBundle)

相关问题