0

我开始从事 android 开发,我从一本书中获取了一个示例代码片段,用于界面上的几个文本字段。我一遍又一遍地检查我的代码与书中的代码相同,但是每次编译和运行时,我都只得到模拟器中的黑屏。

这是我的代码:

package me.kevinossia.mystuff;

import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity
{
    private LinearLayout nameContainer;
    private LinearLayout addressContainer;
    private LinearLayout parentContainer;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        createNameContainer();
        createAddressContainer();
        createParentContainer();
        setContentView(parentContainer);
    }

    private void createNameContainer()
    {
        nameContainer = new LinearLayout(this);

        nameContainer.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        nameContainer.setOrientation(LinearLayout.HORIZONTAL);

        TextView nameLbl = new TextView(this);

        nameLbl.setText("Name: ");
        nameContainer.addView(nameLbl);

        TextView nameValueLbl = new TextView(this);
        nameValueLbl.setText("Kevin");

        nameContainer.addView(nameValueLbl);
    }

    private void createAddressContainer()
    {
        addressContainer = new LinearLayout(this);

        addressContainer.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        addressContainer.setOrientation(LinearLayout.VERTICAL);

        TextView nameLbl = new TextView(this);

        nameLbl.setText("Address: ");
        addressContainer.addView(nameLbl);

        TextView nameValueLbl = new TextView(this);

        nameValueLbl.setText("26662");
        addressContainer.addView(nameValueLbl);
    }

    private void createParentContainer()
    {
        parentContainer = new LinearLayout(this);

        parentContainer.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        parentContainer.setOrientation(LinearLayout.VERTICAL);

        parentContainer.addView(nameContainer);
        parentContainer.addView(addressContainer);
    }
}

我在这里想念什么?

这是我的清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="me.kevinossia.mystuff.tutorial"
    android:versionCode="1"
    android:versionName="1.0">

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

    <application android:label="@string/app_name"
        android:icon="@drawable/ic_launcher"
        android:theme="@style/AppTheme">

    </application>

</manifest>

这是新的清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="me.kevinossia.mystuff.tutorial"
android:versionCode="1"
android:versionName="1.0">

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

<application android:label="@string/app_name"
    android:icon="@drawable/ic_launcher"
    android:theme="@style/AppTheme">
    <activity
        android:name="me.kevinossia.mystuff.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" android:theme="@android:style/Theme.Holo" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
 </application>

4

1 回答 1

0

你没有任何东西作为你的启动器manifest。你应该有类似的东西

  <application android:label="@string/app_name"
    android:icon="@drawable/ic_launcher"
    android:theme="@style/AppTheme">
    <activity
        android:name="me.kevinossia.mystuff.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" android:theme="@android:style/Theme.Holo" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
      </activity>

<application>标签内。这些是重要的线路。

      <intent-filter>
            <action android:name="android.intent.action.MAIN" >

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

取决于您的应用

清单.xml

意图过滤器

于 2013-07-15T23:07:42.873 回答