我只想用一个简单的按钮切换活动
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");
}
});
我没有错误,我只是在白屏上到达,而没有参加新活动。日志显示在我的控制台中。创建第二个活动时,我也有一个日志,但此活动没有出现。
有关更多代码, 这是我的清单:
<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>
TestActivity:
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"
/>
活动测试:
<?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"/>
答案 0 :(得分:0)
您在TestActivity中重写了错误的onCreate方法。您必须重写此方法:
protected void onCreate(@Nullable Bundle savedInstanceState) {}