2

I am working on an android Temperature converter app(which does not work). At first I saw this message on LogCat

the application may be doing too much work on its main thread

I then removed the code that was never used.

Now, this is the error:

java.lang.RuntimeException: Unable to start activity

Code: Main_Activity.java

package com.example.tempconverter;

import android.support.v7.app.ActionBarActivity;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;


public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    final EditText editCelsius = (EditText) findViewById(R.id.editCelsius);
    final EditText editFahrenheit = (EditText) findViewById(R.id.editFahrenheit);
    Button buttonConvert =(Button)findViewById(R.id.buttonConvert);

    buttonConvert.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            double celsius = Double.valueOf(editCelsius.getText().toString());
            double fahrenheit = (celsius * 9)/5 +32;
            editFahrenheit.setText(String.valueOf(fahrenheit));


        }
    });
}
}

The code in fragment_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="com.example.tempconverter.MainActivity$PlaceholderFragment" >

<TextView
    android:id="@+id/textCelsius"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="55dp"
    android:text="Celsius"
    android:textAppearance="?android:attr/textAppearanceMedium"
    tools:ignore="hardcodedtext" />

<EditText
    android:id="@+id/editCelsius"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/textCelsius"
    android:layout_marginLeft="52dp"
    android:layout_toRightOf="@+id/textCelsius"
    android:ems="7"
    android:inputType="number" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/textFahrenheit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editCelsius"
    android:layout_marginTop="31dp"
    android:text="Fahrenheit"
    android:textAppearance="?android:attr/textAppearanceMedium"
    tools:ignore="hardcodedtext" />

<Button
    android:id="@+id/buttonConvert"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editFahrenheit"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="70dp"
    android:text="Convert"
    tools:ignore="hardcodedtext" />

<EditText
    android:id="@+id/editFahrenheit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/editCelsius"
    android:layout_alignTop="@+id/textFahrenheit"
    android:ems="7"
    android:inputType="number" />

</RelativeLayout>

this is a part of the LogCat now ... https://drive.google.com/file/d/0Bxd5Rg3QFBfbdkZULWxNUTVrV2M/edit?usp=sharing

5
  • 1
    the logcat don't contains something of useful here, you should post the logcat when crash (but the text, not an image) Mar 30, 2014 at 13:57
  • Do you mean that I should select "verbose" instead of "errors"?
    – Rsaleh
    Mar 30, 2014 at 13:59
  • Check if your ids on the layout correspond with your findViewById methods.
    – Blo
    Mar 30, 2014 at 14:00
  • Nope, i mean: Launch your app, when it crash go to eclipse and paste where the output Mar 30, 2014 at 14:00
  • Did you try to debug it?
    – vlio20
    Mar 30, 2014 at 14:00

1 Answer 1

0

Apparently, you are inflating the wrong view. You've got a RuntimeException, and you should have a NullPointerException, because the setContentView try to set (and methods findViewById try to find views inside) the wrong layout as:

setContentView(R.layout.activity_main);  

whereas all your EditTexts, TextViews and Buttons are in fragment_main.xml.
Try to set the content view as follow:

setContentView(R.layout.fragment_main);  

or, rename fragment_main.xml to activity_main.xml.


Update from comments:

These kinds of errors "Couldn't load memtrack module' occur with some devices with emulator. To avoid this, always test with multiple devices (real or not).

5
  • I've tried this one: setContentView(R.layout.fragment_main); I got the same error message "couldn't load memtrack module"
    – Rsaleh
    Mar 30, 2014 at 19:06
  • @Rsaleh you did this on emulator? Did you try on real device?
    – Blo
    Mar 30, 2014 at 21:02
  • THANK YOU SO MUCH, I changed the "Run Configurations" and it works fine now :')
    – Rsaleh
    Mar 30, 2014 at 23:20
  • @Rsaleh Glad to help! Sometimes the emulator with some devices have these kinds of errors: "couldn't load memtrack module". Apparently, it's a current error with emulators. To avoid, test with multiple devices (real or not).
    – Blo
    Mar 30, 2014 at 23:31
  • @Rsaleh Thank you to accept my answer. You did the most part to resolve your issue, I just gave the clue. +1 to this, bravo! Happy coding.
    – Blo
    Mar 30, 2014 at 23:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.