我是Android的初学者,正在尝试运行Hello,但是当模拟器出现时,只显示Android图像。我的输出“你好世界”从未出现过。
MainActivity.class
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView text = new TextView(this);
text.setText("Hello World, Android");
setContentView(text);
// setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}当模拟器弹出时,我可以等待5-10分钟,但是输出却没有显示出来。我没有在任何XML文件中更改任何内容。我在试着测试“你好世界”程序。
我在使用Eclipse。
编辑
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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world"
tools:context=".MainActivity" />
</RelativeLayout>androidManifest.XML
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>发布于 2012-08-31 07:35:06
您是否将Activity添加到AndroidManifest文件中?
示例
<application android:label="@string/app_name"
android:icon="@drawable/ic_launcher">
<activity android:name=".YourActivity">
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>更新
变化
setContentView(text)至
setContentView(R.layout.activity_main) 这意味着您将使用预定义的XML文件作为此Activity的布局。
如果您想要更改xml中的TextView:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// find the TextView in the XML file defined by setContentView
TextView text = (TextView) findViewById(R.id.mTextView);
text.setText("Your text");
}不要忘记将`android:id="@+id/mTextView“属性添加到XML中的TextView中。
发布于 2012-08-31 07:38:26
您必须编辑manifest.xml:
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>发布于 2012-08-31 07:54:09
我建议您“改进”您的MainActivity:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //The XML where the text view is
TextView text = (TextView) findViewById(R.id.TEXTVIEWID); // The id of the text view
text.setText("Hello World, Android");
}
}那么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" >
<TextView
android:id="@+id/TEXTVIEWID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>对于基础知识,您只需遵循这教程即可!
https://stackoverflow.com/questions/12210797
复制相似问题