我只想用一个简单的按钮切换 Activity
Button test = findViewById(R.id.please);
test.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("MainActivity","I pass there ");
Intent intent = new Intent(MainActivity.this, TestActivity.class);
startActivity(intent);
Log.d("MainActivity","I pass there too");
}
});
我没有错误,我只是到达白屏,而不是我的新 Activity 。日志出现在我的控制台中。当我创建第二个 Activity 时,我也有一个日志,但这个没有出现。
欲了解更多代码, 这是我的 list :
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".HomeActivity">
</activity>
<activity android:name=".ScheduleActivity"></activity>
<activity android:name=".MapActivity"></activity>
<activity android:name=".TestActivity"
android:theme="@style/YourTheme">
</activity>
</application>
测试 Activity :
public class TestActivity extends AppCompatActivity {
@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.activity_test);
Log.d("TestActivity","I arrived in Test Activity !!");
}
}
activity_main 中的按钮:
</android.support.design.widget.AppBarLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="wtf"
android:background="@color/colorAccent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="@+id/please"
/>
Activity 测试:
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_map_black_24dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello there! :D"/>
最佳答案
您在 TestActivity 中重写了错误的 onCreate 方法。您必须重写此方法:
protected void onCreate(@Nullable Bundle savedInstanceState) {}
关于java - 当我尝试在 Android 应用程序上切换 Activity 时,我出现白屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53133297/