Trouble running simple app. - android

I'm new at this. Go easy.
My code so far looks like this.
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.hardware.Camera;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String fmode = "Not Supported";
Camera cam = Camera.open();
Camera.Parameters p = cam.getParameters();
if (p.getFlashMode() != null)
{fmode = p.getFlashMode();}
TextView tv = new TextView(this);
tv.setText(fmode);
setContentView(tv);
}
}
When I run the program, I get the message stating that The application has stopped unexpectedly. Please try again. If I comment out these four lines...
//Camera cam = Camera.open();
//Camera.Parameters p = cam.getParameters();
//if (p.getFlashMode() != null)
//{fmode = p.getFlashMode();}
then the code runs fine and I get the "Not Supported" message. Then if I uncomment the first line where I declare the Camera object, it crashes again.
Feel free to be verbose, I'm in learning mode and would like all the information I can get. Thanks in advance.

Any chance you missed adding the camera permission in your AndroidManifest?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
<application ...>
.
.
.
</application>
</manifest>
If that's not the case:
Why does the android emulator camera stop unexpectedly?

I'm not very familiar using a camera in apps but I found you a great tutorial that can help you on your way towards your solution.
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html
I hope this is helpful for you and may you find what you need :) if not, I'll make a test app myself and help you further
And like someone else stated above, some permissions must be added to use the camera, you can find those here:
http://developer.android.com/reference/android/hardware/Camera.html

Permissions are needed to allow access to certain features in android. Did you put <uses-permission android:name="android.permission.CAMERA" /> in your android manifest to have access to the camera.

Related

parsing an android manifest file with JDOM2

hope you're fine.
I have to parse an android manifest file to extract data like 'the minSdkVersion' used, after several seraches I found a code using JDOM.
Display data related to "uses-sdk" was expected but When running I got a null object.
Need Help
Thanks
Manifest Example
<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.rssfeed"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
The code
import java.io.File;
import java.util.Iterator;
import java.util.List;
import org.jdom2.*;
import org.jdom2.input.SAXBuilder;
public class ManifestP2 {
static Document document;
static Element root;
static NeededTag tags;//Data receiver
public static void main(String[] args){
SAXBuilder builder = new SAXBuilder();
try{
document = builder.build(new File("AndroidManifest.xml"));
}catch(Exception e){
System.out.println(e.getMessage());
}
root = document.getRootElement();
tags = new NeededTag();
findTag();
System.out.println(tags.usesSdk.toString());
}
static void findTag(){
//get values for "uses-sdk" tag
Element sdk = root.getChild("uses-sdk");
tags.usesSdk.minSdk = sdk.getAttributeValue("android:minSdkVersion");
tags.usesSdk.targetSdk = sdk.getAttributeValue("android:targetSdkVersion");
}
}
The attributes in your document are in the android namespace. Namespaces in XML add a level of complexity, that's true, but they also ensure that you can have special types of documents, like your manifest, where you can store "meta" types of information inside the same document as regular data, while still guaranteeing that there won't be any name collisions.
That's a complicated way of saying that where you see android in your XML document, it refers to a namespace, not the attribute name.
So, for example, where you have:
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="19" />
that really means you have your own element uses-sdk and that element has 2 attributes, one called minSdkVersion, and the other called targetSdkVersion. Those attributes are in the namespace with the URI: http://schemas.android.com/apk/res/android.
To get those attributes off the element in JDOM, you have to ask for them by name, and in the right namespace.
To do that, you have the code:
Namespace ans = Namespace.getNamespace("android", "http://schemas.android.com/apk/res/android");
String min = sdk.getAttributeValue("minSdkVersion", ans);
String target = sdk.getAttributeValue("targetSdkVersion", ans);
You declare the namespace to search for (only the URI is important when searching, the android namespace prefix is there for convenience only)

Air StageWebView Leaflet map images are jagged on android

i wanted to incororate a Leaflet Map into an ios and android air mobile application using the StageWebView class.
Unfortunately the quality of the map images is so jagged, that it is hard to make out the streetnames. There seems to be some minor scaling going on.
For test purposes i used the Leaflet tutorial mal at http://leafletjs.com/examples/quick-start-example.html
When viewed in the Android browser ( chrome ) the images look fine.
Here is some simple code to show the issue:
package {
import flash.display.MovieClip;
import flash.display.StageScaleMode;
import flash.display.StageAlign;
import flash.media.StageWebView;
import flash.geom.Rectangle;
import flash.desktop.NativeApplication;
import flash.utils.setTimeout;
public class Main extends MovieClip{
private var _stageWebView:StageWebView
public function Main()
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
setTimeout( init, 100 );
}
private function init():void {
_stageWebView = new StageWebView();
_stageWebView.stage = this.stage;
_stageWebView.viewPort = new Rectangle( 0, 0, stage.stageHeight, stage.stageWidth );
_stageWebView.loadURL( "http://leafletjs.com/examples/quick-start-example.html" );
}
}
}
Any ideas? Does it have to do with a resolution problem maybe?
Thanks
The problem that you got is a rendering problem which you can avoid by enabling hardware acceleration (force GPU rendering) when it's supported of course.
Beginning in Android 3.0 (API level 11), the Android 2D rendering pipeline supports hardware acceleration, meaning that all drawing operations that are performed on a View's canvas use the GPU.
So to force GPU rendering, you can :
1 - Enable hardware acceleration in your application's manifest file, where you should set the android:hardwareAccelerated attribute of the application element to true :
<android>
<manifestAdditions>
<![CDATA[
<manifest>
<application android:hardwareAccelerated="true"/>
</manifest>
]]>
</manifestAdditions>
</android>
And this is an example of a full manifest file (my test browser-app.xml) :
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<application xmlns="http://ns.adobe.com/air/application/19.0">
<id>browser</id>
<versionNumber>1.0.0</versionNumber>
<versionLabel/>
<filename>browser</filename>
<description/>
<name>browser</name>
<copyright/>
<initialWindow>
<content>browser.swf</content>
<systemChrome>standard</systemChrome>
<transparent>false</transparent>
<visible>true</visible>
<fullScreen>false</fullScreen>
<renderMode>auto</renderMode>
<autoOrients>true</autoOrients>
</initialWindow>
<icon/>
<customUpdateUI>false</customUpdateUI>
<allowBrowserInvocation>false</allowBrowserInvocation>
<android>
<manifestAdditions>
<![CDATA[
<manifest>
<uses-permission android:name="android.permission.INTERNET"/>
<application android:hardwareAccelerated="true"/>
</manifest>
]]>
</manifestAdditions>
</android>
</application>
then you have just to publish your AIR app, you will get something like this :
and you can see the result when I comment <application android:hardwareAccelerated="true"/> :
2 - You can also force GPU rendering from the Developer Options of your Android device :
and you can get the same result even when <application android:hardwareAccelerated="true"/> is commented :
That's all !
Hope that can help.
If you change it to use nativeMode new StageWebView(true) it solves the problem for me but the mouse/touch events don't work :/

AndroidPlot - XYplot doesn't show up on graphical layout in xml

I'm fairly new in the android developing scene, however I have done a few dead simple applications to get an understanding of what the workflow would be like in the environment.
I'm having a difficulty running the example program called SimpleXYPlotExample and here's the code, manifest, main.xml and activity.java, respectively. I know this is a user error of some sort, but it would be great if someone could give me some helpful pointers as to what I'm doing wrong.
At first, all the code was broken, but I realized i forgot to import the library in. Now that I have done so, I try to run the program on my nexus s and it just crashes as it enters it.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="6"
android:targetSdkVersion="16"/>
<application android:label="SimpleXYPlotExample"
android:icon="#drawable/icon"
android:debuggable="true"
android:hardwareAccelerated="false">
<activity android:name=".MyActivity"
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>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.androidplot.xy.XYPlot
android:id="#+id/mySimpleXYPlot"
android:layout_width="fill_parent"
android:layout_height="150px"
android:layout_marginTop="10px"
android:layout_marginLeft="10px"
android:layout_marginRight="10px"
andsroidplot.title="A Simple XYPlot Example"
androidplot.ticksPerRangeLabel="4"
androidplot.ticksPerDomainLabel="2"
androidplot.gridPadding="4dp|4dp|4dp|4dp"
/>
</LinearLayout>
package com.example;
import java.util.Arrays;
import android.app.Activity;
import android.os.Bundle;
import com.androidplot.series.XYSeries;
import com.androidplot.ui.AnchorPosition;
import com.androidplot.xy.LineAndPointFormatter;
import com.androidplot.xy.SimpleXYSeries;
import com.androidplot.xy.XLayoutStyle;
import com.androidplot.xy.XYPlot;
import com.androidplot.xy.YLayoutStyle;
public class MyActivity extends Activity
{
private XYPlot plot;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// initialize our XYPlot reference:
plot = (XYPlot) findViewById(R.id.mySimpleXYPlot);
// add a new series
XYSeries mySeries = new SimpleXYSeries(
Arrays.asList(0, 25, 55, 2, 80, 30, 99, 0, 44, 6),
SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "series1");
plot.addSeries(mySeries, new LineAndPointFormatter(
getApplicationContext(), R.xml.f1));
// reposition the domain label to look a little cleaner:
plot.position(plot.getDomainLabelWidget(), // the widget to position
45, // x position value, in this case 45 pixels
XLayoutStyle.ABSOLUTE_FROM_LEFT, // how the x position value is applied, in this case from the left
0, // y position value
YLayoutStyle.ABSOLUTE_FROM_BOTTOM, // how the y position is applied, in this case from the bottom
AnchorPosition.LEFT_BOTTOM); // point to use as the origin of the widget being positioned
plot.centerOnRangeOrigin(60);
plot.centerOnDomainOrigin(5);
}
}
This may be contributed by the fact that the plot doesn't even show up on the graphical layout. This is what it looks like with this code:
http://imgur.com/qYl96Zi
As you can see, I have imported the androidplot library. Please advise.
The screenshot appears to be from the eclipse gui editor, which as the exception mentions does not support the setShadowLayer method and thus will not render. That's just a limitation with Eclipse and not a bug in your code or the library.
Normally to be able to answer questions regarding why Androidplot is crashing you'd need to supply the corresponding stack trace. I did however notice this typo in your xml:
andsroidplot.title="A Simple XYPlot Example"
As far as getting a working WYSIWYG editor my personal suggestion would be to give either IntelliJ IDEA or Android Studio (built on the current EAP version IntelliJ IDEA) a try. Both are free and both are in (IMHO) far superior IDE's.

ZXing QR Reader Direct Integration android

Want to develop a QR code reader.. which will customized for my application. after a lot of search i found a link http://www.androidaz.com/development/zxing-qr-reader-direct-integration this tutorial demonstrate what i exactly want. but when i import it and then run this app i notice that its camera is in 90 degree angle when i rotate device. what is the problem i can not realize. my main.xml is
<FrameLayout
android:layout_width="200dip"
android:layout_height="200dip"
android:layout_gravity="center_horizontal">
<include layout="#layout/capture"/>
</FrameLayout>
my mainactivity file is:
public class ScannerActivity extends CaptureActivity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qrcode);
}
#Override
public void handleDecode(Result rawResult, Bitmap barcode)
{
Toast.makeText(this.getApplicationContext(), "Scanned code " + rawResult.getText(), Toast.LENGTH_LONG).show();
}
}
menifest file with permission :
<uses-permissionandroid:name="android.permission.CAMERA"/>
<uses-permissionandroid:name="android.permission.INTERNET"/>
<uses-permissionandroid:name="android.permission.VIBRATE"/>
<uses-permissionandroid:name="android.permission.FLASHLIGHT"/>
<uses-permissionandroid:name="android.permission.READ_CONTACTS"/>
<uses-permissionandroid:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS"/>
<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permissionandroid:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permissionandroid:name="android.permission.ACCESS_WIFI_STATE"/>
it can read QR code fine. only problem camera caused abnormal behavior when rotating..
Thanks in advanced.
Maybe what you would like to do is here:
android-zxinglib
An android library project of zxing BarcodeScanner
https://code.google.com/p/android-zxinglib/
Download the project and look these files:
AndroidManifest.xml
capture.xml
Go into your manifest and change the orientation to landscape. Portrait caused the same problem for me, and landscape looked a lot better.

Unable to resolve activity for: Intent

I am having a problem in running Android unit test. I got this error when I tried to run a simple test.
Here's the log:
Blockquote
java.lang.RuntimeException: Unable to resolve activity for: Intent { act=android.intent.action.MAIN flg=0x10000000 cmp=com.wsandroid.Activities/.SplashActivity }
at android.app.Instrumentation.startActivitySync(Instrumentation.java:371)
at android.test.InstrumentationTestCase.launchActivityWithIntent(InstrumentationTestCase.java:120)
at android.test.InstrumentationTestCase.launchActivity(InstrumentationTestCase.java:98)
at android.test.ActivityInstrumentationTestCase2.getActivity(ActivityInstrumentationTestCase2.java:87)
at com.wsandroid.test.activity.TestEULA.setUp(TestEULA.java:15)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:430)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)
This error occurs for Android less than 2.2. It works fine for Android 2.2 emulator. Yet Android 2.2 emulator has a bug of sending a key twice even though we only press it one. Application to be tested runs on Android 2.2 platform.
Appreciate if anyone of you can help me.
Dzung.
This can also be cause by a missing
Make sure you have a corresponding entry in your manifest.
<activity android:name=".SplashActivity" ...
I had a similar problem with a simple test project for an app that was just a splash screen. I found that I had implemented the constructor wrong. My initial implementation of the constructor was this...
public SplashScreenTest(){
super("com.mycomp.myapp.SplashScreen", SplashScreen.class);
}
After some beating my head against the wall, I somehow decided to remove the SplashScreen from the pkg argument of super(). My successful implementation is now like this...
public SplashScreenTest() {
super("com.mycomp.myapp", SplashScreen.class);
}
I hope that this helps you or others solve the problem.
Try to check your Manifest.xml file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tablet.test"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<uses-library android:name="android.test.runner" />
</application>
<uses-sdk android:minSdkVersion="8" />
<!-- This line below! -->
<instrumentation android:targetPackage="com.tablet.tablet"
android:name="android.test.InstrumentationTestRunner" />
</manifest>
You need to check the following line:
<instrumentation android:targetPackage="com.tablet.tablet"
android:name="android.test.InstrumentationTestRunner" />
So the targetPackage must be the same as in your code.
I had specific similar problem while using the AndroidAnnotations lib.
Later, I found out it was due to forgetting to use the generated class (MyActivity_ instead of MyActivity).
In my case the problem was that TestFragmentActivity, meaning the Activity used in our test
extends ActivityInstrumentationTestCase2<TestFragmentActivity>
must be available in the package defined in Manifest.xml as targetPackage:
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="de.my.androidhd" />
My solution was to move TestFragmentActivity into tested application package.
For the keys being sent twice issue, are you sure you're not now getting both the Down and Up actions? I had this issue when using Robotium, and generated this to make things easier:
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.widget.EditText;
import com.jayway.android.robotium.solo.Solo;
public static void type(Solo robot, EditText edit, String text) {
int index = 0;
//Find the index of this control, as Robotium doesn't seem to like R.id
for (int i = 0; i < robot.getCurrentEditTexts().size(); i++) {
if (robot.getCurrentEditTexts().get(i).getId() == edit.getId()) {
index = i;
}
}
robot.clickOnEditText(index);
KeyCharacterMap map = KeyCharacterMap.load(KeyCharacterMap.BUILT_IN_KEYBOARD);
KeyEvent[] events = map.getEvents(text.toCharArray());
for (int event = 0; event < events.length; event++) {
if (events[event].getAction() == KeyEvent.ACTION_DOWN) {
robot.sendKey(events[event].getKeyCode());
}
}
}
I've had two activities with same name in different packages. Issue was about importing from the wrong package. I spend much time on it maybe it will save someone some time.

Categories

Resources