我认为我做的一切都正确但是当我点击按钮时,我得到一个空白页而不是带有文本的页面。我想在单击主xml上的按钮后出现creation.xml。我错了什么?
我的主要活动xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="@drawable/creeper" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/hello"
android:textSize="20sp" />
<Button
android:id="@+id/button1"
android:layout_width="200dp"
android:layout_height="60dp"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:textSize="20sp"
android:text="@string/button1_text" />
</RelativeLayout>
我的主要活动java
package com.berkcoop.deneme;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
Button button1, button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent1 = new Intent(MainActivity.this, Creation.class);
startActivity(intent1);
}
});
}
我的创作xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="Creations"
android:textSize="25sp"
android:gravity="center"/>
</LinearLayout>
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.berkcoop.deneme"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.berkcoop.deneme.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.berkcoop.deneme.Creation"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
和我的创作java
package com.berkcoop.deneme;
import android.app.Activity;
public class Creation extends Activity{
@Override
public void setContentView(int layoutResID) {
// TODO Auto-generated method stub
super.setContentView(R.layout.creation);
}
}
谢谢..
答案 0 :(得分:1)
为什么要覆盖Creation类中的setContentView
方法?由于您没有调用它,因此不会设置正确的视图。
您应该从setContentView
致电onCreate
,据我所知,无需覆盖setContentView
。
在Creation中尝试:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.creation);
}