I’m a beginner in Android and am trying to run the Hello World but when the Emulator shows up, only the Android image is shown. My output “Hello World” never shows up.
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;
}
}
When the Emulator pops up i can wait for 5-10mins but yet the output never shows up. I haven’t change anything in any of the XML files. Am trying to test the “hello world” program.
I am using Eclipse.
Edit
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>
Did you add your
Activityto theAndroidManifestfile?Example
Update
Change
to
This means you will use your predefined XML file as the layout for this
Activity.If you want to change the TextView inside your xml:
Do not forget to add the attribute `android:id=”@+id/mTextView” to the TextView in your XML.