Application successfully installed but stops working when run in emulator - java

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.

Related

App keeps crashing, programmed in android studio

package kdev.circles;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText ca = (EditText)findViewById(R.id.ca);
final EditText cp = (EditText)findViewById(R.id.cp);
final EditText sv = (EditText)findViewById(R.id.sv);
final EditText r = (EditText)findViewById(R.id.radius);
final TextView log = (TextView)findViewById(R.id.log);
Button clear = (Button)findViewById(R.id.clear);
Button solve = (Button)findViewById(R.id.Solve);
clear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ca.setText("");
cp.setText("");
sv.setText("");
r.setText("");
}
});
solve.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if((r.toString()).equals("")) {
log.setText(log.getText() + "\n" + "error, no value entered");
}else{
sv.setText(String.valueOf(Double.valueOf(String.valueOf(r.getText())) * Double.valueOf(String.valueOf(r.getText())) * Double.valueOf(String.valueOf(r.getText())) * 4 / 3 * 3.14159265));
cp.setText(String.valueOf(Double.valueOf(String.valueOf(r.getText()))*3.14159265*2));
ca.setText(String.valueOf(Double.valueOf(String.valueOf(r.getText()))*Double.valueOf(String.valueOf(r.getText()))*3.14159265));
log.setText(log.getText()+"\n"+"ca"+ca.getText()+"\n"+"sv"+sv.getText()+"\n"+"cp"+cp.getText());
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
xml file:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Solve"
android:id="#+id/Solve"
android:width="100dp"
android:layout_alignParentBottom="true"
android:layout_alignEnd="#+id/sv" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/radius"
android:layout_alignParentTop="true"
android:layout_alignStart="#+id/editText" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editText"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="49dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editText2"
android:layout_below="#+id/editText"
android:layout_alignStart="#+id/editText" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editText3"
android:layout_below="#+id/editText2"
android:layout_alignStart="#+id/editText2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="circle&apos;s perimeter"
android:id="#+id/cp"
android:layout_above="#+id/editText2"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="circle&apos;s area"
android:id="#+id/ca"
android:layout_above="#+id/editText3"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="sphere&apos;s volume"
android:id="#+id/sv"
android:layout_alignBottom="#+id/editText3"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="radius"
android:id="#+id/textView3"
android:layout_alignBottom="#+id/radius"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="clear"
android:id="#+id/clear"
android:layout_alignBottom="#+id/Solve"
android:layout_alignStart="#+id/editText3" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/log"
android:layout_below="#+id/editText3"
android:layout_centerHorizontal="true"
android:layout_marginTop="57dp" />
Everything is working fine till i try opening it, i couldn't find the error, because my android virtual device wasn't connected to android studio properly. I am not sure what i should do. I did have another problem before but i realised that it was due to me using the findviewbyid function before the whole layout was loaded. I have solved that problem and tried a few different techniques to solve this problem but nothing worked.
When i try and run the app it just crashes, i am not sure why?
Any help will be usefull, thank you.
this is the logcat:
12-10 15:21:18.030 1907-1907/kdev.circles W/art﹕ Verification of java.lang.CharSequence android.support.v7.widget.ResourcesWrapper.getQuantityText(int, int) took 123.881364ms
12-10 15:21:18.520 1907-1907/kdev.circles D/AndroidRuntime﹕ Shutting down VM
12-10 15:21:18.520 1907-1907/kdev.circles E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: kdev.circles, PID: 1907
java.lang.RuntimeException: Unable to start activity ComponentInfo{kdev.circles/kdev.circles.MainActivity}: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText
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.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText
at kdev.circles.MainActivity.onCreate(MainActivity.java:21)
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.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-10 15:21:27.230 1907-1907/kdev.circles I/Process﹕ Sending signal. PID: 1907 SIG: 9
12-10 15:21:31.360 2153-2153/kdev.circles D/AndroidRuntime﹕ Shutting down VM
12-10 15:21:31.360 2153-2153/kdev.circles E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: kdev.circles, PID: 2153
java.lang.RuntimeException: Unable to start activity ComponentInfo{kdev.circles/kdev.circles.MainActivity}: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText
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.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText
at kdev.circles.MainActivity.onCreate(MainActivity.java:21)
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.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
Check your view types in Java code and their matching id's in xml. They have to be of same type.
You have EditText in Java, but matching view id in xml is TextView
For instance:
final EditText ca = (EditText)findViewById(R.id.ca);
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="circle&apos;s area"
android:id="#+id/ca"
android:layout_above="#+id/editText3"
android:layout_alignParentStart="true" />

Error in Fragment Activity in Android

I am trying a fragment app as per a tutorial and getting following error:
06-28 04:21:47.118: D/AndroidRuntime(1491): Shutting down VM
06-28 04:21:47.128: W/dalvikvm(1491): threadid=1: thread exiting with uncaught exception (group=0xb4af7b90)
06-28 04:21:47.158: E/AndroidRuntime(1491): FATAL EXCEPTION: main
06-28 04:21:47.158: E/AndroidRuntime(1491): Process: com.example.fragmentapp, PID: 1491
06-28 04:21:47.158: E/AndroidRuntime(1491): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragmentapp/com.example.fragmentapp.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.ActivityThread.access$700(ActivityThread.java:135)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.os.Handler.dispatchMessage(Handler.java:102)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.os.Looper.loop(Looper.java:137)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.ActivityThread.main(ActivityThread.java:4998)
06-28 04:21:47.158: E/AndroidRuntime(1491): at java.lang.reflect.Method.invokeNative(Native Method)
06-28 04:21:47.158: E/AndroidRuntime(1491): at java.lang.reflect.Method.invoke(Method.java:515)
06-28 04:21:47.158: E/AndroidRuntime(1491): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
06-28 04:21:47.158: E/AndroidRuntime(1491): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
06-28 04:21:47.158: E/AndroidRuntime(1491): at dalvik.system.NativeStart.main(Native Method)
06-28 04:21:47.158: E/AndroidRuntime(1491): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
06-28 04:21:47.158: E/AndroidRuntime(1491): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.Activity.setContentView(Activity.java:1928)
06-28 04:21:47.158: E/AndroidRuntime(1491): at com.example.fragmentapp.MainActivity.onCreate(MainActivity.java:13)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.Activity.performCreate(Activity.java:5243)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
06-28 04:21:47.158: E/AndroidRuntime(1491): ... 11 more
06-28 04:21:47.158: E/AndroidRuntime(1491): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.pavan.fragmentdemo.MyListFragment: make sure class name exists, is public, and has an empty constructor that is public
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.Fragment.instantiate(Fragment.java:597)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.Fragment.instantiate(Fragment.java:561)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.Activity.onCreateView(Activity.java:4777)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
06-28 04:21:47.158: E/AndroidRuntime(1491): ... 21 more
06-28 04:21:47.158: E/AndroidRuntime(1491): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.pavan.fragmentdemo.MyListFragment" on path: DexPathList[[zip file "/data/app/com.example.fragmentapp-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.fragmentapp-2, /system/lib]]
06-28 04:21:47.158: E/AndroidRuntime(1491): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
06-28 04:21:47.158: E/AndroidRuntime(1491): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
06-28 04:21:47.158: E/AndroidRuntime(1491): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.Fragment.instantiate(Fragment.java:583)
06-28 04:21:47.158: E/AndroidRuntime(1491): ... 24 more
The code is as follows:
Main Activity.java
package com.example.fragmentapp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
public class MainActivity extends Activity implements
ListFragment.Communicator {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void Message(String OS_Name) {
DetailFragment detailfragment = (DetailFragment) getFragmentManager()
.findFragmentById(R.id.detail_Fragment);
if (detailfragment != null && detailfragment.isInLayout()) {
detailfragment.setText(OS_Name);
}
else {
Intent intent = new Intent(getApplicationContext(),
DetailActivity.class);
Bundle extras = new Bundle();
extras.putString(DetailActivity.os_name, OS_Name);
intent.putExtras(extras);
startActivity(intent);
}
}
}
Code of DetailActivity.java
package com.example.fragmentapp;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
public class DetailActivity extends Activity {
public static String os_name = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
finish();
return;
}
setContentView(R.layout.detail_activity);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String name = extras.getString(os_name);
DetailFragment detailFragment = (DetailFragment) getFragmentManager()
.findFragmentById(R.id.detailFragment);
detailFragment.setText(name);
}
}
}
Code of DetailFragment.java
package com.example.fragmentapp;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DetailFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.detail_fragment, container, false);
return view;
}
// we call this method when button from listfragment is clicked
public void setText(String item) {
TextView view = (TextView) getView().findViewById(R.id.display_tv);
view.setText(item);
}
}
Code of ListFragment.java
package com.example.fragmentapp;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
public class ListFragment extends Fragment implements OnClickListener {
private Communicator communicator;
Button android_btn, ios_btn, windows_btn;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof Communicator) {
communicator = (Communicator) activity;
}
else {
throw new
ClassCastException(activity.toString()
+ " must implemenet MyListFragment.Communicator");
}
}
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View view =
inflater.inflate(R.layout.list_fragment, container, false);
// Initialize Views android_btn = (Button)
view.findViewById(R.id.android_btn_id);
ios_btn = (Button)
view.findViewById(R.id.ios_btn_id);
windows_btn = (Button)
view.findViewById(R.id.windows_btn_id);
// set on click Listeners for buttons
android_btn.setOnClickListener(this);
ios_btn.setOnClickListener(this);
windows_btn.setOnClickListener(this);
return view;
}
//Create Interface
public interface Communicator {
public void Message(String OS_Name);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.android_btn_id:
updateFragment("Android");
break;
case R.id.ios_btn_id:
updateFragment("IOS");
break;
case R.id.windows_btn_id:
updateFragment("Windows");
break;
}
}
private void updateFragment(String OS_Name) {
communicator.Message(OS_Name);
}
}
In Layout Folder
activity_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="horizontal" >
<fragment
android:id="#+id/list_Fragment"
android:layout_width="0sp"
android:layout_height="match_parent"
android:layout_weight="1"
class="com.pavan.fragmentdemo.MyListFragment" >
</fragment>
<fragment
android:id="#+id/detail_Fragment"
android:layout_width="0sp"
android:layout_height="match_parent"
android:layout_weight="2"
class="com.pavan.fragmentdemo.DetailFragment" >
</fragment>
</LinearLayout>
Code of detail_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFF99"
android:orientation="vertical"
android:padding="20dp" >
<TextView
android:id="#+id/display_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="40sp" />
</LinearLayout>
code of list_fragment.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#CCFF99"
android:orientation="vertical"
android:padding="5dp" >
<Button
android:id="#+id/android_btn_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android" />
<Button
android:id="#+id/ios_btn_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="IOS" />
<Button
android:id="#+id/windows_btn_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Windows" />
</LinearLayout>
In layout-port
code activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="#+id/list_Fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
class="com.pavan.fragmentdemo.MyListFragment" >
</fragment>
</LinearLayout>
Code of detail_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="#+id/detailFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.pavan.fragmentdemo.DetailFragment" />
</LinearLayout>
You have different names of fragments:
in xml com.pavan.fragmentdemo.MyListFragment
in java com.example.fragmentapp.MyListFragment
fix package names

Android Calculator Bug

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.

NULLPOINT EXCEPTION ERROR during Simple Android app development [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 8 years ago.
i am trying to develop this simple app using Android ADT and I got null point exception error. It is returning null point exception error at line 32 which is
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
counter++;
display.setText("your total is" + counter);
}
});
My whole java code is
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.*;
public class MainActivity extends ActionBarActivity {
int counter;
Button add , subtract;
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
add = (Button) findViewById(R.id.badd1);
subtract = (Button) findViewById(R.id.bsub1);
display = (TextView) findViewById(R.id.textView1);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
counter++;
display.setText("your total is" + counter);
}
});
subtract.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
counter--;
display.setText("your total is" + counter);
}
});
}
#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;
}
}
MY xml CODE IS
<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:gravity="center_horizontal"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.newboston.MainActivity$PlaceholderFragment" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="your total is 0"
android:textSize="45dp" />
<Button
android:id="#+id/badd1"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_marginTop="34dp"
android:layout_centerHorizontal="true"
android:text="add one"
android:textSize="20dp" />
<Button
android:id="#+id/bsub1"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/badd1"
android:layout_below="#+id/badd1"
android:layout_marginTop="34dp"
android:text="subtract one"
android:textSize="20dp" />
</RelativeLayout>
My logCat file is
03-25 23:10:29.219: D/AndroidRuntime(1211): Shutting down VM
03-25 23:10:29.229: W/dalvikvm(1211): threadid=1: thread exiting with uncaught exception (group=0xb2b10ba8)
03-25 23:10:29.279: E/AndroidRuntime(1211): FATAL EXCEPTION: main
03-25 23:10:29.279: E/AndroidRuntime(1211): Process: com.example.newboston, PID: 1211
03-25 23:10:29.279: E/AndroidRuntime(1211): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newboston/com.example.newboston.MainActivity}: java.lang.NullPointerException
03-25 23:10:29.279: E/AndroidRuntime(1211): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-25 23:10:29.279: E/AndroidRuntime(1211): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-25 23:10:29.279: E/AndroidRuntime(1211): at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-25 23:10:29.279: E/AndroidRuntime(1211): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-25 23:10:29.279: E/AndroidRuntime(1211): at android.os.Handler.dispatchMessage(Handler.java:102)
03-25 23:10:29.279: E/AndroidRuntime(1211): at android.os.Looper.loop(Looper.java:136)
03-25 23:10:29.279: E/AndroidRuntime(1211): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-25 23:10:29.279: E/AndroidRuntime(1211): at java.lang.reflect.Method.invokeNative(Native Method)
03-25 23:10:29.279: E/AndroidRuntime(1211): at java.lang.reflect.Method.invoke(Method.java:515)
03-25 23:10:29.279: E/AndroidRuntime(1211): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-25 23:10:29.279: E/AndroidRuntime(1211): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-25 23:10:29.279: E/AndroidRuntime(1211): at dalvik.system.NativeStart.main(Native Method)
03-25 23:10:29.279: E/AndroidRuntime(1211): Caused by: java.lang.NullPointerException
03-25 23:10:29.279: E/AndroidRuntime(1211): at com.example.newboston.MainActivity.onCreate(MainActivity.java:32)
03-25 23:10:29.279: E/AndroidRuntime(1211): at android.app.Activity.performCreate(Activity.java:5231)
03-25 23:10:29.279: E/AndroidRuntime(1211): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-25 23:10:29.279: E/AndroidRuntime(1211): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-25 23:10:29.279: E/AndroidRuntime(1211): ... 11 more
03-25 23:10:36.449: I/Process(1211): Sending signal. PID: 1211 SIG: 9
If you're using more than one layout file (e.g layout-land, layout-sw360dp), make sure all elements you reference exist on all of them.

Why does my app "unfortunately stop". All this I want is some simple buttons

This is my very first app. All I need is five buttons, two that call certain phone numbers (only have created one so far) and three buttons that take the user to a certain URL. I have no errors or warnings or any direction on how to navigate the LogCat.
LogCat:
10-08 14:41:40.716: D/AndroidRuntime(793): Shutting down VM
10-08 14:41:40.716: W/dalvikvm(793): threadid=1: thread exiting with uncaught exception (group=0x41465700)
10-08 14:41:40.777: E/AndroidRuntime(793): FATAL EXCEPTION: main
10-08 14:41:40.777: E/AndroidRuntime(793): java.lang.RuntimeException: Unable to start activity ComponentInfo{acps.mhs/acps.mhs.MainActivity}: java.lang.NullPointerException
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.os.Handler.dispatchMessage(Handler.java:99)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.os.Looper.loop(Looper.java:137)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-08 14:41:40.777: E/AndroidRuntime(793): at java.lang.reflect.Method.invokeNative(Native Method)
10-08 14:41:40.777: E/AndroidRuntime(793): at java.lang.reflect.Method.invoke(Method.java:525)
10-08 14:41:40.777: E/AndroidRuntime(793): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-08 14:41:40.777: E/AndroidRuntime(793): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-08 14:41:40.777: E/AndroidRuntime(793): at dalvik.system.NativeStart.main(Native Method)
10-08 14:41:40.777: E/AndroidRuntime(793): Caused by: java.lang.NullPointerException
10-08 14:41:40.777: E/AndroidRuntime(793): at acps.mhs.MainActivity.onCreate(MainActivity.java:25)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.Activity.performCreate(Activity.java:5133)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-08 14:41:40.777: E/AndroidRuntime(793): ... 11 more
10-08 14:42:19.696: I/Process(793): Sending signal. PID: 793 SIG: 9
Mainactivity.java
package acps.mhs;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity implements View.OnClickListener {
Button mhshome, pp, mhsdir, cmhs;
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
mhshome = (Button) findViewById(R.id.mhshome);
pp = (Button) findViewById(R.id.pp);
mhsdir = (Button) findViewById(R.id.mhsdir);
cmhs = (Button) findViewById(R.id.cmhs);
mhshome.setOnClickListener(this);
pp.setOnClickListener(this);
mhsdir.setOnClickListener(this);
cmhs.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.mhshome:
Uri uri = Uri.parse("http://www2.k12albemarle.org/school/mohs/Pages/default.aspx");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
break;
case R.id.pp:
Uri uri2 = Uri.parse("http://www2.k12albemarle.org/school/MOHS/Pages/Directory.aspx");
Intent intent2 = new Intent(Intent.ACTION_VIEW, uri2);
startActivity(intent2);
break;
case R.id.mhsdir:
Uri uri3 = Uri.parse("http://www2.k12albemarle.org/school/MOHS/Pages/Directory.aspx");
Intent intent3 = new Intent(Intent.ACTION_VIEW, uri3);
startActivity(intent3);
break;
case R.id.cmhs:
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:1234567890"));
startActivity(callIntent);
break;
}
}
#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;
}
}
activity_main.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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="#+id/pp"
style="#style/AppBaseTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/mhshome"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="#string/pp"/>
<Button
android:id="#+id/mhshome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:onClick="onClick"
android:text="#string/mhshome" />
<Button
android:id="#+id/mhsdir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/pp"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="#string/mhsdir" />
<Button
android:id="#+id/cmhs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/mhsdir"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="#string/callmhs" />
</RelativeLayout>
Its gonna be something incredibly simple I'm certain. Please point me in the right direction.
You need to call setContentView(R.layout.activity_main) :) That's why findViewById is returning null on your views.
You are not setting your layout.
Call setContentView(layout) in your onCreate() method.

Categories