简体   繁体   English

从片段按钮单击(意图)开始活动将引发空白屏幕

[英]Starting activity from fragment button click (intent) throws blank screen

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. 我已经阅读了很多关于此的内容,人们最常见的错误是他们没有声明Activity或没有夸大正确的布局。 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);
    }
}

What is your minimum sdk version? 您的最低SDK版本是什么? Your version of onCreate is introduced in 21: stackoverflow.com/a/29871359/1541763 Remove the PersistableBundle persistentState from your parameters 您的onCreate版本在21中引入:stackoverflow.com/a/29871359/1541763从参数中删除PersistableBundlepersistentState

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM