NullPointerException's on StartActivityForResult, Struggling with SQLite in android i think? - android

I have one class called Budget.java, within that i start Keypad.java. This was all working fine up until recently when i added a bunch of code to Keypad.java (The added code was to update a row in my SQLite database on the press of a button, all the unrelated methods were working until i tried to implement this). Now using breakpoints i think i've figured out that i get the error message as soon as i try to open the Keypad activity and i don't have a clue what could be the problem.
Maybe it's my misunderstanding of the sqlite open helper? Or perhaps its because i'm using StartActivityForResult?
Any suggestions would be very appreciated! I can upload the logcat if you think that would help.
I uploaded the two little classes to pastebin, you might find it easier to read?
Budget.java ( look for ListItemCommonIntent )
keypad.java

In your Keypad.java, you have the following outside the onCreate:
EditText userAmount=(EditText)this.findViewById(R.id.cost_input);
This wont work because you have to use the setContentView to reference the layout where you want to find the view. And when it initializes userAmount, the object is not available yet (so this is null) .
Try this:
private EditText userAmount;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.keypad);
userAmount=(EditText)findViewById(R.id.cost_input);
MySpinner();
Main();
}

Related

Confused on MainActivity.java again

So I'm still fairly new to Android Studio and programming in general and I'm trying to follow this course by Udacity. The problem is that in MainActivity.java all it says in mine is
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ...
}
while they have more Override Methods such as OnOptionsItemSelected, etc. I believe I was told by them to set it to "Blank Activity", which I believe is "Empty Activity" now, and I think the problem is the updates that have been made since there video had been made and now, but I'm just looking for a solution for this. Any help would be appreciated. I hope I am making sense here Thanks.
What you're looking for is "Basic Activity" instead of "Empty Activity" and you should be able to continue following the tutorial.
If they have the xml files and java files available, once you copy and paste it should look the same as well.
Sounds like you didn't really know about Android's coding framework, and need to read some example or tutorial.
I'm not have any relation about the following site, but as my point of view, it's a good Android coding tutorial.
Android - Hello World Example
OnOptionItemSelected is a callback method which handles menus.
You only need the onCreate callback method in order to display a sample Hello world! and then follow the Udacity course.
You can erase the others if you have created a blank activity instead of an empty one.

Android `initialize()` is not recognized

So I was following this tutorial and got everything mostly working. But for some reason, the line that says initialize() is not being recognized by Android Studio.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.part_camera_view);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
myContext = this;
initialize(); //This line is not recognized
}
I'm way too new to understanding what's going on, so I'm hoping to be nudged in the right direction. I can't find anything in Android documents that talk about initialize()
I've basically copy and pasted the script from the tutorial and changed the specific elements to fit my project. I am able to see a screen with the buttons provided, but I can not see the live view from the camera, and I believe that line is the problem
Any thoughts?
Initialize is a function. It isn't a prebuilt SDK function- if you want one, you have to write it yourself. If you don't need one, the call to it shouldn't be there.
As an aside, I have serious concerns about this code without even looking at the link to the tutorial- there's no reason to store this in a separate variable, and it makes me doubt the author understands his own code.
If you scroll down to the bottom of the tutorials page you will find a link where you can download the source code of the example project. You will find the initialize method in theAndroidCameraExample class.

Activity is getting too large and becoming more and more difficult to work with. Solutions to solve this issue?

I am working on my second Android Application, first being, hello world. The application code is quite crazy looking because I love to test new libraries and ideas in it. I have been working on this application for well over 3 months and one of my activities is getting way to large and difficult to work with. I find myself getting lost in the code and it is taking longer to do simple things. There might be simple solutions to solving this issue. I really want to split my activity into two and reference each other if possible. Is there are any suggestions to simplifying and organizing code that would be greatly helpful. Even example will help me very much.
Part of my activity is adding a ton of data into a database and the other part is a long equation with multiple values. Another part is implementing the HoloGraphLibrary (Which I love). It is also implementing a listView with custom adapter. It also has a custom dialog............ I can go on and on. I hope you get my point.
EDIT
Going to work with this.
HoloGraphHelper holoGraph = new HoloGraphHelper();
holoGraph.initialize();
Try creating classes for each responsibility.
A Database Helper that has functions to insert data too:
DatabaseHelper database = new DatabaseHelper();
database .insertData(whatever);
A HoloGraphHelper that initializes the HoloGraph
HoloGraphHelper holoGraph = new HoloGraphHelper();
holoGraph.initialize();
And so on.
Break into multiple files. First classes defined in the Activity like the adapter. Change anonymous classes to classes defined in their own file. Look for ways to break out other related code into a class.
Right click on src folder of your Project and select new - class to create a new class. You can use a class for storing methods but you won't be able to display anything on screen.
To display contents to user, you can create a new Activity bu pressing Ctrl + N and selecting Android - Android Activity.
The best way is modularise your code.
I.e split your code into various related modules, for example a separate class for each part that your testing. So you could have a database entry class, a class for Gui testing, i.e. for your custom dialog. That class does all the work for that test, into various functions, I always try to keep functions as small as possible as they are easy to read.
As an example for your database entry, you could have a function which checks the database if the record already exists and then insert it. But a better way would be your insert function only performs the insert code and instead within this function it calls CheckIfDatAlreadyExists function which can return a bool so you know whether you should go ahead and insert the record. This would keep the code tidy and clean to manage.
Then from your main activity all would need to do is instantiate the relative class and call the relevant method.

Cannot call 'SetContentView' on new activity - Android Mono

I've had a search for this problem but nothing seems to help me to solve this particular error I am getting.
I am writing my first Android app and am coming across a java.lang.RuntimeException whenever I call SetContentView on a new activity.
There is nothing in the logcat which helps (an activity idle timeout is all because it falters on the call).
My activity Login has a layout set during OnCreate which works fine, but any subsequent calls fall over. Here's some code ;)
[Activity(Label = "Usage")]
public class Usage : TabActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
//**FALLS OVER HERE**
SetContentView(Resource.Layout.Usage);
The Resource.Designer.cs has a record of my layout:
// aapt resource value: 0x7f030002
public const int Usage = 2130903042;
...and when I reference that layout by it's int value it falls back to the previous activity without hitting any breakpoints in the Usage activity.
Anyone got any thoughts or can point me in the direction of a similar post?
Legends!
UPDATE
I tried a whole stack of fixes I found on forums etc but nothing would fix this. I put the whole thing on the backburner while I worked on something else, came back to it and now it works...wish I could say what it was that made it work to help others out but I can't explain it! COULD have been an update to a new version of MonoDroid?
As stated in my comment to Stuart's answer, this problem appears to have resolved itself. I revisited the project all this time later and think that it might have just been a case of cleaning the project and rebuilding all. I have not had this problem since.
Sorry that this is not a detailed answer, I would suggest trying the ol' clean and rebuild.
I've recently had some issues when working in VS2010 where the resource ids are being not kept perfectly in-sync with the resource files and the java ids.
To resolve these, I generally find the quickest way is to add a new id to one of the layout xml files - this then causes a regeneration or the resources.cs file which then means the app works again.
If that doesn't help, then please post more info about what the message inside the RuntimeException is,
Although it's very late response, but someone who might be getting this sort of error. Wrap it with try-catch and it gives more details about the exception. I spent few hours before figuring out to do that

Add Words to Android's UserDictionary

I want to add an entire medical dictionary to my android phone (Moto Droid). I would like to be able to send text messages and have the medical words be in the predictable text.
I've been trying to write a small app that would accomplish this, but everything I try the app crashes on startup. I've never written an app for a mobile platform so that is a first for me. Here is what is not working properly.
public class WordAdd extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
UserDictionary.Words.addWord( this , "newMedicalWord", 1, UserDictionary.Words.LOCALE_TYPE_CURRENT);
}
}
It seems so simple to do, yet I am so stuck. Thanks for any help you can provide.
EDIT: I should mention that I am getting this error for Android 2.1 in the AVD (virtual device).
EDIT 2: User Dictionary is found in the Android API. addWord is a static method. I don't declare UserDictionary because I just use the one static method. It's been ages since I developed anything in Java and this is my first attempt at any mobile development, so I don't know if I am doing something wrong.
Add this to your app's AndroidManifest.xml file outside of the <application> element:
<uses-permission android:name="android.permission.WRITE_USER_DICTIONARY"></uses-permission>
Try setting a breakpoint at the start of your activity and stepping through the code with the debugger. This should help you isolate whether that call is really what is causing the crash, and what the underlying exception is.
mm mm where UserDictionary is defined? maybe you should just
UserDictionary = new UserDictionaryType();
UserDictionary.Words = new WordsType();
OR define in the class just under the class declaration the fallowing :
static UserDictionaryType UserDictionary;
if that's the case it's obvious why your app crashed ... (do it on kernel mode and "Houston we got a problem" you cant access pointer that you didnt allocated memory for even in java which is managed code... )
but again I am not familiar with your code show us where it defined and I would try to help you more ...
EDIT1: even if UserDictionary exists in the API you didn't declare on one...
you should declare somewhwere static UserDictionary ud = new UserDictionary();

Categories

Resources