Android Calculator Bug - java

I coded a calculator for Android, but when I press the "equals" button while the app is running in the emulator, an error comes in the LogCat and the app closes.
Please help me fix the error.
Here's the LogCat Output:
07-25 01:34:13.213: I/Choreographer(1387): Skipped 39 frames! The application may be doing too much work on its main thread.
07-25 01:34:13.473: D/gralloc_goldfish(1387): Emulator without GPU emulation detected.
07-25 01:34:50.653: I/Choreographer(1387): Skipped 36 frames! The application may be doing too much work on its main thread.
07-25 01:34:52.973: D/AndroidRuntime(1387): Shutting down VM
07-25 01:34:52.973: W/dalvikvm(1387): threadid=1: thread exiting with uncaught exception (group=0xb3ab0ba8)
07-25 01:34:53.103: E/AndroidRuntime(1387): FATAL EXCEPTION: main
07-25 01:34:53.103: E/AndroidRuntime(1387): Process: com.example.owncode, PID: 1387
07-25 01:34:53.103: E/AndroidRuntime(1387): java.lang.IllegalStateException: Could not find a method Sum(View) in the activity class com.example.owncode.MainActivity for onClick handler on view class android.widget.Button with id 'button_calculate'
07-25 01:34:53.103: E/AndroidRuntime(1387): at android.view.View$1.onClick(View.java:3810)
07-25 01:34:53.103: E/AndroidRuntime(1387): at android.view.View.performClick(View.java:4438)
07-25 01:34:53.103: E/AndroidRuntime(1387): at android.view.View$PerformClick.run(View.java:18422)
07-25 01:34:53.103: E/AndroidRuntime(1387): at android.os.Handler.handleCallback(Handler.java:733)
07-25 01:34:53.103: E/AndroidRuntime(1387): at android.os.Handler.dispatchMessage(Handler.java:95)
07-25 01:34:53.103: E/AndroidRuntime(1387): at android.os.Looper.loop(Looper.java:136)
07-25 01:34:53.103: E/AndroidRuntime(1387): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-25 01:34:53.103: E/AndroidRuntime(1387): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 01:34:53.103: E/AndroidRuntime(1387): at java.lang.reflect.Method.invoke(Method.java:515)
07-25 01:34:53.103: E/AndroidRuntime(1387): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-25 01:34:53.103: E/AndroidRuntime(1387): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-25 01:34:53.103: E/AndroidRuntime(1387): at dalvik.system.NativeStart.main(Native Method)
07-25 01:34:53.103: E/AndroidRuntime(1387): Caused by: java.lang.NoSuchMethodException: Sum [class android.view.View]
07-25 01:34:53.103: E/AndroidRuntime(1387): at java.lang.Class.getConstructorOrMethod(Class.java:472)
07-25 01:34:53.103: E/AndroidRuntime(1387): at java.lang.Class.getMethod(Class.java:857)
07-25 01:34:53.103: E/AndroidRuntime(1387): at android.view.View$1.onClick(View.java:3803)
07-25 01:34:53.103: E/AndroidRuntime(1387): ... 11 more
07-25 01:36:28.423: I/Choreographer(1452): Skipped 52 frames! The application may be doing too much work on its main thread.
07-25 01:36:28.753: D/gralloc_goldfish(1452): Emulator without GPU emulation detected.
07-25 01:36:32.873: I/Choreographer(1452): Skipped 39 frames! The application may be doing too much work on its main thread.
07-25 01:36:35.713: W/ResourceType(1452): No package identifier when getting value for resource number 0x00000009
07-25 01:36:35.723: D/AndroidRuntime(1452): Shutting down VM
07-25 01:36:35.723: W/dalvikvm(1452): threadid=1: thread exiting with uncaught exception (group=0xb3ab0ba8)
07-25 01:36:36.103: E/AndroidRuntime(1452): FATAL EXCEPTION: main
07-25 01:36:36.103: E/AndroidRuntime(1452): Process: com.example.owncode, PID: 1452
07-25 01:36:36.103: E/AndroidRuntime(1452): java.lang.IllegalStateException: Could not execute method of the activity
07-25 01:36:36.103: E/AndroidRuntime(1452): at android.view.View$1.onClick(View.java:3823)
07-25 01:36:36.103: E/AndroidRuntime(1452): at android.view.View.performClick(View.java:4438)
07-25 01:36:36.103: E/AndroidRuntime(1452): at android.view.View$PerformClick.run(View.java:18422)
07-25 01:36:36.103: E/AndroidRuntime(1452): at android.os.Handler.handleCallback(Handler.java:733)
07-25 01:36:36.103: E/AndroidRuntime(1452): at android.os.Handler.dispatchMessage(Handler.java:95)
07-25 01:36:36.103: E/AndroidRuntime(1452): at android.os.Looper.loop(Looper.java:136)
07-25 01:36:36.103: E/AndroidRuntime(1452): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-25 01:36:36.103: E/AndroidRuntime(1452): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 01:36:36.103: E/AndroidRuntime(1452): at java.lang.reflect.Method.invoke(Method.java:515)
07-25 01:36:36.103: E/AndroidRuntime(1452): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-25 01:36:36.103: E/AndroidRuntime(1452): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-25 01:36:36.103: E/AndroidRuntime(1452): at dalvik.system.NativeStart.main(Native Method)
07-25 01:36:36.103: E/AndroidRuntime(1452): Caused by: java.lang.reflect.InvocationTargetException
07-25 01:36:36.103: E/AndroidRuntime(1452): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 01:36:36.103: E/AndroidRuntime(1452): at java.lang.reflect.Method.invoke(Method.java:515)
07-25 01:36:36.103: E/AndroidRuntime(1452): at android.view.View$1.onClick(View.java:3818)
07-25 01:36:36.103: E/AndroidRuntime(1452): ... 11 more
07-25 01:36:36.103: E/AndroidRuntime(1452): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x9
07-25 01:36:36.103: E/AndroidRuntime(1452): at android.content.res.Resources.getText(Resources.java:244)
07-25 01:36:36.103: E/AndroidRuntime(1452): at android.widget.TextView.setText(TextView.java:3888)
07-25 01:36:36.103: E/AndroidRuntime(1452): at com.example.owncode.MainActivity.Sum(MainActivity.java:113)
07-25 01:36:36.103: E/AndroidRuntime(1452): ... 14 more
07-25 01:39:31.103: I/Choreographer(1502): Skipped 47 frames! The application may be doing too much work on its main thread.
07-25 01:39:31.383: D/gralloc_goldfish(1502): Emulator without GPU emulation detected.
07-25 01:39:35.203: I/Choreographer(1502): Skipped 42 frames! The application may be doing too much work on its main thread.
07-25 01:39:38.363: W/ResourceType(1502): No package identifier when getting value for resource number 0x00000009
07-25 01:39:38.423: D/AndroidRuntime(1502): Shutting down VM
07-25 01:39:38.423: W/dalvikvm(1502): threadid=1: thread exiting with uncaught exception (group=0xb3ab0ba8)
07-25 01:39:38.603: E/AndroidRuntime(1502): FATAL EXCEPTION: main
07-25 01:39:38.603: E/AndroidRuntime(1502): Process: com.example.owncode, PID: 1502
07-25 01:39:38.603: E/AndroidRuntime(1502): java.lang.IllegalStateException: Could not execute method of the activity
07-25 01:39:38.603: E/AndroidRuntime(1502): at android.view.View$1.onClick(View.java:3823)
07-25 01:39:38.603: E/AndroidRuntime(1502): at android.view.View.performClick(View.java:4438)
07-25 01:39:38.603: E/AndroidRuntime(1502): at android.view.View$PerformClick.run(View.java:18422)
07-25 01:39:38.603: E/AndroidRuntime(1502): at android.os.Handler.handleCallback(Handler.java:733)
07-25 01:39:38.603: E/AndroidRuntime(1502): at android.os.Handler.dispatchMessage(Handler.java:95)
07-25 01:39:38.603: E/AndroidRuntime(1502): at android.os.Looper.loop(Looper.java:136)
07-25 01:39:38.603: E/AndroidRuntime(1502): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-25 01:39:38.603: E/AndroidRuntime(1502): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 01:39:38.603: E/AndroidRuntime(1502): at java.lang.reflect.Method.invoke(Method.java:515)
07-25 01:39:38.603: E/AndroidRuntime(1502): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-25 01:39:38.603: E/AndroidRuntime(1502): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-25 01:39:38.603: E/AndroidRuntime(1502): at dalvik.system.NativeStart.main(Native Method)
07-25 01:39:38.603: E/AndroidRuntime(1502): Caused by: java.lang.reflect.InvocationTargetException
07-25 01:39:38.603: E/AndroidRuntime(1502): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 01:39:38.603: E/AndroidRuntime(1502): at java.lang.reflect.Method.invoke(Method.java:515)
07-25 01:39:38.603: E/AndroidRuntime(1502): at android.view.View$1.onClick(View.java:3818)
07-25 01:39:38.603: E/AndroidRuntime(1502): ... 11 more
07-25 01:39:38.603: E/AndroidRuntime(1502): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x9
07-25 01:39:38.603: E/AndroidRuntime(1502): at android.content.res.Resources.getText(Resources.java:244)
07-25 01:39:38.603: E/AndroidRuntime(1502): at android.widget.TextView.setText(TextView.java:3888)
07-25 01:39:38.603: E/AndroidRuntime(1502): at com.example.owncode.MainActivity.Sum(MainActivity.java:113)
07-25 01:39:38.603: E/AndroidRuntime(1502): ... 14 more
Here's my MainActivity.JAVA:
package com.example.owncode;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText calculatorText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
calculatorText = (EditText) findViewById(R.id.calculatorText);
}
#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_activity_actions, menu);
return super.onCreateOptionsMenu(menu);*/
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.button_quitProgram) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void button_one_click(View view){
calculatorText.append("1");
}
public void button_two_click(View view){
calculatorText.append("2");
}
public void button_three_click(View view){
calculatorText.append("3");
}
public void button_four_click(View view){
calculatorText.append("4");
}
public void button_five_click(View view){
calculatorText.append("5");
}
public void button_six_click(View view){
calculatorText.append("6");
}
public void button_seven_click(View view){
calculatorText.append("7");
}
public void button_eight_click(View view){
calculatorText.append("8");
}
public void button_nine_click(View view){
calculatorText.append("9");
}
public void button_zero_click(View view){
calculatorText.append("0");
}
public void button_plus_click(View view){
calculatorText.append("+");
}
public void button_minus_click(View view){
calculatorText.append("-");
}
public void button_backspace_click(View view){
if (calculatorText.length()>0){
String calculatorText_text = calculatorText.getText().toString();
calculatorText_text = calculatorText_text.substring(0, calculatorText_text.length()-1);
calculatorText.setText(calculatorText_text);
}
}
public void Sum(View view){
String str = calculatorText.getText().toString();
int len = str.length();
int i=0;
String str1 = "", str2 = "";
char op;
int num1 = 0, num2 = 0, res=0;
for(i=0; i<len; i++){
if(str.charAt(i)=='+' || str.charAt(i)=='-')
break;
str1 = str1 + str.charAt(i);
}
op = str.charAt(i);
i++;
while(i<len){
str2 = str2 + str.charAt(i);
i++;
}
num1 = Integer.parseInt(str1);
num2 = Integer.parseInt(str2);
if(op=='+')
res = num1+num2;
else if(op=='-')
res = num1-num2;
calculatorText.setText(res);
}
}
And, here's my main_activity.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"
android:orientation="horizontal" >
<Button
android:id="#+id/button_plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button_minus"
android:layout_alignBottom="#+id/button_minus"
android:layout_alignParentLeft="true"
android:hint="#string/button_plus"
android:onClick="button_plus_click" />
<Button
android:id="#+id/button_zero"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button_plus"
android:layout_alignBottom="#+id/button_plus"
android:layout_toRightOf="#+id/button_plus"
android:hint="#string/button_zero"
android:onClick="button_zero_click" />
<Button
android:id="#+id/button_minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="135dp"
android:hint="#string/button_minus"
android:onClick="button_minus_click" />
<Button
android:id="#+id/button_nine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button_minus"
android:layout_alignLeft="#+id/button_minus"
android:hint="#string/button_nine"
android:onClick="button_nine_click" />
<Button
android:id="#+id/button_eight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button_zero"
android:layout_toLeftOf="#+id/button_minus"
android:hint="#string/button_eight"
android:onClick="button_eight_click" />
<Button
android:id="#+id/button_seven"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button_eight"
android:layout_alignBottom="#+id/button_eight"
android:layout_toLeftOf="#+id/button_eight"
android:hint="#string/button_seven"
android:onClick="button_seven_click" />
<Button
android:id="#+id/button_six"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button_nine"
android:layout_toRightOf="#+id/button_eight"
android:hint="#string/button_six"
android:onClick="button_six_click" />
<Button
android:id="#+id/button_five"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button_six"
android:layout_alignBottom="#+id/button_six"
android:layout_toRightOf="#+id/button_seven"
android:hint="#string/button_five"
android:onClick="button_five_click" />
<Button
android:id="#+id/button_four"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button_five"
android:layout_alignBottom="#+id/button_five"
android:layout_toLeftOf="#+id/button_five"
android:hint="#string/button_four"
android:onClick="button_four_click" />
<Button
android:id="#+id/button_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button_six"
android:layout_toRightOf="#+id/button_five"
android:hint="#string/button_three"
android:onClick="button_three_click" />
<Button
android:id="#+id/button_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button_three"
android:layout_alignBottom="#+id/button_three"
android:layout_toRightOf="#+id/button_four"
android:hint="#string/button_two"
android:onClick="button_two_click" />
<Button
android:id="#+id/button_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button_two"
android:layout_alignBottom="#+id/button_two"
android:layout_toLeftOf="#+id/button_two"
android:hint="#string/button_one"
android:onClick="button_one_click" />
<Button
android:id="#+id/button_backspace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button_three"
android:layout_marginBottom="29dp"
android:layout_marginLeft="17dp"
android:layout_toRightOf="#+id/calculatorText"
android:hint="#string/button_backspace"
android:onClick="button_backspace_click" />
<Button
android:id="#+id/button_calculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button_backspace"
android:layout_alignLeft="#+id/button_backspace"
android:hint="#string/button_calculate"
android:onClick="Sum" />
<EditText
android:id="#+id/calculatorText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button_backspace"
android:layout_alignParentLeft="true"
android:editable="false"
android:ems="10"
android:hint="#string/calculatorText_text" >
<requestFocus />
</EditText>
</RelativeLayout>
I know it's all simple code but this is my first app and please help me so that I can get it running on a few of my relatives' devices.

Because of this line you are getting error
calculatorText.setText(res);
change it into
calculatorText.setText(""+res);
You are setting Integer value..so View taken it as a Resouce ID.

Change calculatorText.setText(res); to calculatorText.setText(Integer.toString(res));
You cannot set an integer value to an EditText.

Related

Passing Integer Values Between Activities - Android

Other stuck posts unfortunately couldn't help me.
When I clicked button while easy radiobutton is checked, the app stops working. I couldn't go and see another activity.
Sender Side:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(radiobutton_arm_triceps_easy.isChecked()) {
String dene = "my example test";
int myValue=2;
Intent intent = new Intent(getApplicationContext(), exercise_arm_triceps_execute.class);
intent.putExtra("attempt1", myValue );
startActivity(intent);
}
}
});
Receiver Side:
int receiveValue=getIntent().getIntExtra("attempt1",0);
textshow.setText(receiveValue);
LOGCAT
04-26 16:52:06.320 31527-31527/com.example.kerem.tutorial_project E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kerem.tutorial_project, PID: 31527
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kerem.tutorial_project/com.example.kerem.tutorial_project.exercise_arm_triceps_execute}: android.content.res.Resources$NotFoundException: String resource ID #0x2
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2
at android.content.res.Resources.getText(Resources.java:244)
at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
at android.widget.TextView.setText(TextView.java:3888)
at com.example.kerem.tutorial_project.exercise_arm_triceps_execute.onCreate(exercise_arm_triceps_execute.java:28)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Use
textshow.setText(String.valueOf(receiveValue));

ProgressBar inside of Fragment class causing NullPointerException at setMax method?

#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.analogin_fragment,
container, false);
AITVVOLT0 = (TextView) rootView.findViewById(R.id.aintvvolt0);
AITVVOLT1 = (TextView) rootView.findViewById(R.id.aintvvolt1);
AITVVOLT2 = (TextView) rootView.findViewById(R.id.aintvvolt2);
AITVVOLT3 = (TextView) rootView.findViewById(R.id.aintvvolt3);
AITVVOLT4 = (TextView) rootView.findViewById(R.id.aintvvolt4);
AITVVOLT5 = (TextView) rootView.findViewById(R.id.aintvvolt5);
AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar0);
AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar1);
AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar2);
AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar3);
AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar4);
AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar5);
AIVOLTBAR0.setMax(100);
AIVOLTBAR1.setMax(100);
AIVOLTBAR2.setMax(100);
AIVOLTBAR3.setMax(100);
AIVOLTBAR4.setMax(100);
AIVOLTBAR5.setMax(100);
AIVOLTBAR0.setProgress(0);
AIVOLTBAR1.setProgress(0);
AIVOLTBAR2.setProgress(0);
AIVOLTBAR3.setProgress(0);
AIVOLTBAR4.setProgress(0);
AIVOLTBAR5.setProgress(0);
return rootView;
}
The AIVOLTBAR#.setMax(); methods are causing a NullPointerException - but the ProgressBars are not null (Tested with a log as well as commenting out the setMax causes the program to work).
Logcat:
08-20 16:23:18.409: E/AndroidRuntime(1332): Process: com.nanospark.androidapp, PID: 1332
08-20 16:23:18.409: E/AndroidRuntime(1332): java.lang.NullPointerException
08-20 16:23:18.409: E/AndroidRuntime(1332): at com.nanospark.androidapp.MainActivity$AnalogInputFragment.onCreateView(MainActivity.java:218)
08-20 16:23:18.409: E/AndroidRuntime(1332): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
08-20 16:23:18.409: E/AndroidRuntime(1332): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
08-20 16:23:18.409: E/AndroidRuntime(1332): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
08-20 16:23:18.409: E/AndroidRuntime(1332): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
08-20 16:23:18.409: E/AndroidRuntime(1332): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
08-20 16:23:18.409: E/AndroidRuntime(1332): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
08-20 16:23:18.409: E/AndroidRuntime(1332): at android.os.Handler.handleCallback(Handler.java:733)
08-20 16:23:18.409: E/AndroidRuntime(1332): at android.os.Handler.dispatchMessage(Handler.java:95)
08-20 16:23:18.409: E/AndroidRuntime(1332): at android.os.Looper.loop(Looper.java:136)
08-20 16:23:18.409: E/AndroidRuntime(1332): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-20 16:23:18.409: E/AndroidRuntime(1332): at java.lang.reflect.Method.invokeNative(Native Method)
08-20 16:23:18.409: E/AndroidRuntime(1332): at java.lang.reflect.Method.invoke(Method.java:515)
08-20 16:23:18.409: E/AndroidRuntime(1332): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-20 16:23:18.409: E/AndroidRuntime(1332): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-20 16:23:18.409: E/AndroidRuntime(1332): at dalvik.system.NativeStart.main(Native Method)
08-20 16:27:27.557: E/BluetoothAdapter(1387): Bluetooth binder is null
08-20 16:27:27.853: E/OpenGLRenderer(1387): Getting MAX_TEXTURE_SIZE from GradienCache
08-20 16:27:27.853: E/OpenGLRenderer(1387): MAX_TEXTURE_SIZE: 16384
08-20 16:27:27.865: E/OpenGLRenderer(1387): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
08-20 16:27:27.869: E/OpenGLRenderer(1387): MAX_TEXTURE_SIZE: 16384
08-20 16:27:28.409: E/AndroidRuntime(1387): FATAL EXCEPTION: main
08-20 16:27:28.409: E/AndroidRuntime(1387): Process: com.nanospark.androidapp, PID: 1387
08-20 16:27:28.409: E/AndroidRuntime(1387): java.lang.NullPointerException
08-20 16:27:28.409: E/AndroidRuntime(1387): at com.nanospark.androidapp.MainActivity$AnalogInputFragment.onCreateView(MainActivity.java:223)
08-20 16:27:28.409: E/AndroidRuntime(1387): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
08-20 16:27:28.409: E/AndroidRuntime(1387): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
08-20 16:27:28.409: E/AndroidRuntime(1387): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
08-20 16:27:28.409: E/AndroidRuntime(1387): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
08-20 16:27:28.409: E/AndroidRuntime(1387): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
08-20 16:27:28.409: E/AndroidRuntime(1387): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
08-20 16:27:28.409: E/AndroidRuntime(1387): at android.os.Handler.handleCallback(Handler.java:733)
08-20 16:27:28.409: E/AndroidRuntime(1387): at android.os.Handler.dispatchMessage(Handler.java:95)
08-20 16:27:28.409: E/AndroidRuntime(1387): at android.os.Looper.loop(Looper.java:136)
08-20 16:27:28.409: E/AndroidRuntime(1387): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-20 16:27:28.409: E/AndroidRuntime(1387): at java.lang.reflect.Method.invokeNative(Native Method)
08-20 16:27:28.409: E/AndroidRuntime(1387): at java.lang.reflect.Method.invoke(Method.java:515)
08-20 16:27:28.409: E/AndroidRuntime(1387): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-20 16:27:28.409: E/AndroidRuntime(1387): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-20 16:27:28.409: E/AndroidRuntime(1387): at dalvik.system.NativeStart.main(Native Method)
The problem is that you have some typos. You're setting AIVOLTBAR0 multiple times instead of setting each progress bar individually:
AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar0);
AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar1);
AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar2);
AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar3);
AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar4);
AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar5);
Instead it needs to be this:
AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar0);
AIVOLTBAR1 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar1);
AIVOLTBAR2 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar2);
AIVOLTBAR3 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar3);
AIVOLTBAR4 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar4);
AIVOLTBAR5 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar5);

Application successfully installed but stops working when run in emulator

I am trying to create a simple application that can sign in / sign up users . All the data are
stored in Mysql database .
My app was installed successfully but as I opened it, it gave an error saying "Unfortunately, 'myapp' has stop working".
I have read almost all related problems posted here but no luck!
here's from logcat .
07-25 01:22:31.452: E/AndroidRuntime(2101): FATAL EXCEPTION: main
07-25 01:22:31.452: E/AndroidRuntime(2101): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mysqltest/com.example.mobileapp.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class android.widget.RelativeLayout
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.os.Handler.dispatchMessage(Handler.java:99)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.os.Looper.loop(Looper.java:137)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.app.ActivityThread.main(ActivityThread.java:5103)
07-25 01:22:31.452: E/AndroidRuntime(2101): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 01:22:31.452: E/AndroidRuntime(2101): at java.lang.reflect.Method.invoke(Method.java:525)
07-25 01:22:31.452: E/AndroidRuntime(2101): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
07-25 01:22:31.452: E/AndroidRuntime(2101): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-25 01:22:31.452: E/AndroidRuntime(2101): at dalvik.system.NativeStart.main(Native Method)
07-25 01:22:31.452: E/AndroidRuntime(2101): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.widget.RelativeLayout
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.LayoutInflater.createView(LayoutInflater.java:620)
07-25 01:22:31.452: E/AndroidRuntime(2101): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
07-25 01:22:31.452: E/AndroidRuntime(2101): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.app.Activity.setContentView(Activity.java:1895)
07-25 01:22:31.452: E/AndroidRuntime(2101): at com.example.mobileapp.MainActivity.onCreate(MainActivity.java:45)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.app.Activity.performCreate(Activity.java:5133)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
07-25 01:22:31.452: E/AndroidRuntime(2101): ... 11 more
07-25 01:22:31.452: E/AndroidRuntime(2101): Caused by: java.lang.reflect.InvocationTargetException
07-25 01:22:31.452: E/AndroidRuntime(2101): at java.lang.reflect.Constructor.constructNative(Native Method)
07-25 01:22:31.452: E/AndroidRuntime(2101): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.LayoutInflater.createView(LayoutInflater.java:594)
07-25 01:22:31.452: E/AndroidRuntime(2101): ... 23 more
07-25 01:22:31.452: E/AndroidRuntime(2101): Caused by: java.lang.OutOfMemoryError
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:503)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:356)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:800)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.content.res.Resources.loadDrawable(Resources.java:2105)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.View.<init>(View.java:3364)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.View.<init>(View.java:3293)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.ViewGroup.<init>(ViewGroup.java:453)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.widget.RelativeLayout.<init>(RelativeLayout.java:242)
activity_main.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:background="#drawable/background" >
<TextView
android:id="#+id/tv2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/password"
android:layout_alignParentLeft="true"
android:layout_marginBottom="14dp"
android:text="Password" />
<TextView
android:id="#+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="20dp" />
<TextView
android:id="#+id/tv0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/tv1"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="38dp"
android:gravity="center"
android:text="PhilTaxWindow User Login"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/username"
android:layout_alignParentLeft="true"
android:layout_marginBottom="18dp"
android:text="Username" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/register"
android:layout_alignBottom="#+id/register"
android:layout_marginRight="17dp"
android:layout_toLeftOf="#+id/register"
android:text="Don't Have an Account?"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="#+id/Button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/register"
android:layout_alignParentLeft="true"
android:layout_marginBottom="60dp"
android:text="Login" />
<EditText
android:id="#+id/username"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/tv2"
android:layout_alignParentLeft="true"
android:layout_marginBottom="22dp"
android:ems="10"
android:inputType="textPersonName"
android:singleLine="true" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/Button01"
android:layout_alignParentLeft="true"
android:layout_marginBottom="31dp"
android:ems="10"
android:inputType="textPassword"
android:singleLine="true" />
<Button
android:id="#+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/tv"
android:layout_alignParentRight="true"
android:layout_marginBottom="17dp"
android:layout_marginRight="29dp"
android:text="Register here" />
</RelativeLayout>
mainactivity.java
package com.example.mobileapp;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import com.example.mysqltest.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
private EditText Username, Password;
private Button Submit, Register;
private ProgressDialog Dialog;
JSONParser jsonParser = new JSONParser();
private static final String LOGIN_URL = "http://192.168.1.4/checkingPHP/CheckUser.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Username = (EditText)findViewById(R.id.username);
Password = (EditText)findViewById(R.id.password);
Submit = (Button)findViewById(R.id.Button01);
Register = (Button)findViewById(R.id.register);
Submit.setOnClickListener(this);
Register.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.Button01:
new AttemptLogin().execute();
break;
case R.id.register:
Intent i = new Intent(this, Register.class);
startActivity(i);
break;
default:
break;
}
}
class AttemptLogin extends AsyncTask<String, String, String> {
boolean failure = false;
#Override
protected void onPreExecute() {
super.onPreExecute();
Dialog = new ProgressDialog(MainActivity.this);
Dialog.setMessage("Validating...");
Dialog.setIndeterminate(false);
Dialog.setCancelable(true);
Dialog.show();
}
#Override
protected String doInBackground(String... args) {
int success;
String username = Username.getText().toString();
String password = Password.getText().toString();
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("Username", username));
params.add(new BasicNameValuePair("Password", password));
Log.d("request!", "starting");
JSONObject json = jsonParser.makeHttpRequest(
LOGIN_URL, "POST", params);
Log.d("Login attempt", json.toString());
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Login Successful!", json.toString());
Intent i = new Intent(MainActivity.this, WelcomPage.class);
finish();
startActivity(i);
return json.getString(TAG_MESSAGE);
}else{
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(String file_url) {
Dialog.dismiss();
if (file_url != null){
Toast.makeText(MainActivity.this, file_url, Toast.LENGTH_LONG).show();
}
}
}
}
The emulator ran into out of memory exception
Caused by: java.lang.OutOfMemoryError due to
android:background="#drawable/background"
The image that you are trying to load is causing this. Try reducing the image resolution.

Android application keep crashing

in the last few days I tried to develop android applications with no success, for some reason my application keep crashing.
the application keeps throwing few exceptions that I didn't succeeded to handle with.
MainActivity.java :
package com.mobIce.digicoin.digitalcoin;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends ActionBarActivity {
private List<Coin> coins;
public MainActivity(){
coins = new ArrayList<Coin>();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
populateCoins();
populateListView();
}
}
private void populateCoins() {
coins.add(new Coin("Bitcoin", "650", "4", "http://coinmarketcap.com/img/Bitcoin.png"));
coins.add(new Coin("Dogecoin", "100", "400", "http://coinmarketcap.com/img/Bitcoin.png"));
coins.add(new Coin("fafaf", "8065", "400", "http://coinmarketcap.com/img/Bitcoin.png"));
}
private void populateListView() {
ArrayAdapter<Coin> adapter = new MyListAdapter();
ListView list = (ListView) findViewById(R.id.coinsListView);
list.setAdapter(adapter);
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
private class MyListAdapter extends ArrayAdapter<Coin> {
public MyListAdapter()
{
super(MainActivity.this, R.layout.coin_layout, coins);
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
View itemView = convertView;
if(itemView == null)
itemView = getLayoutInflater().inflate(R.layout.coin_layout, parent, false);
//find the coin to work with.
Coin currentCoin = coins.get(position);
//fill the view
try{
TextView coinName = (TextView) itemView.findViewById(R.id.coinName);
coinName.setText(currentCoin.getName());
TextView coinPrice = (TextView) itemView.findViewById(R.id.coinPrice);
coinPrice.setText(currentCoin.getPrice());
TextView coinPercentage = (TextView) itemView.findViewById(R.id.coinPercentage);
coinPercentage.setText(currentCoin.getPercentage());
}
catch (NullPointerException e){ }
return itemView;
}
}
}
activity_main.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mobIce.digicoin.digitalcoin.MainActivity"
tools:ignore="MergeRootFrame" />
fragment_main.xml:
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/coinsListView"
android:paddingLeft="30dp" />
</LinearLayout>
coin_layout.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:padding="8dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="coinName"
android:id="#+id/coinName"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/coinIcon"
android:layout_marginTop="35dp"
android:layout_marginLeft="20dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/coinIcon"
android:maxHeight="80dp"
android:maxWidth="80dp"
android:minHeight="80dp"
android:minWidth="80dp"
android:src="#drawable/dogecoin"
android:longClickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:baselineAlignBottom="false"
android:clickable="false"
android:adjustViewBounds="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/coinPrice"
android:layout_marginLeft="35dp"
android:layout_alignTop="#+id/coinIcon"
android:layout_toRightOf="#+id/coinName"
android:layout_marginTop="25dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/coinPercentage"
android:layout_below="#+id/coinPrice"
android:layout_alignLeft="#+id/coinPrice"
android:layout_alignStart="#+id/coinPrice" />
</RelativeLayout>
logcat trace:
03-17 08:59:33.977 1107-1107/com.mobIce.digicoin.digitalcoin D/AndroidRuntime﹕ Shutting down VM
03-17 08:59:33.977 1107-1107/com.mobIce.digicoin.digitalcoin W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1a38ba8)
03-17 08:59:33.987 1107-1107/com.mobIce.digicoin.digitalcoin E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.mobIce.digicoin.digitalcoin, PID: 1107
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobIce.digicoin.digitalcoin/com.mobIce.digicoin.digitalcoin.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.mobIce.digicoin.digitalcoin.MainActivity.populateListView(MainActivity.java:47)
at com.mobIce.digicoin.digitalcoin.MainActivity.onCreate(MainActivity.java:36)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
03-17 09:04:09.857 1149-1149/com.mobIce.digicoin.digitalcoin D/AndroidRuntime﹕ Shutting down VM
03-17 09:04:09.857 1149-1149/com.mobIce.digicoin.digitalcoin W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1a38ba8)
03-17 09:04:09.867 1149-1149/com.mobIce.digicoin.digitalcoin E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.mobIce.digicoin.digitalcoin, PID: 1149
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobIce.digicoin.digitalcoin/com.mobIce.digicoin.digitalcoin.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.mobIce.digicoin.digitalcoin.MainActivity.populateListView(MainActivity.java:50)
at com.mobIce.digicoin.digitalcoin.MainActivity.onCreate(MainActivity.java:39)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
03-17 09:06:36.697 1192-1192/com.mobIce.digicoin.digitalcoin D/AndroidRuntime﹕ Shutting down VM
03-17 09:06:36.697 1192-1192/com.mobIce.digicoin.digitalcoin W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1a38ba8)
03-17 09:06:36.717 1192-1192/com.mobIce.digicoin.digitalcoin E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.mobIce.digicoin.digitalcoin, PID: 1192
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobIce.digicoin.digitalcoin/com.mobIce.digicoin.digitalcoin.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.mobIce.digicoin.digitalcoin.MainActivity.populateListView(MainActivity.java:50)
at com.mobIce.digicoin.digitalcoin.MainActivity.onCreate(MainActivity.java:39)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
03-17 09:13:38.647 1239-1239/com.mobIce.digicoin.digitalcoin D/AndroidRuntime﹕ Shutting down VM
03-17 09:13:38.647 1239-1239/com.mobIce.digicoin.digitalcoin W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1a38ba8)
03-17 09:13:38.747 1239-1239/com.mobIce.digicoin.digitalcoin E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.mobIce.digicoin.digitalcoin, PID: 1239
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobIce.digicoin.digitalcoin/com.mobIce.digicoin.digitalcoin.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.mobIce.digicoin.digitalcoin.MainActivity.populateListView(MainActivity.java:50)
at com.mobIce.digicoin.digitalcoin.MainActivity.onCreate(MainActivity.java:37)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
03-17 09:13:45.127 1239-1239/com.mobIce.digicoin.digitalcoin I/Process﹕ Sending signal. PID: 1239 SIG: 9
03-17 09:19:34.847 1285-1285/com.mobIce.digicoin.digitalcoin D/AndroidRuntime﹕ Shutting down VM
03-17 09:19:34.847 1285-1285/com.mobIce.digicoin.digitalcoin W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1a38ba8)
03-17 09:19:34.857 1285-1285/com.mobIce.digicoin.digitalcoin E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.mobIce.digicoin.digitalcoin, PID: 1285
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobIce.digicoin.digitalcoin/com.mobIce.digicoin.digitalcoin.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.mobIce.digicoin.digitalcoin.MainActivity.populateListView(MainActivity.java:50)
at com.mobIce.digicoin.digitalcoin.MainActivity.onCreate(MainActivity.java:37)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
03-17 09:20:11.987 1285-1285/com.mobIce.digicoin.digitalcoin I/Process﹕ Sending signal. PID: 1285 SIG: 9
Your coinsListView is in the fragment layout and not in activity layout. You cannot access it with findViewById() in populateListView() called from activity onCreate(). Instead, move the list view population to the fragment's onCreateView() after the inflation, calling findViewById() on the rootView layout you just inflated.

Unfortunately app has stopped working

I am new to android application development. I was doing this tutorial app.It's a very simple one. It adds one and subtracts one from the counter.When I run it in the emulator ,it says "Unfortunately tutorial has stopped working." There are no errors in the code. The API level is 17. Please help me out.
Code for java:
public class Startingpoint extends Activity {
int counter=0;
Button add,subtract;
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.startingpoint);
add = (Button) findViewById(R.id.bAdd);
subtract= (Button) findViewById(R.id.bSubtract);
display= (Button) findViewById(R.id.text);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter++;
display.setText("The total is " + counter);
}
});
subtract.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter--;
display.setText("The total is " + counter);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.startingpoint, menu);
return true;
}
}
Layout code in 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/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your total is 0"
android:textSize="35dp"
android:layout_gravity="center"
android:gravity="center"/>
<Button android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Add One"
android:layout_gravity="center"
android:textSize="25dp"
android:id="#+id/bAdd"/>
<Button android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Subtract One"
android:layout_gravity="center"
android:textSize="25dp"
android:id="#+id/bSubtract"/>
</LinearLayout>
Here is the logcat :
03-02 02:45:10.730: D/AndroidRuntime(780): Shutting down VM
03-02 02:45:10.730: W/dalvikvm(780): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-02 02:45:10.750: E/AndroidRuntime(780): FATAL EXCEPTION: main
03-02 02:45:10.750: E/AndroidRuntime(780): java.lang.RuntimeException: Unable to start activity ComponentInfo{tutorial.example.tutorial/tutorial.example.tutorial.Startingpoint}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.os.Handler.dispatchMessage(Handler.java:99)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.os.Looper.loop(Looper.java:137)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-02 02:45:10.750: E/AndroidRuntime(780): at java.lang.reflect.Method.invokeNative(Native Method)
03-02 02:45:10.750: E/AndroidRuntime(780): at java.lang.reflect.Method.invoke(Method.java:511)
03-02 02:45:10.750: E/AndroidRuntime(780): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-02 02:45:10.750: E/AndroidRuntime(780): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-02 02:45:10.750: E/AndroidRuntime(780): at dalvik.system.NativeStart.main(Native Method)
03-02 02:45:10.750: E/AndroidRuntime(780): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
03-02 02:45:10.750: E/AndroidRuntime(780): at tutorial.example.tutorial.Startingpoint.onCreate(Startingpoint.java:22)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.Activity.performCreate(Activity.java:5104)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-02 02:45:10.750: E/AndroidRuntime(780): ... 11 more
display= (Button) findViewById(R.id.text);
should be
display= (TextView) findViewById(R.id.text);
Since display is supposed to reference a TextView instance, but you're explictly casting into a Button, and these are not compatible types.
As you got the answer from A-C this time, but for next time in android application development a important suggestion:
Always see the logcat for error, and see the "Caused by:" tag, It specifies what was the cause of the problem with sufficient detail, Also see the line no that caused that error.
And try to find what can be wrong with that line of code.
For example: in your logcat it is showing-
Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
at tutorial.example.tutorial.Startingpoint.onCreate(Startingpoint.java:22)
So you can try to read the log, and you will understand that in your file Startingpoint.java at line 22 which is located in onCreate method the error is android.widget.TextView cannot be cast to android.widget.Button. So you can easily remove your errors without any help.
Hope that helps not only you current problem but prevented your future time and efforts.

Categories