Categories
discuss

Using querySelector with IDs that are numbers

From what I understand the HTML5 spec lets you use IDs that are numbers like this.

<div id="1"></div>
<div id="2"></div>

I can access these fine using getElementById but not with querySelector. If I try do the following I get SyntaxError: DOM Exception 12 in the console.

document.querySelector("#1")

I’m just curious why using numbers as IDs does not work querySelector when the HTML5 spec says these are valid. I tried multiple browsers.

Answer

It is valid, but requires some special handling. From here: http://mathiasbynens.be/notes/css-escapes

Leading digits

If the first character of an identifier is numeric, you’ll need to escape it based on its Unicode code point. For example, the code point for the character 1 is U+0031, so you would escape it as 00031 or 31 .

Basically, to escape any numeric character, just prefix it with 3 and append a space character ( ). Yay Unicode!

So your code would end up as (CSS first, JS second):

#31  {
    background: hotpink;
}

document.getElementById('1');
document.querySelector('#\31 ');
Categories
discuss

To check or not to check “IsLibrary”?

I built my app using a library project (that I also created, to be reused in other apps). It builds fine, but when I try to install & run it through Eclipse (Ctrl+F11), I get this red Could not find MyLib.apk! error line in my console:

Console output:

[2013-11-30 14:42:59 - MyApp] ------------------------------
[2013-11-30 14:42:59 - MyApp] Android Launch!
[2013-11-30 14:42:59 - MyApp] adb is running normally.
[2013-11-30 14:42:59 - MyApp] Performing com.sfinja.myapp.ActivitySubClass activity launch
[2013-11-30 14:42:59 - MyApp] Automatic Target Mode: using device '5752068FC64500EA'
[2013-11-30 14:43:00 - MyApp] Application already deployed. No need to reinstall.
[2013-11-30 14:43:00 - MyLib] Could not find MyLib.apk!
[2013-11-30 14:43:00 - MyApp] Starting activity com.sfinja.myapp.ActivitySubClass on device 5752068FC64500EA
[2013-11-30 14:43:00 - MyApp] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.sfinja.myapp/.ActivitySubClass }

So I searched SO and found this thread which solves the problem by simply unchecking the library project’s Is Librarycheckbox.

Sure enough, my Library Project (MyLib) had this checkbox checked so I unchecked it and I no longer receive this error in the console, but then I started getting runtime NullPointerExceptions.

This clearly suggests that I have a problem in my project setup/configuration (although it builds fine) but in order to find what it is, I believe I need to understand a bit more about this confusing checkbox:

  1. I have other library projects used in this app, all having the Is Library checkbox checked but none of them presents the problem exhibited by MyLib. So, to check or not to check “IsLibrary”?
  2. If it is a library, why should the Is Library checkbox be unchecked? Is that thread’s correct answer from 2 years ago still valid?
  3. Is the Could not find APK! essentially a “red herring” for something else?

Answer

I recall encountering a problem similar to yours that drove me nuts. I also thought that unchecking the projects Is Library checkbox would solve the problem but in actuality it didn’t.

After lots of lost hours I then found the culprit: An extra, redundant, wicked and totally misleading and destructive line in the project’s .classpath:

<classpathentry kind="src" path="/RogueProjLib"/>

(I had no idea how it snuck in)

Once I removed it, the the “Could not find RogueProjLib.apk!” went away and project installed and ran fine, with the Is Library checkbox checked!

Based on that, I would answer your questions as follows:

  1. Your other library projects aren’t probably in that .classpath… Yes, do check Is Library.
  2. Question irrelevant because it should be checked. The answer from 2 years ago is no longer relevant with the latest Eclipse/ADT bundle.
  3. Yes, it is a red herring: Instead of telling you “there is a weird line in my .classpath with which I don’t know what to do”, it just emits that cryptic “Could not find APK” message.

Hope this helps.

Categories
discuss

How can a servlet do a hot re-deploy of itself

Is there a way for a Servlet to do a hot re-deploy the webapp it is part of?

I know that I can configure Tomcat to do a hot re-deploy on changes to web.xml but I can’t find any way to do the same from java code. I don’t want tomcat to having to watch the web.xml for changes all the time.

Answer

I believe it can be done using JMX.

The Tomcat Manager is a web application that can be used interactively (via HTML GUI) or programmatically (via URL-based API) to deploy and manage web applications.

There is the link – http://tomcat.apache.org/tomcat-7.0-doc/deployer-howto.html

Categories
discuss

Navigating back to parent activity in ActionBar without handling Up button event

I have two activities that I want this navigation to happen, they are VendorsActivity and QuestionsActivity. The following how my AndroidManifest.xml looks like:

(I am not using the full name of my activities like com.hello.world.MyActivity as I am defined package attribute in manifest node.)

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".VendorsActivity"
        android:label="@string/vendors_activity_title" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".TestsActivity"
        android:label="@string/tests_activity_title"
        android:parentActivityName=".VendorsActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".VendorsActivity" />
    </activity>
    <activity
        android:name=".QuestionsActivity"
        android:label="@string/questions_activity_title" >
    </activity>
</application>

And in TestsActivity, I am calling getActionBar().setDisplayHomeAsUpEnabled(true); method from within onCreate method.

The problem is, it won’t work unless I implement the following method in .TestsActivity class:

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    switch (item.getItemId())
    {
    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(TestsActivity.this);
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

But Android Developer Guide says that I don’t have to handle the Up button’s event as mentioned at the very bottom of hit page: https://developer.android.com/training/basics/actionbar/adding-buttons.html

Because the system now knows MainActivity is the parent activity for DisplayMessageActivity, when the user presses the Up button, the system navigates to the parent activity as appropriate—you do not need to handle the Up button’s event.

Edit and Answer:

As Android Developer Guide says:

Beginning in Android 4.1 (API level 16), you can declare the logical parent of each activity by specifying the android:parentActivityName attribute in the element.

If your app supports Android 4.0 and lower, include the Support Library with your app and add a element inside the . Then specify the parent activity as the value for android.support.PARENT_ACTIVITY, matching the android:parentActivityName attribute.

So I think my problem was because of two reasons:

  1. Running the app on a proper emulator. I was targeting a higher version but the emulator was running on API 14 (Android 4.0) so it didn’t know how to handle android:parentActivityName attribute.

  2. Targeting the right API level in Project Build Target properties as shown below:

enter image description here

Answer

android:parentActivityName attribute supported only after API level.

One alternative is using support-library:v7 combined with NavUtils.

There is a great training material about this topic (include compatibility issue). please check – http://developer.android.com/training/implementing-navigation/ancestral.html

That being said, i am posting my code below because it is working :-

MainActivity.Java

package com.example.activitydatasend;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button click = (Button) findViewById(R.id.click);

        click.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(MainActivity.this,Second.class);
                intent.putExtra("first", "first");
                intent.putExtra("second", "second");
                startActivity(intent);

            }
        });
    }

    @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;
    }

}

SecondActivity.java

package com.example.activitydatasend;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class Second extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        Bundle bundle = getIntent().getExtras();
        String a = bundle.getString("first");
        String b = bundle.getString("second");
        System.out.println(a+b);
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @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;
    }

}

AndroidManifext.xml

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

    <uses-sdk
        android:minSdkVersion="11"
        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.activitydatasend.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>
        <activity
            android:name="com.example.activitydatasend.Second"
            android:label="@string/app_name"
            android:parentActivityName="com.example.activitydatasend.MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

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">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/click"
        android:text="Click" />

</RelativeLayout>

second.xml

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Second Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>
Categories
discuss

Simulate Android SMS/Call on USB device

I’ve seen already that ddms allows to send fake SMS messages or init fake calls on Android emulator. However I am not using emulator for development, but only real device connected with USB cable.

In developer preferences on my device I can allow to simulate SMS messages. Everything I know about that is to connect to the device (emulator) with telnet:

telnet localhost 5554

and send SMS message with

sms send 12345 Hello SO!

I cannot connect with telnet to the USB device – it just yels connection refused.

I had forwarded ports with adb, got into device using telnet, but after calling sms send ... it shows that sms was not found.

Back to the merits of the SMS simulating – is it even possible to send any with adb/shell from PC to the USB device ?

Answer

It seems from the developer site that there is currently no support for USB connections See this link : SMS Emulation under Emulator Limitations

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