Hi guys,
I am writing a program which has to display content provided by a web platform. Therefore, I split the program up into four classes. The "Main" class (Activity) just initializes the Controller "AppScheduler" (Intent) which then initializes the "PlatformConnector" (Service) and the "UI" (Activity).
Since I am a newbie in programming apps for Adroid, I am having trouble with getting setup a view in the UI. In case I define a view there, the app returns an exception. Does anyone of you knows what I am doing wrong?
Main Class:
public class Main extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppScheduler controller = new AppScheduler(this);
controller.Initialize();
// everything else happens in AppScheduler class
}
}
-------------------------
AppScheduler Class:
public class AppScheduler extends Intent {
// PLATFORM CONNECTOR ATTRIBUTES
protected PlatformConnector pfconnect = null;
// VIEW ATTRIBUTES
protected UI uiMain = null;
// AppScheduler Constructor
public AppScheduler(Main main) {
// TODO Auto-generated constructor stub
}
public void Initialize (){
//initialize Web Service Connector
pfconnect = new PlatformConnector();
pfconnect.initialize();
// create and initalize GUI
uiMain = new UI();
uiMain.onCreate(null);
}
}
---------------------------
UI Class:
public class UI extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
The Error occurs in case I include "setContentView(R.layout.main);". Note: The main.xml exists and normally runs without any problems.
I don't know why you are using the Intent the way you are. It isn't intended to be used as a controller class base.
An intent is meant invoke an event in the system, and the system figures out the best way to handle it.
Oh and you are trying to instantiate an Activity class and show it; you aren't supposed to do that either. My guess is you haven't done any Android development yet. I recommend starting with the basics and the sample programs.
http://code.google.com/android/intro/hello-android.html
Related
Check this class below,
______ My Code _______
public class Main extends Activity implements OnKeyListener {
DisplayWord currentWord;
TextView wordStatus;
EditText inputWord;
Button okBtn;
int pos = 1;
char currentChar;
char inputChar;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
currentWord = (DisplayWord) this.findViewById(R.id.trueWord);
currentWord.setText("Bonjure"); // word from database
wordStatus = (TextView) this.findViewById(R.id.wordStatus);
inputWord = (EditText) this.findViewById(R.id.wordInput);
inputWord.setOnKeyListener(this);
}
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (inputWord.getText().toString().length() <= currentWord.getText().toString().length()) {
pos = inputWord.getText().toString().length();
currentChar = currentWord.getText().toString().charAt(pos);
inputChar = inputWord.getText().toString().charAt(pos);
} return false;
}
}
_____ END _____
When running this code, upon entering data into the input box it causes an exception straight away and for the program exit unexpectedley.
I am trying to extract the input word from EditText field and the word stored in a TextView and to compare them char by char as those chars are entered into the input box, sort of a word game, where the character will be highlighted green if it is correct and red if it is not.
I have no idea why this behaviour happens, I can see in debugger an StringIndexOutOfBounds exception, and a ResourceNotFound when I have been tampering with different ways of attempting this, access position or the char array, it always seems to throw an exception.
Any advice or links appreciated,
AnthonyI am trying to post a copy and paste of just text, no links but validation error on starting a new thread is telling me that I cannot submit a message with a link in it? I want to show everyone a simple class because of some problem's i'm having.. any ideas?
Hi:
I'm very new at programming in Android (I have programmed extensively PC applications and used Java) and I'm at a loss. I want to create a application that uses fragments. I want to create the Fragment Programaticaly (as the introduction says on Android Developer page) buy my application keeps crashing and I have no Idea as to why.
Here is the code for my main activity:
Code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FragmentManager fmanager = getFragmentManager();
FragmentTransaction ftransaction = fmanager.beginTransaction();
FileBrowser fbrowser = new FileBrowser();
ftransaction.add(R.layout.main,fbrowser, "FileBrowser");
ftransaction.commit();
fbrowser.test();
}
And here is the code for FileBrowser:
Code:
public class FileBrowser extends ListFragment {
public void test(){
System.err.println("Entre a test!!!");
String[] MyList = {"HOla","Mundo","De","Las","Listas"};
if (getActivity() == null){
System.err.println("I get a NULL Activity");
}
else{
System.err.println("No NULL Activity Attempting to Create an Adapter");
ArrayAdapter<String> aa = new ArrayAdapter<String>(getActivity(), R.layout.file_row, MyList);
}
//setListAdapter(new ArrayAdapter<String>(this.getActivity(), R.layout.file_row, MyList));
}
}
All I wan to do to start is to print the string list MyList in the fragment. However I the program crashes and I get this error:
06-09 19:25:42.920: ERROR/AndroidRuntime(26064): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ccr/com.ccr.Main}: java.lang.IllegalArgumentException: No view found for id 0x7f030001 for fragment FileBrowser{4077d558 #0 id=0x7f030001 FileBrowser}
Thanks for any help!
I'm trying to develop (for the liveview) using an SDK and need to get a bitmap of Google Maps.
Code:
//Map Section
public class myMap extends MapActivity {
MapView mapView;
Bitmap bitmap;
boolean created = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
createMap();
}
public void createMap()
{
// build map and mark point
mapView = new MapView(this, "keykeykeykeykeykey");
mapView.setBuiltInZoomControls(false);
GeoPoint point = new GeoPoint(52242730,-884211);
mapView.getController().animateTo(point);
mapView.preLoad();
// copy MapView to canvas
bitmap = Bitmap.createBitmap(400, 800, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
mapView.draw(canvas);
PluginUtils.sendTextBitmap(mLiveViewAdapter, mPluginId, "Loading Map...", 128, 12);
created = true;
}
public void displayMap()
{
if (created == false)
{
createMap();
}
//Display map on device
Bitmap mybit = Bitmap.createBitmap(128, 128, Bitmap.Config.ARGB_8888);
for(int i = 0; i < 128; i++)
{
for(int j = 0; j < 128; j++)
{
mybit.setPixel(i, j, bitmap.getPixel(i, j));
}
}
//white pixel for debugging
mybit.setPixel(64, 64, Color.WHITE);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
Is what I've got so far, the code in createMap() was in onCreate() but It didn't seem to be called.
Now it just force closes.
Anyone know how to help? Is there a better place to ask development questions?
Thanks.
-=Edit=-
Oh, and here's the routine calling the map thing, every couple of seconds.
Code:
private class Timer implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
myMap theMap = new myMap();
theMap.displayMap();
}
}
Okay, looks like you cant use google street map like this.
Anyone looking for similar solution, I used open street map and downloaded 'slipery tiles' png's. They provide java routines for finding the tile url from lat/lon.
http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
Hi all,
I am developing an android app for my project which sends data serially, through RS232 + OTG cable, to a circuit developed by me (and the PC code for which is already done and developed in C#). I'm new to android and tried to get help from example apps. i found an app which establishes the serial communication successfully but the problem is that it sends data in the form of byte, but i want data to be sent as a string and also received as a string. portion of the activity of interest is being added which sends binary 10101010... but I want data in string format. like "abcdefg". it must also be noted that this code is ignoring the incoming data but i want the incoming data to be stored in a variable (which i think is not very difficult but it could be handy if someone can also guide about that)
if someone can help, it'll be highly appreciated
Code:
package android_serialport_api.sample;
import java.io.IOException;
import java.util.Arrays;
import android.os.Bundle;
import java.io.*;
import java.util.*;
//import javax.comm.*; // for SUN's serial/parallel port libraries
public class Sending01010101Activity extends SerialPortActivity {
SendingThread mSendingThread;
byte[] mBuffer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sending01010101);
mBuffer = new byte[1024];
Arrays.fill(mBuffer, (byte) 0x55);
if (mSerialPort != null) {
mSendingThread = new SendingThread();
mSendingThread.start();
}
}
@Override
protected void onDataReceived(byte[] buffer, int size) {
// ignore incoming data
}
private class SendingThread extends Thread {
@Override
public void run() {
while (!isInterrupted()) {
try {
if (mOutputStream != null) {
mOutputStream.write(mBuffer);
} else {
return;
}
} catch (IOException e) {
e.printStackTrace();
return;
}
}
}
}
}
not enough info is availabe on the internet on this topic and I only expect help from this forum. its very important for me
Prerequisite
Don't know what is AutomateIt ? Read more on this thread.
What is AutomateIt Plugin ?
Plugins are applications that by installing them, you add new actions and triggers that you can use within AutomateIt. Sounds easy, right ?
Each plugin can provide additional triggers and actions that extend the capabilities of AutomateIt, so you can focus on the functionality and take advantage of the AutomateIt platform to connect each trigger or action with existing capabilities of AutomateIt or with other plugins.
More details and a list of available plugins are available on the app website.
Getting started
Although developing a plugin is super-easy and requires only several simple steps, you are deeply encouraged to read the full documentation on the app website.
To summarize it for you, here are the steps you need to follow when developing a new plugin:
Download AutomateIt Plugin SDK
Setup your environment
Develop your plugin
Test your plugin
Setting up your environment after downloading the plugin SDK is done by adding a single dependency needs to be added to your build.gradle file:
Code:
compile 'com.automateitapp:automateit.plugin.sdk:1.0.3'
Creating a new plugin action or trigger is done by implementing a class that extends either PluginAction or PluginTrigger in your plugin project that references the AutomateItPluginLibrary.
Each of these abstract classes requires a set of simple functions to be implemented that provide metadata for the action or trigger (such as title, description, icons and parameters) and the functionality of the action or trigger. See sample code below for an action that shows a Toast message.
a complete sample app is available as a git repo that can be cloned from here.
When the plugin app is installed on a user device, it communicates with AutomateIt so that the plugin actions and triggers become available within AutomateIt.
Additional Documentation
AutomateIt Plugin Developer Guide
AutomateIt Plugin SDK Reference
Looking forward to seeing some awesome plugins !
Sample Action Code
Code:
package com.smarterapps.demoplugin.actions;
import java.util.List;
import android.content.Context;
import android.widget.Toast;
import com.smarterapps.automateitplugin.sdk.PluginDataFieldCollection;
import com.smarterapps.automateitplugin.sdk.PluginValidationResult;
import com.smarterapps.automateitplugin.sdk.RequiredApp;
import com.smarterapps.automateitplugin.sdk.fields.PluginDataFieldBoolean;
import com.smarterapps.automateitplugin.sdk.fields.PluginDataFieldString;
import com.smarterapps.demoplugin.R;
public class Action1 extends com.smarterapps.automateitplugin.sdk.PluginAction
{
private static final int FIELD_ID_TEXT_TO_SHOW = 1;
private static final int FIELD_ID_DURATION_LONG = 2;
@Override
public String getActionTitle(Context context)
{
return context.getString(R.string.action1_title);
}
@Override
public String getActionDescription(Context context)
{
return context.getString(R.string.action1_default_description);
}
@Override
public String getActionDescription(Context context, PluginDataFieldCollection data)
{
if (null != data)
{
String msgText = (String) data.getFieldById(FIELD_ID_TEXT_TO_SHOW).getValue();
boolean durationLong = (Boolean) data.getFieldById(FIELD_ID_DURATION_LONG).getValue();
int toastDurationStringId = durationLong ? R.string.duration_long: R.string.duration_short;
return context.getString(R.string.action1_description,
context.getString(toastDurationStringId),
msgText);
}
return getActionDescription(context);
}
@Override
public int getActionIconResourceId()
{
return R.drawable.ic_action1;
}
@Override
public int getActionSmallIconResourceId()
{
return R.drawable.ic_action1_small;
}
@Override
public PluginDataFieldCollection getActionFields(Context context)
{
PluginDataFieldCollection retval = new PluginDataFieldCollection();
retval.add(new PluginDataFieldBoolean(
FIELD_ID_DURATION_LONG,
context.getString(R.string.action1_field_duration_title),
context.getString(R.string.action1_field_duration_description),
true));
retval.add(new PluginDataFieldString(
FIELD_ID_TEXT_TO_SHOW,
context.getString(R.string.action1_field_msg_title),
context.getString(R.string.action1_field_msg_description),
""));
return retval;
}
@Override
public void doAction(Context context, PluginDataFieldCollection data)
{
boolean durationLong = (Boolean) data.getFieldById(FIELD_ID_DURATION_LONG).getValue();
int toastDuration = durationLong ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT;
String toastMsg = (String) data.getFieldById(FIELD_ID_TEXT_TO_SHOW).getValue();
Toast.makeText(context, toastMsg, toastDuration).show();
}
@Override
public PluginValidationResult validateData(Context context, PluginDataFieldCollection data)
{
// Validate action1
String toastMsg = (String) data.getFieldById(FIELD_ID_TEXT_TO_SHOW).getValue();
if (null == toastMsg || toastMsg.trim().length() == 0)
{
return new PluginValidationResult(false, "Toast message can't be empty");
}
return PluginValidationResult.getValidResult();
}
@Override
public List<RequiredApp> getRequiredApps()
{
return null;
}
}
AutomateIt Plugin SDK has been updated so it is now even easier to integrate actions and triggers into your apps, or build a brand new plugin.
It requires a single dependency in your build.gradle file and the rest is up to you!
Sample app is available as a git repo on bitbucket.org:
https://bitbucket.org/AutomateIt/automateit-plugin-demo
(Instructions were updated on the developer website and this post).