Pink Jazz
Pink Jazz

Reputation: 802

Android - Eclipse ignoring Java code, only runs from XML file

I am new to Android development in Eclipse, and for some reason, when trying to run my Java code, Eclipse ignores it and only runs the XML code.

Here is my Java code so far:

package com.example.boat;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ScrollView mainView = new ScrollView(this);
    LinearLayout mainLayout = new LinearLayout(this);
    LinearLayout results = new LinearLayout(this);

    mainView.addView(mainLayout);
    mainLayout.addView(results);
    results.setVisibility(View.GONE);

    TextView mhs = new TextView(this);
    mhs.setText("Maximum hull speed:");
    mainLayout.addView(mhs);

    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

Here is my XML code:

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

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

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

Does anyone know what is wrong here? I prefer to use Java, not XML.

Upvotes: 0

Views: 665

Answers (1)

codeMagic
codeMagic

Reputation: 44571

I'm not sure what you think isn't running but the only thing that will happen here is that your activity_main.xml will be inflated because of this line

 setContentView(R.layout.activity_main);

All of the Java code before this is just creating Views but not being added to the inflated View with setContentView(). Once you call setContentView() that is what will be displayed. If you want to set those Views on this layout then you need to do it after your call to setContentView() and you have to add them to that layout...not just on each other.

However, in this situation, I see no need to create those dynamically. Just add them to your activity_main.xml and take those out of onCreate().

Upvotes: 2

Related Questions