为什么没有按钮改变布局?

时间:2013-11-17 21:12:53

标签: android button layout user-interface

我想按一个按钮,然后显示新的布局。

我做错了什么?

我的类名是:StarWars.java和Test1.java 布局名称:activity_star_wars-.xml和test1.xml

我的代码:

package de.starwars;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_star_wars);

Button jm = (Button) findViewById(R.id.btxJediMaster);
jm.setOnClickListener(new View.OnClickListener()
{

@Override
public void onClick(View v)
{
    // TODO Auto-generated method stub
    Intent i = new Intent (StarWars.this,Test1.class);
        startActivity(i);       
}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_star_wars, menu);
    return true;
}  
}

清单:

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="de.starwars.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>
</application>

1 个答案:

答案 0 :(得分:0)

替换它:

jm = (Button) findViewById(R.id.btxJediMaster);
    jm.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            Intent activityChangeIntent = new Intent(StarWars.this,Test1.class);
            StarWars.this.startActivity(activityChangeIntent);

        }
    });

manifest

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
    android:name="de.starwars.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="de.starwars.Test1"/>
</application>