我开始在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>
答案 0 :(得分: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>
取决于您的应用