6

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

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>
Rahul Mishra
  • 1,122
  • 1
  • 10
  • 25

1 Answers1

11

you have "overridden" the wrong onCreate method:

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

you should use this instead:

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

onCreate(Bundle) is used for initialization mean where your all UI initialization like view are done ,docs link for depth details

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 .

Note : You can only use onCreate(Bundle, PersistableBundle) in API level 21(Android Lollipop 5.0) or above.

ericn
  • 12,476
  • 16
  • 84
  • 127
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68