0

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.

5
  • those are two absolutely unrelated code. Commented Oct 15, 2013 at 18:08
  • What do u mean ignores it ? Commented Oct 15, 2013 at 18:10
  • I mean that when I build and run my project, I don't see my TextView that I have in my Java code. Commented Oct 15, 2013 at 18:12
  • I have noticed if you have the XML page showing in the Eclipse editor when you try to run the application, it tries to run the xml page instead of the application. Make sure the java code is showing in the window. Commented Oct 15, 2013 at 18:14
  • Tow things to understand. XML is not code, it's just data and you cannot "run" XML, just like I can't run a PDF. Commented Oct 15, 2013 at 18:19

1 Answer 1

2

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().

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that worked. I simply replaced this line with setContentView(mainView)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.