Categories
discuss

Layout definition. “You must supply a layout_width attribute.”

I’m developing a little android app, and I’m having a problem with a layout, I been trying to find the error in my xml, but I couldn’t find it…

The error I’m getting is “You must supply a layout_width attribute”, but I did it and it still doesn’t work…

Here is my XML file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="100" >

    <TextView android:id="@+id/nombreEvento"
        android:layout_height="fill_parent"
        android:layout_weight="70"
    />

    <TextView android:id="@+id/moneda"
        android:layout_height="fill_parent"
        android:layout_weight="10"
    />

    <TextView android:id="@+id/totalEvento"
        android:layout_height="wrap_content"
        android:layout_weight="20"
    />
    </LinearLayout>

<TextView android:id="@+id/fecha"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
/>

</LinearLayout>

Answer

Nope all of your TextViews have layout_height and layout_weight instead of layout_height and layout_width (and possibly layout_weight). Try this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="100" >

    <TextView android:id="@+id/nombreEvento"
        android:layout_height="fill_parent"
        android:layout_width="0dp"
        android:layout_weight="70"
    />

    <TextView android:id="@+id/moneda"
        android:layout_height="fill_parent"
        android:layout_width="0dp"
        android:layout_weight="10"
    />

    <TextView android:id="@+id/totalEvento"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="20"
    />
    </LinearLayout>

<TextView android:id="@+id/fecha"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
/>

</LinearLayout>
Categories
discuss

Getting the last number in a string (JavaScript)

var str = "7-Dec-1985" var str = "12-Jan-1703" var str = "18-Feb-1999"

How would I got about pulling just the year out of the string? I have tried a number of different RegExp but none seem to be working.

I would have expected re = new RegExp(/(d+)D*z/); To have worked but sadly it did not.

Any suggestions would be very appreciated

Answer

Since all of your str(s) use - as a separator, this will work for you:

var str = "7-Dec-1985",
    arr = str.split('-'),
    year = arr[2];

console.log(year);
Categories
discuss

SimpleDateFormat ParseException: Unparseable date Error

I’m parsing this date format from XML:=> “2011-12-06T07:41:14.016+00:00“, and I’m getting this error:

  • W/System.err(574): java.text.ParseException: Unparseable date: “2011-12-06T07:41:14.016+00:00”

I’m certain it’s the formatting statement I’m using, but I can’t figure out what it SHOULD be…

Here’s the statement I’m using:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ:ss");

I get how to create a format for this part: “2011-12-06T07:41:14….”, it’s this other part :=> “.016+00:00” that’s throwing me for a loop.

I’ve looked for answers here already: Android SimpleDateFormat Page, and here Oracle SimpleDateFormat Page, but I fear I’m missing something fundamental….

Do you have any suggestions on a proper format statement for that particular date format, or pertinent resources to peruse?

Big Thanks in advance!

Answer

The “Z” pattern matches +0000 and not +00:00 so if you remove the last “:” before you parse then it will work.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ");
try {
  Date myDate = sdf.parse( "2011-12-06T07:41:14.016+00:00".replaceAll( "([0-9\-T]+:[0-9]{2}:[0-9.+]+):([0-9]{2})", "$1$2" ) );
  System.out.println( myDate );
} catch (ParseException e) {
  e.printStackTrace();
}
Categories
discuss

Eclipse says my Android project(s) contain(s) errors, but there aren’t any

Since last night, Eclipse hasn’t been letting me run any of my projects and gives me the error “Your project contains error(s), please fix it before running it”.

  • This happens regardless of which project I use.
  • There are no code errors in any projects
  • There are no missing files (R.java, etc.) or folders (src, res, etc.)
  • There are no errors in the console (even on verbose) or the error log screen
  • I’ve cleaned all the projects, and each individual project many times
  • I’ve restarted ADB, Eclipse, and my computer

I have no idea what to do next, so hopefully its fixable.
NOTE: I am using Git, and this occurred after switching to my master branch to export a new update to one of my apps.

Thank you in advance! (:

Edit 1: I have also tried deleting and re-importing the projects before I posted this.

Answer

Click Window -> Show View -> Problems, that should show what exactly Eclipse thinks is wrong with your project. Sometimes you can simply delete the problem from that view, and Eclipse believes you everything is fine (yep, weird, I know).

Also, after you switch branches using an external source control tool (one not integrated into Eclipse through a plugin), Eclipse sometimes won’t pick up all changes, you’d need to manually refresh the whole project file tree.

If nothing else helps, try setting up a new Eclipse workspace and importing your project there.

Categories
discuss

Sending data from node.js to Java using sockets

I’m trying to send data from node.js to Java through sockets. I searched around but nothing was really useful. I’m used to socket.io but in this case it doesn’t seem really suitable for this. It seems like all the socket extensions for node.js are not really suited for sending messages, but rather listening to messages and answering something.

My Java app basically should receive some work to do from node.js, do the work and send some result to node.js back. And no, the work cannot be done on node.js, it has to be done by Java (which actually is Scala but whatever).

Does anyone of you know how can I do something like this?

Thanks

Answer

You can use the build in socket in node.js to do something like that (very easy both in java and node.js, but you’ll get the point) :

Java :

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class Test {

    public static void main(String[] args) {
        ServerSocket server;
        Socket client;
        InputStream input;

        try {
            server = new ServerSocket(1010);
            client = server.accept();

            input = client.getInputStream();
            String inputString = Test.inputStreamAsString(input);

            System.out.println(inputString);

            client.close();
            server.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String inputStreamAsString(InputStream stream) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(stream));
        StringBuilder sb = new StringBuilder();
        String line = null;

        while ((line = br.readLine()) != null) {
            sb.append(line + "n");
        }

        br.close();
        return sb.toString();
    }

}

Node.js :

var net = require('net');

var client = net.connect(1010, 'localhost');

client.write('Hello from node.js');

client.end();

And the link to the node.js doc about sockets : http://nodejs.org/docs/latest/api/net.html

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..