When I press the button to show the next activity, only a white screen is displayed

When I press the button, it only shows a white screen, not the kind of next activity. I can’t find out why this happens when they give me a solution ...... all my 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> 
+2
source share
1 answer

you "redefined" 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) used to indicate initialization when all your initializations in the user interface are executed, docs link for depth details

onCreate(Bundle, PersistableBundle) , since it itself assumes that it is used to constantly reuse the old intention (set for the first time for this activity) when it is recreated, you can enable reuse by adding the persistableMode flag to your activity so that the activity will saved during reboots. try this link for other options .

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

+9
source

Source: https://habr.com/ru/post/1259283/


All Articles