Android: how to refer to EditText view - android

I have the following layout:
<TextView
android:id = "#+id/HRNoninTextBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/exitbtn"
android:text="Heart Rate"
android:textSize = "20sp"/>
<EditText
android:id="#+id/labelNoninHeartRate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00"
android:layout_alignParentRight="true">
</EditText>
from the code i refer to these views, form my activity, in this way:
TextView tvHr= (TextView)findViewById(R.id.HRNoninTextBox);
EditText etHr = (EditText)findViewById(R.id.labelNoninHeartRate);
the first one does not produce any error, while the second statement produce the following error:
01-09 13:24:57.599: W/dalvikvm(28816): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
01-09 13:24:57.619: E/AndroidRuntime(28816): FATAL EXCEPTION: main
01-09 13:24:57.619: E/AndroidRuntime(28816): java.lang.RuntimeException: Unable to start activity ComponentInfo{aid.assistant233/aid.assistant233.AidAssistant233Activity}: java.lang.ClassCastException: android.widget.TextView
01-09 13:24:57.619: E/AndroidRuntime(28816): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
01-09 13:24:57.619: E/AndroidRuntime(28816): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
01-09 13:24:57.619: E/AndroidRuntime(28816): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-09 13:24:57.619: E/AndroidRuntime(28816): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
01-09 13:24:57.619: E/AndroidRuntime(28816): at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 13:24:57.619: E/AndroidRuntime(28816): at android.os.Looper.loop(Looper.java:130)
01-09 13:24:57.619: E/AndroidRuntime(28816): at android.app.ActivityThread.main(ActivityThread.java:3691)
01-09 13:24:57.619: E/AndroidRuntime(28816): at java.lang.reflect.Method.invokeNative(Native Method)
01-09 13:24:57.619: E/AndroidRuntime(28816): at java.lang.reflect.Method.invoke(Method.java:507)
01-09 13:24:57.619: E/AndroidRuntime(28816): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
01-09 13:24:57.619: E/AndroidRuntime(28816): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
01-09 13:24:57.619: E/AndroidRuntime(28816): at dalvik.system.NativeStart.main(Native Method)
01-09 13:24:57.619: E/AndroidRuntime(28816): Caused by: java.lang.ClassCastException: android.widget.TextView
01-09 13:24:57.619: E/AndroidRuntime(28816): at aid.assistant233.AidAssistant233Activity.onCreate(AidAssistant233Activity.java:331)
01-09 13:24:57.619: E/AndroidRuntime(28816): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-09 13:24:57.619: E/AndroidRuntime(28816): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
01-09 13:24:57.619: E/AndroidRuntime(28816): ... 11 more
Where may be the problem?

Step1 : you can clean/build your project as everyone said . but If you are not getting your issue solve by clean/build then check my step2 for it.
Step:2:
I am not Sure But Can you please check your xml file once again?
this also could be happen if You've two elements with the same ID:
<TextView android:text="Hello Ahmedabad:"
android:id="#+id/txtahmedabad"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<EditText android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="#+id/txtahmedabad"
android:text="">
</EditText>
if this is so then Give them different IDs -
it could picking the first one (the TextView) which obviously can't be cast to an EditText.
Hope it will some how help you.

Related

show text after pressing button in android

I'm new to android, now i want to press a button then display text on the screen. here's my code:
xml
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendMessages"
android:text="Button" />
<TextView
android:id="#+id/buttonText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:ems="10"
android:inputType="textMultiLine"
android:text="hello" />
main activity
public class MainActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState){
.......
}
public void sendMessages(View view) {
TextView welcome = (TextView) findViewById(R.id.buttonText);
welcome.setText("button clicked");
}
}
but when i run this on a simulator, after pressing the button, the application will report
fortunately, First Android App has stopped
does anyone know what's wrong with my program?
04-18 00:56:20.147: D/AndroidRuntime(1280): Shutting down VM
04-18 00:56:20.147: W/dalvikvm(1280): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
04-18 00:56:20.177: E/AndroidRuntime(1280): FATAL EXCEPTION: main
04-18 00:56:20.177: E/AndroidRuntime(1280): java.lang.IllegalStateException: Could not execute method of the activity
04-18 00:56:20.177: E/AndroidRuntime(1280): at android.view.View$1.onClick(View.java:3591)
04-18 00:56:20.177: E/AndroidRuntime(1280): at android.view.View.performClick(View.java:4084)
04-18 00:56:20.177: E/AndroidRuntime(1280): at android.view.View$PerformClick.run(View.java:16966)
04-18 00:56:20.177: E/AndroidRuntime(1280): at android.os.Handler.handleCallback(Handler.java:615)
04-18 00:56:20.177: E/AndroidRuntime(1280): at android.os.Handler.dispatchMessage(Handler.java:92)
04-18 00:56:20.177: E/AndroidRuntime(1280): at android.os.Looper.loop(Looper.java:137)
04-18 00:56:20.177: E/AndroidRuntime(1280): at android.app.ActivityThread.main(ActivityThread.java:4745)
04-18 00:56:20.177: E/AndroidRuntime(1280): at java.lang.reflect.Method.invokeNative(Native Method)
04-18 00:56:20.177: E/AndroidRuntime(1280): at java.lang.reflect.Method.invoke(Method.java:511)
04-18 00:56:20.177: E/AndroidRuntime(1280): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-18 00:56:20.177: E/AndroidRuntime(1280): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-18 00:56:20.177: E/AndroidRuntime(1280): at dalvik.system.NativeStart.main(Native Method)
04-18 00:56:20.177: E/AndroidRuntime(1280): Caused by: java.lang.reflect.InvocationTargetException
04-18 00:56:20.177: E/AndroidRuntime(1280): at java.lang.reflect.Method.invokeNative(Native Method)
04-18 00:56:20.177: E/AndroidRuntime(1280): at java.lang.reflect.Method.invoke(Method.java:511)
04-18 00:56:20.177: E/AndroidRuntime(1280): at android.view.View$1.onClick(View.java:3586)
04-18 00:56:20.177: E/AndroidRuntime(1280): ... 11 more
04-18 00:56:20.177: E/AndroidRuntime(1280): Caused by: java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to android.widget.TextView
04-18 00:56:20.177: E/AndroidRuntime(1280): at com.example.firstandroidapp.MainActivity.sendMessages(MainActivity.java:110)
04-18 00:56:20.177: E/AndroidRuntime(1280): ... 14 more
Do button and TextView exist in the activity layout?
Do you call setContentView(Layout) in onCreate?
Are you working with ADT?
If you are working with ADT you can create a new activity and
a new xml layout, so call setContentView(R.layout.YourLayout)
in onCreate
YourLayout.xml has to contains button and TextView

ResourceNotFoundException while trying to parse xml file in android

I'm trying to parse an xml file and extract data from it to display in my android app.
The xml file is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<record>
<study id="1">
<topic>SAX Parser</topic>
<content>Learn how to parse XML using SAXParser</content>
<author>Pete Houston</author>
<date>02-Oct-2011</date>
</study>
The file is stored in res/raw folder. The android activity calls a function which tries to read the xml file and parses it to retrieve data,which is then stored into an object of type Study (defined by me). This object is then returned by the function.
The skeleton structure of my code is given below:
public class XmlParserActivity extends Activity {
Study study=null;
protected void onCreate(Bundle savedInstanceState) {
//...android code and some variable initializations
try{
study = parseXml(getResources().openRawResource(R.raw.record));
}catch(Exception e){
e.printStackTrace();
finish();
}
}
public Study parseXml(InputStream is){
//code for parsing the input stream
return study;
}
When I launch the app in an emulator it crashes and produces android.content.res.ResourcesNotFoundException. Is there something wrong with the way I am accessing the xml file? Or do I need to set some permissions in the AndroidManifest.xml file? I have searched this site and other sites but have not found a satisfactory solution to my problem. Any guidance will be deeply appreciated.
logcat messages:
01-09 15:49:55.801: I/Process(1152): Sending signal. PID: 1152 SIG: 9
01-09 15:50:04.174: E/Trace(1198): error opening trace file: No such file or directory(2)
01-09 15:50:04.992: W/ResourceType(1198): No package identifier when getting value for resource number 0x00000001
01-09 15:50:05.002: D/AndroidRuntime(1198): Shutting down VM
01-09 15:50:05.002: W/dalvikvm(1198): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
01-09 15:50:05.012: E/AndroidRuntime(1198): FATAL EXCEPTION: main
01-09 15:50:05.012: E/AndroidRuntime(1198): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidxmlparser/com.example.androidxmlparser.XmlParserActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x1
01-09 15:50:05.012: E/AndroidRuntime(1198): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
01-09 15:50:05.012: E/AndroidRuntime(1198): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
01-09 15:50:05.012: E/AndroidRuntime(1198): at android.app.ActivityThread.access$600(ActivityThread.java:130)
01-09 15:50:05.012: E/AndroidRuntime(1198): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
01-09 15:50:05.012: E/AndroidRuntime(1198): at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 15:50:05.012: E/AndroidRuntime(1198): at android.os.Looper.loop(Looper.java:137)
01-09 15:50:05.012: E/AndroidRuntime(1198): at android.app.ActivityThread.main(ActivityThread.java:4745)
01-09 15:50:05.012: E/AndroidRuntime(1198): at java.lang.reflect.Method.invokeNative(Native Method)
01-09 15:50:05.012: E/AndroidRuntime(1198): at java.lang.reflect.Method.invoke(Method.java:511)
01-09 15:50:05.012: E/AndroidRuntime(1198): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-09 15:50:05.012: E/AndroidRuntime(1198): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-09 15:50:05.012: E/AndroidRuntime(1198): at dalvik.system.NativeStart.main(Native Method)
01-09 15:50:05.012: E/AndroidRuntime(1198): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1
01-09 15:50:05.012: E/AndroidRuntime(1198): at android.content.res.Resources.getText(Resources.java:229)
01-09 15:50:05.012: E/AndroidRuntime(1198): at android.widget.TextView.setText(TextView.java:3620)
01-09 15:50:05.012: E/AndroidRuntime(1198): at com.example.androidxmlparser.XmlParserActivity.onCreate(XmlParserActivity.java:34)
01-09 15:50:05.012: E/AndroidRuntime(1198): at android.app.Activity.performCreate(Activity.java:5008)
01-09 15:50:05.012: E/AndroidRuntime(1198): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
01-09 15:50:05.012: E/AndroidRuntime(1198): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
01-09 15:50:05.012: E/AndroidRuntime(1198): ... 11 more
You're trying to access a string resource that cannot be found.
As Matt Taylor suggests, you should check line 34 of your activity, and find which string resource is wrong.
Further to fiddler's comment on your question, I've just seen what I think is the problem:
Your xml file just needs to contain:
<study id="1">
<topic>SAX Parser</topic>
<content>Learn how to parse XML using SAXParser</content>
<author>Pete Houston</author>
<date>02-Oct-2011</date>
</study>
And you access it using:
getResources().openRawResource(R.raw.<xml file name (without.xml)>);
For example, if it was called xmldata.xml you'd use R.raw.xmldata

Adding a Style to a Custom Tab

I have changed my app so the user can have 2 different styles to the whole app. I have updated the Custom Tabs I created so they link to a Style which in turns (depending on the selected style) links to a Selector xml in drawable (ie what to show for selected or unselected).
Running this gives me the following Errors
01-09 10:46:28.701: E/AndroidRuntime(375): FATAL EXCEPTION: main
01-09 10:46:28.701: E/AndroidRuntime(375): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myappname/com.myappname.RoundSelect}: android.view.InflateException: Binary XML file line #11: Error inflating class <unknown>
01-09 10:46:28.701: E/AndroidRuntime(375): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-09 10:46:28.701: E/AndroidRuntime(375): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-09 10:46:28.701: E/AndroidRuntime(375): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-09 10:46:28.701: E/AndroidRuntime(375): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-09 10:46:28.701: E/AndroidRuntime(375): at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 10:46:28.701: E/AndroidRuntime(375): at android.os.Looper.loop(Looper.java:123)
01-09 10:46:28.701: E/AndroidRuntime(375): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-09 10:46:28.701: E/AndroidRuntime(375): at java.lang.reflect.Method.invokeNative(Native Method)
01-09 10:46:28.701: E/AndroidRuntime(375): at java.lang.reflect.Method.invoke(Method.java:507)
01-09 10:46:28.701: E/AndroidRuntime(375): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-09 10:46:28.701: E/AndroidRuntime(375): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-09 10:46:28.701: E/AndroidRuntime(375): at dalvik.system.NativeStart.main(Native Method)
01-09 10:46:28.701: E/AndroidRuntime(375): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class <unknown>
01-09 10:46:28.701: E/AndroidRuntime(375): at android.view.LayoutInflater.createView(LayoutInflater.java:518)
01-09 10:46:28.701: E/AndroidRuntime(375): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
01-09 10:46:28.701: E/AndroidRuntime(375): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
01-09 10:46:28.701: E/AndroidRuntime(375): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
01-09 10:46:28.701: E/AndroidRuntime(375): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
01-09 10:46:28.701: E/AndroidRuntime(375): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
01-09 10:46:28.701: E/AndroidRuntime(375): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
01-09 10:46:28.701: E/AndroidRuntime(375): at com.myappname.RoundSelect.createTabView(RoundSelect.java:234)
01-09 10:46:28.701: E/AndroidRuntime(375): at com.myappname.RoundSelect.setupTab(RoundSelect.java:223)
01-09 10:46:28.701: E/AndroidRuntime(375): at com.myappname.RoundSelect.setupCustomTabs(RoundSelect.java:215)
01-09 10:46:28.701: E/AndroidRuntime(375): at com.myappname.RoundSelect.onCreate(RoundSelect.java:90)
01-09 10:46:28.701: E/AndroidRuntime(375): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-09 10:46:28.701: E/AndroidRuntime(375): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-09 10:46:28.701: E/AndroidRuntime(375): ... 11 more
01-09 10:46:28.701: E/AndroidRuntime(375): Caused by: java.lang.reflect.InvocationTargetException
01-09 10:46:28.701: E/AndroidRuntime(375): at java.lang.reflect.Constructor.constructNative(Native Method)
01-09 10:46:28.701: E/AndroidRuntime(375): at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
01-09 10:46:28.701: E/AndroidRuntime(375): at android.view.LayoutInflater.createView(LayoutInflater.java:505)
01-09 10:46:28.701: E/AndroidRuntime(375): ... 23 more
01-09 10:46:28.701: E/AndroidRuntime(375): Caused by: android.content.res.Resources$NotFoundException: File res/drawable/custom_tab_text_selector_black.xml from drawable resource ID #0x7f020023
01-09 10:46:28.701: E/AndroidRuntime(375): at android.content.res.Resources.loadDrawable(Resources.java:1697)
01-09 10:46:28.701: E/AndroidRuntime(375): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
01-09 10:46:28.701: E/AndroidRuntime(375): at android.view.View.<init>(View.java:1951)
01-09 10:46:28.701: E/AndroidRuntime(375): at android.widget.TextView.<init>(TextView.java:344)
01-09 10:46:28.701: E/AndroidRuntime(375): at android.widget.TextView.<init>(TextView.java:337)
01-09 10:46:28.701: E/AndroidRuntime(375): ... 26 more
01-09 10:46:28.701: E/AndroidRuntime(375): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #7: <item> tag requires a 'drawable' attribute or child tag defining a drawable
01-09 10:46:28.701: E/AndroidRuntime(375): at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:167)
01-09 10:46:28.701: E/AndroidRuntime(375): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
01-09 10:46:28.701: E/AndroidRuntime(375): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
01-09 10:46:28.701: E/AndroidRuntime(375): at android.content.res.Resources.loadDrawable(Resources.java:1694)
01-09 10:46:28.701: E/AndroidRuntime(375): ... 30 more
Code for the Custombackground
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tabsLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
style="?custom_tab_background_selector">
<TextView
android:id="#+id/customTabsText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?custom_tab_text_selector">
</TextView>
</LinearLayout>
Method that is calling this
private static View createTabView(Context pContext, String pTabTitleText) {
View view = LayoutInflater.from(pContext).inflate(R.layout.custom_tabs_background, null);
TextView tv = (TextView) view.findViewById(R.id.customTabsText);
tv.setText(pTabTitleText);
return view;
}
The rest is just a xml Theme that points to a Selector XML which in turns points to some drawble xml files. Here is the Selector XML
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Active tab -->
<item
android:state_selected="true"
android:state_focused="false"
android:state_pressed="false"
android:drawable="#drawable/custom_tab_background_selected_black" />
<!-- Inactive tab -->
<item
android:state_selected="false"
android:state_focused="false"
android:state_pressed="false"
android:drawable="#drawable/custom_tab_background_unselected_black" />
<!-- Pressed tab -->
<item
android:state_pressed="true"
android:drawable="#android:color/transparent" />
<!-- Selected tab (using d-pad) -->
<item
android:state_focused="true"
android:state_selected="true"
android:state_pressed="false"
android:drawable="#android:color/transparent" />
</selector>
Any ideas on why I am getting this error would be great. Thanks for you time
It appears I tracked it down.
the background of the tab I was using android:background=.... but was also using the same for the colour of the text and it should have been android:textColour=....
all works now. Sorry for the confusion

Null Pointer Exception in First Android App

I am trying to write my first Android app, and I'm having trouble. I wrote a simple Java program to find the factors of a number provided by the user, and I'd like to port it over to Java. I have an XML file for the interface, which seemed to run OK until I added the first Java class. Now it won't run, and log.cat says that there's a null pointer exception. So far, I only have stubs, but it seems like it should run OK with what I've done to this point. I'm including the log.cat text, but I'm too new it this to make much sense out of it.
There may be more than one problem. After adding the Java file I began to get run-time errors right away, but I don't think they were null pointer exceptions. I think that started when I made changes to fix what was already causing problems.
Any help is appreciated.
Here is the Java file:
import android.app.Activity; <br>
import android.os.Bundle; <br>
import android.widget.Button; <br>
import android.widget.EditText; <br>
import android.widget.TextView; <br>
public class AndroidFactoringActivity extends Activity {
// Instance Variables
EditText userNumber;
Button factorButton;
TextView resultsField;
int factorResults;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
factorButton = (Button) findViewById(R.id.factorButton);
userNumber = (EditText) findViewById(R.id.userNumber);
factorResults = 1;
resultsField.setText(String.valueOf(factorResults));
}
}
Here is main.xml:
<?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="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/askField"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/ask"
android:textSize="24dp" />
<EditText
android:id="#+id/userNumber"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<Button
android:id="#+id/factorButton"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/click" />
<TextView
android:id="#+id/resultsField"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/tell"
android:textSize="24dp" />
</LinearLayout>
Here are the log.cat results:
03-31 23:58:53.579: D/AndroidRuntime(2804): Shutting down VM
03-31 23:58:53.589: W/dalvikvm(2804): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
03-31 23:58:53.589: E/AndroidRuntime(2804): Uncaught handler: thread main exiting due to uncaught exception
03-31 23:58:53.650: E/AndroidRuntime(2804): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.dave_b.factoring/net.dave_b.factoring.AndroidFactoringActivity}: java.lang.NullPointerException
03-31 23:58:53.650: E/AndroidRuntime(2804): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
03-31 23:58:53.650: E/AndroidRuntime(2804): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
03-31 23:58:53.650: E/AndroidRuntime(2804): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
03-31 23:58:53.650: E/AndroidRuntime(2804): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
03-31 23:58:53.650: E/AndroidRuntime(2804): at android.os.Handler.dispatchMessage(Handler.java:99)
03-31 23:58:53.650: E/AndroidRuntime(2804): at android.os.Looper.loop(Looper.java:123)
03-31 23:58:53.650: E/AndroidRuntime(2804): at android.app.ActivityThread.main(ActivityThread.java:4363)
03-31 23:58:53.650: E/AndroidRuntime(2804): at java.lang.reflect.Method.invokeNative(Native Method)
03-31 23:58:53.650: E/AndroidRuntime(2804): at java.lang.reflect.Method.invoke(Method.java:521)
03-31 23:58:53.650: E/AndroidRuntime(2804): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-31 23:58:53.650: E/AndroidRuntime(2804): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-31 23:58:53.650: E/AndroidRuntime(2804): at dalvik.system.NativeStart.main(Native Method)
03-31 23:58:53.650: E/AndroidRuntime(2804): Caused by: java.lang.NullPointerException
03-31 23:58:53.650: E/AndroidRuntime(2804): at net.dave_b.factoring.AndroidFactoringActivity.onCreate(AndroidFactoringActivity.java:26)
03-31 23:58:53.650: E/AndroidRuntime(2804): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-31 23:58:53.650: E/AndroidRuntime(2804): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
03-31 23:58:53.650: E/AndroidRuntime(2804): ... 11 more
03-31 23:58:53.679: I/dalvikvm(2804): threadid=7: reacting to signal 3
03-31 23:58:53.679: E/dalvikvm(2804): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
03-31 23:59:57.629: I/Process(2804): Sending signal. PID: 2804 SIG: 9
04-01 00:07:36.129: D/AndroidRuntime(3040): Shutting down VM
04-01 00:07:36.129: W/dalvikvm(3040): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
04-01 00:07:36.139: E/AndroidRuntime(3040): Uncaught handler: thread main exiting due to uncaught exception
04-01 00:07:36.159: E/AndroidRuntime(3040): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.dave_b.factoring/net.dave_b.factoring.AndroidFactoringActivity}: java.lang.NullPointerException
04-01 00:07:36.159: E/AndroidRuntime(3040): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
04-01 00:07:36.159: E/AndroidRuntime(3040): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
04-01 00:07:36.159: E/AndroidRuntime(3040): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
04-01 00:07:36.159: E/AndroidRuntime(3040): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
04-01 00:07:36.159: E/AndroidRuntime(3040): at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 00:07:36.159: E/AndroidRuntime(3040): at android.os.Looper.loop(Looper.java:123)
04-01 00:07:36.159: E/AndroidRuntime(3040): at android.app.ActivityThread.main(ActivityThread.java:4363)
04-01 00:07:36.159: E/AndroidRuntime(3040): at java.lang.reflect.Method.invokeNative(Native Method)
04-01 00:07:36.159: E/AndroidRuntime(3040): at java.lang.reflect.Method.invoke(Method.java:521)
04-01 00:07:36.159: E/AndroidRuntime(3040): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-01 00:07:36.159: E/AndroidRuntime(3040): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-01 00:07:36.159: E/AndroidRuntime(3040): at dalvik.system.NativeStart.main(Native Method)
04-01 00:07:36.159: E/AndroidRuntime(3040): Caused by: java.lang.NullPointerException
04-01 00:07:36.159: E/AndroidRuntime(3040): at net.dave_b.factoring.AndroidFactoringActivity.onCreate(AndroidFactoringActivity.java:26)
04-01 00:07:36.159: E/AndroidRuntime(3040): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-01 00:07:36.159: E/AndroidRuntime(3040): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
04-01 00:07:36.159: E/AndroidRuntime(3040): ... 11 more
04-01 00:07:36.199: I/dalvikvm(3040): threadid=7: reacting to signal 3
04-01 00:07:36.199: E/dalvikvm(3040): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
04-01 00:07:40.329: I/Process(3040): Sending signal. PID: 3040 SIG: 9
resultsField.setText(String.valueOf(factorResults));
resultsField is null and you are trying to set value on null.
resultsField= (TextView ) findViewById(R.id.userNumber);
Get textview instance before setting value, otherwise resultsField will be null and all operations on null reference results in NullPointerException.
You got a null pointer because you didnt intialise the variable resultsField (you just declared it) .
As you used
userNumber = (EditText) findViewById(R.id.userNumber);
you must even initialise the reference resultsField.
This can be done using
resultsField= (TextView ) findViewById(R.id.resultsField);
in the onCreate() after the statement setContentView()
You need to initialize the userNumer and resultsFiled as follows
userNumber = (EditText) findViewById(R.id.userNumber);
resultsField= (TextView ) findViewById(R.id.resultsField);

Problem with Stack traces in android market

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.falling.inairproandmark/com.falling.inairproandmark.Q9Popup}: android.view.InflateException: Binary XML file line #9: Error inflating class <unknown>
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:876)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:513)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:219)
at android.app.Activity.setContentView(Activity.java:1647)
at com.falling.inairproandmark.Q9Popup.onCreate(Q9Popup.java:36)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
... 11 more
Caused by: java.lang.reflect.InvocationTargetException
at android.widget.Button.<init>(Button.java:65)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
at android.view.LayoutInflater.createView(LayoutInflater.java:500)
... 22 more
Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:468)
at android.graphics.Bitmap.createBitmap(Bitmap.java:435)
at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:340)
at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:488)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:462)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:323)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
at android.content.res.Resources.loadDrawable(Resources.java:1709)
at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
at android.view.View.<init>(View.java:1885)
at android.widget.TextView.<init>(TextView.java:352)
at android.widget.Button.<init>(Button.java:69)
... 26 more
==
here's my xml layout:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:background="#drawable/q9popup" android:id="#+id/qnine"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</Button>
<com.google.ads.AdView android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="**********"
ads:adSize="BANNER"
ads:loadAdOnCreate="true"/>
</AbsoluteLayout>
I don't understand why I keep getting this error :S
Please help.
Thanks
Android cannot allocate the memory for the Button. Pre-Honeycomb allocates the memory for a bitmap natively, outside the Dalvik VM, using malloc and has to rely on Java finalizers to free that memory again. Unless you call Bitmap.recycle that is.
See http://www.youtube.com/watch?v=_CruQY55HOk&feature=player_detailpage#t=650s for some info on allocation of Bitmaps.
So you have to figure out why external allocation is so high in your app. Maybe AdMob is the cause. Make sure to invoke AdView.destroy in your activity's onDestroy.

Categories

Resources