从片段按钮单击(意图)启动活动会抛出空白屏幕

时间:2015-07-21 20:01:41

标签: android android-fragments android-intent android-activity android-button

我正在尝试从放置在片段中的按钮开始活动,但是当我运行应用程序时(在模拟器和真实设备中)并按下该按钮,活动无法启动,空白屏幕出现导航栏。我已经阅读了很多关于此的内容,而人们最常犯的错误是他们没有宣布活动,或者他们没有夸大正确的布局。任何想法都会受到赞赏,因为我现在不想做什么。

以下是清单的代码:

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

以下是片段布局的代码:

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

片段代码:

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;
    }
}

新活动布局的代码:

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

新活动的代码:

public class NewActivity extends Activity{

@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
    super.onCreate(savedInstanceState, persistentState);
    setContentView(R.layout.newactivity_layout);
    }
}

1 个答案:

答案 0 :(得分:2)

你的最低版本是什么?您的onCreate版本在21中引入:stackoverflow.com/a/29871359/1541763从参数中删除PersistableBundle persistentState