0

「Beginning Android Games」本 (Mario Zechner、Apress.com) から最初の例を実行しようとしました: このコードを自分のプロジェクト (MainActivity.java ファイル) にコピーしました:

HelloWorldActivity.java

package com.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity implements View.OnClickListener {

    Button button;
    int touchCount;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    button = new Button(this);
    button.setText( "Touch me!" );
    button.setOnClickListener(this);
    setContentView(button);
}

public void onClick(View v) {
    touchCount++;
    button.setText("Touched me " + touchCount + " time(s)");
}
}

しかし、この単純なアプリケーションを実行すると、エミュレーターにボタンが表示されませんでした (「Android」ラベルしか表示されませんでした。「project-->clean..」を試しましたが、結果はありませんでした。(そして「project-このボタンはどこで宣言して記述すればよいのでしょうか? しかし、本に書かれている手順を踏襲し、最後にレイアウト フォルダから xml ファイルを完全に削除しました。

ありがとう。 _ _ _ _ _ _ _ _ _ _ _編集: xml ファイルを追加_ _ _ __ _ ____

res-->layout-->activity_main.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"
    tools:context=".MainActivity" >


</RelativeLayout>

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.firstbuttontest2"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="4"
        android:targetSdkVersion="11" />

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

</manifest>
4

3 に答える 3

1

これを試してみてください。

ボタンを特定のレイアウトに追加します。このような

LinearLayout linearLayout=(LinearLayout)findViewById(R.id.linearLayout);
linearLayout.addView(button);
于 2013-01-02T06:35:34.600 に答える
0

setContentViewとは対照的に、あなたが探しているものよりも多いかもしれないaddContentViewメソッドがあることを私は知っています。

于 2013-01-02T06:42:12.550 に答える