[英]Starting activity from fragment button click (intent) throws blank screen
我試圖從放置在片段中的按鈕開始活動,但是當我運行該應用程序(在仿真器和真實設備中)並按下該按鈕時,該活動無法啟動,並且沒有黑屏導航欄出現。 我已經閱讀了很多關於此的內容,人們最常見的錯誤是他們沒有聲明Activity或沒有誇大正確的布局。 任何想法都會受到贊賞,因為我現在不知道該怎么辦。
這是清單的代碼:
<?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);
}
}
您的最低SDK版本是什么? 您的onCreate版本在21中引入:stackoverflow.com/a/29871359/1541763從參數中刪除PersistableBundlepersistentState
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.