1

I'm trying to start an activity from a button which is placed in a fragment, but when I run the app (in emulator and in a real device) and I press that button, the activity doesn't start and a blank screen whithout navigation bar appears. I've read a lot about this, and the most common error people had is that they didn't declare the Activity or they weren't inflating the correct layout. Any idea would be appreciated, because I don't now what to do right now.

Here is the code of the manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.isa.example" >

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/Theme.CustomMaterial" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".NewActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/Theme.CustomMaterial" >
        <intent-filter>
            <action android:name="com.isa.example.NewActivity" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

Here is the code for the fragment layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Start new activity"/>

</RelativeLayout>

Code for the fragment:

public class Fragment1 extends Fragment {

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_fragment1, container, false);

    Button button = (Button) view.findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(getActivity(), NewActivity.class);
            startActivity(i);
        }
    });

    return view;
    }
}

Code for the new activity layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="This is new activity"
    android:id="@+id/textView"
    android:layout_gravity="center_horizontal" />
</LinearLayout>

And code for the new activity:

public class NewActivity extends Activity{

@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
    super.onCreate(savedInstanceState, persistentState);
    setContentView(R.layout.newactivity_layout);
    }
}
Isaías
  • 491
  • 2
  • 9
  • 20
  • your fragment-layout xml doesn't contain any `fragment`. – Aman Grover Jul 21 '15 at 20:16
  • What is your minimum sdk version? Your version of `onCreate` is introduced in 21: http://stackoverflow.com/a/29871359/1541763 Remove the `PersistableBundle persistentState` from your parameters – TBridges42 Jul 21 '15 at 20:23
  • @Aman Grover The fragment layout is the layout for the fragment itself. It doesn't need a fragment tag. Fragment tags are for layouts that contain fragments, not that are fragments. Although a fragment can contain fragments. Also, he says that the button is displaying correctly. – TBridges42 Jul 21 '15 at 20:24
  • @TBridges42 thank you, that worked for me :) – Isaías Jul 24 '15 at 11:45
  • I added it as an answer, if you would be so kind as to accept it since it helped you. – TBridges42 Jul 24 '15 at 16:52

1 Answers1

2

What is your minimum sdk version? Your version of onCreate is introduced in 21: stackoverflow.com/a/29871359/1541763 Remove the PersistableBundle persistentState from your parameters

TBridges42
  • 1,849
  • 1
  • 19
  • 29