Error: Sorry the Aplication.... has unexpectedly stopped Please Try Again Java Eclipse

Asked

Viewed 581 times

-4

I’m having a problem with my project and I don’t know how to fix it. I’m trying to create a calculator using some internet tutorials but, is giving this error when I try to run in emulator: Sorry The Aplication calculator (proces com.example.app) has stopped unexpectedly. Please Try Again.

My Layout/ fragment_main.xml:

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<Button
    android:id="@+id/BTSomar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/Valor1"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="76dp"
    android:text="Somar" />

<EditText
    android:id="@+id/Resultado"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/BTSomar"
    android:layout_alignTop="@+id/BTSomar"
    android:layout_marginTop="51dp"
    android:ems="10" />

<EditText
    android:id="@+id/Valor2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/BTSomar"
    android:layout_alignLeft="@+id/BTSomar"
    android:layout_marginBottom="51dp"
    android:ems="10" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/Valor1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/Valor2"
    android:layout_alignLeft="@+id/textView1"
    android:layout_marginBottom="70dp"
    android:ems="10" />

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/Valor2"
    android:layout_alignLeft="@+id/Valor2"
    android:text="Digite o Segundo N°" />

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/Valor1"
    android:layout_alignLeft="@+id/Valor1"
    android:text="Digite o Primeiro N°" />

My Androidmanifest.xml bin/res

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="10" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.app.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

My Mainactivity.java

package com.example.app;

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.view.*;
import android.widget.*;
import android.widget.TextView;



public class MainActivity extends ActionBarActivity {



    EditText ET1, ET2, ETResultado;
    double num1, num2, resultado;
    Button BTsoma;  

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


        ET1 = (EditText) findViewById(R.id.Valor1);
        ET2 = (EditText) findViewById(R.id.Valor2);
        ETResultado = (EditText) findViewById(R.id.Resultado);
        BTsoma = (Button) findViewById(R.id.BTSomar);

        BTsoma.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                num1 = Double.parseDouble(ET1.getText().toString());
                num2 = Double.parseDouble(ET2.getText().toString());
                resultado = num1 + num2;
                ETResultado.setText(String.valueOf(resultado));
            }
        });





        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }


    @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;
        }
    }

}

Meu Logcat

04-13 01:45:14.200: E/FragmentManager(768): No view found for id 0x7f05003c (com.example.babr:id/container) for fragment PlaceholderFragment{40d2fbd0 #0 id=0x7f05003c}
04-13 01:45:14.200: E/FragmentManager(768): Activity state:
04-13 01:45:14.200: D/FragmentManager(768):   Local FragmentActivity 40d230c8 State:
04-13 01:45:14.220: D/FragmentManager(768):     mCreated=truemResumed=false mStopped=false mReallyStopped=false
04-13 01:45:14.220: D/FragmentManager(768):     mLoadersStarted=false
04-13 01:45:14.230: D/FragmentManager(768):   Active Fragments in 40d23330:
04-13 01:45:14.230: D/FragmentManager(768):     #0: PlaceholderFragment{40d2fbd0 #0 id=0x7f05003c}
04-13 01:45:14.230: D/FragmentManager(768):       mFragmentId=#7f05003c mContainerId=#7f05003c mTag=null
04-13 01:45:14.250: D/FragmentManager(768):       mState=0 mIndex=0 mWho=android:fragment:0 mBackStackNesting=0
04-13 01:45:14.250: D/FragmentManager(768):       mAdded=true mRemoving=false mResumed=false mFromLayout=false mInLayout=false
04-13 01:45:14.260: D/FragmentManager(768):       mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
04-13 01:45:14.260: D/FragmentManager(768):       mRetainInstance=false mRetaining=false mUserVisibleHint=true
04-13 01:45:14.280: D/FragmentManager(768):       mFragmentManager=FragmentManager{40d23330 in Segundo{40d230c8}}
04-13 01:45:14.280: D/FragmentManager(768):       mActivity=com.example.babr.Segundo@40d230c8
04-13 01:45:14.280: D/FragmentManager(768):   Added Fragments:
04-13 01:45:14.280: D/FragmentManager(768):     #0: PlaceholderFragment{40d2fbd0 #0 id=0x7f05003c}
04-13 01:45:14.290: D/FragmentManager(768):   FragmentManager misc state:
04-13 01:45:14.290: D/FragmentManager(768):     mActivity=com.example.babr.Segundo@40d230c8
04-13 01:45:14.300: D/FragmentManager(768):     mContainer=android.support.v4.app.FragmentActivity$2@40d233a8
04-13 01:45:14.310: D/FragmentManager(768):     mCurState=2 mStateSaved=false mDestroyed=false
04-13 01:45:14.310: D/FragmentManager(768):   View Hierarchy:
04-13 01:45:14.330: D/FragmentManager(768):     com.android.internal.policy.impl.PhoneWindow$DecorView{40d248e8 V.E..... ... 0,0-0,0}
04-13 01:45:14.350: D/FragmentManager(768):       android.widget.LinearLayout{40d24fb0 V.E..... ... 0,0-0,0}
04-13 01:45:14.350: D/FragmentManager(768):         com.android.internal.widget.ActionBarContainer{40d25b90 V.ED.... ... 0,0-0,0 #1020359 android:id/action_bar_container}
04-13 01:45:14.360: D/FragmentManager(768):           com.android.internal.widget.ActionBarView{40d26178 V.E..... ... 0,0-0,0 #102035a android:id/action_bar}
04-13 01:45:14.370: D/FragmentManager(768):             android.widget.LinearLayout{40d272b0 V.....C. ... 0,0-0,0}
04-13 01:45:14.370: D/FragmentManager(768):               com.android.internal.widget.ActionBarView$HomeView{40d289b0 V.E..... ... 0,0-0,0}
04-13 01:45:14.390: D/FragmentManager(768):                 android.widget.ImageView{40d28e10 G.ED.... ... 0,0-0,0 #1020246 android:id/up}
04-13 01:45:14.390: D/FragmentManager(768):                 android.widget.ImageView{40d29190 V.ED.... ... 0,0-0,0 #102002c android:id/home}
04-13 01:45:14.400: D/FragmentManager(768):               android.widget.LinearLayout{40d2aab0 G.E..... ... 0,0-0,0}
04-13 01:45:14.400: D/FragmentManager(768):                 android.widget.ImageView{40d2ae98 G.ED.... ... 0,0-0,0 #1020246 android:id/up}
04-13 01:45:14.410: D/FragmentManager(768):                 android.widget.LinearLayout{40d2b220 V.E..... ... 0,0-0,0}
04-13 01:45:14.420: D/FragmentManager(768):                   android.widget.TextView{40d2b610 V.ED.... ... 0,0-0,0 #102024d android:id/action_bar_title}
04-13 01:45:14.440: D/FragmentManager(768):                   android.widget.TextView{40d2c340 G.ED.... ... 0,0-0,0 #102024e android:id/action_bar_subtitle}
04-13 01:45:14.440: D/FragmentManager(768):           com.android.internal.widget.ActionBarContextView{40d2c998 G.E..... ... 0,0-0,0 #102035b android:id/action_context_bar}
04-13 01:45:14.460: D/FragmentManager(768):         android.widget.FrameLayout{40d2cf10 V.ED.... ... 0,0-0,0 #1020002 android:id/content}
04-13 01:45:14.460: D/FragmentManager(768):           android.widget.RelativeLayout{40d2dfb8 V.E..... ... 0,0-0,0}
04-13 01:45:14.480: D/FragmentManager(768):             android.widget.TextView{40d2e550 V.ED.... ... 0,0-0,0 #7f050040 app:id/textView1}
04-13 01:45:14.480: D/FragmentManager(768):             android.widget.TextView{40d2eb70 V.ED.... ... 0,0-0,0 #7f050041 app:id/textView2}
04-13 01:45:14.480: D/FragmentManager(768):             android.widget.TextView{40d2f198 V.ED.... ... 0,0-0,0 #7f050042 app:id/textView3}
04-13 01:45:14.490: D/FragmentManager(768):         com.android.internal.widget.ActionBarContainer{40d2d390 G.ED.... ... 0,0-0,0 #102035c android:id/split_action_bar}
04-13 01:45:14.500: D/AndroidRuntime(768): Shutting down VM
04-13 01:45:14.520: W/dalvikvm(768): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-13 01:45:14.540: D/dalvikvm(768): GC_CONCURRENT freed 205K, 12% free 2572K/2916K, paused 8ms+34ms, total 213ms
04-13 01:45:14.580: E/AndroidRuntime(768): FATAL EXCEPTION: main
04-13 01:45:14.580: E/AndroidRuntime(768): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.babr/com.example.babr.Segundo}: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.babr:id/container) for fragment PlaceholderFragment{40d2fbd0 #0 id=0x7f05003c}
04-13 01:45:14.580: E/AndroidRuntime(768):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-13 01:45:14.580: E/AndroidRuntime(768):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-13 01:45:14.580: E/AndroidRuntime(768):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-13 01:45:14.580: E/AndroidRuntime(768):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-13 01:45:14.580: E/AndroidRuntime(768):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-13 01:45:14.580: E/AndroidRuntime(768):  at android.os.Looper.loop(Looper.java:137)
04-13 01:45:14.580: E/AndroidRuntime(768):  at android.app.ActivityThread.main(ActivityThread.java:5041)
04-13 01:45:14.580: E/AndroidRuntime(768):  at java.lang.reflect.Method.invokeNative(Native Method)
04-13 01:45:14.580: E/AndroidRuntime(768):  at java.lang.reflect.Method.invoke(Method.java:511)
04-13 01:45:14.580: E/AndroidRuntime(768):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-13 01:45:14.580: E/AndroidRuntime(768):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-13 01:45:14.580: E/AndroidRuntime(768):  at dalvik.system.NativeStart.main(Native Method)
04-13 01:45:14.580: E/AndroidRuntime(768): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.babr:id/container) for fragment PlaceholderFragment{40d2fbd0 #0 id=0x7f05003c}
04-13 01:45:14.580: E/AndroidRuntime(768):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:930)
04-13 01:45:14.580: E/AndroidRuntime(768):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
04-13 01:45:14.580: E/AndroidRuntime(768):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-13 01:45:14.580: E/AndroidRuntime(768):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
04-13 01:45:14.580: E/AndroidRuntime(768):  at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
04-13 01:45:14.580: E/AndroidRuntime(768):  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1164)
04-13 01:45:14.580: E/AndroidRuntime(768):  at android.app.Activity.performStart(Activity.java:5114)
04-13 01:45:14.580: E/AndroidRuntime(768):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
04-13 01:45:14.580: E/AndroidRuntime(768):  ... 11 more
04-13 01:45:17.000: I/Process(768): Sending signal. PID: 768 SIG: 9

Someone can help me?

  • By your Logcat the error is due to a null pointer (Caused by: java.lang.NullPointerException) and occurred in the method onCreate (line 38 of MainActivity.java). It seems that the variable BTsoma is null (but looking at your layout file I could not identify why not find the button by ID).

  • Could the error also be in the emulator or in the eclipse java itself? because I have already touched the code several times and not solved :/

  • I find it unlikely that it is the emulator, and even less the Eclipse. You already consulted that thread in the English OS? Seems to be a similar problem.

  • Have some object that is null in your onCreate, there is occurring Nullpointerexception

3 answers

1

Apparently the error is in this line of Mainactivity.java:

BTsoma.setOnClickListener(new View.OnClickListener() {

The reason is because BTsoma is getting the value null on that other line:

BTsoma = (Button) findViewById(R.id.BTSomar);

And the reason for this is because the layout you pass to setContentView() DOES NOT CONTAIN A Button id @+id/BTSomar.

In fact, you show the code of your file fragment_main.xml but it’s not that file you use when calling setContentView(R.layout.activity_main). Instead, it uses another file called activity_main.xml, which is not shown in your question and which probably DOES NOT HAVE the Button in question.

Obs.: The fact that you have a layout of fragment suggests that you have a fragment within your activity. An alternative is to remove the elements Button, TextView etc. of that fragment_main.xml and put into activity_main.xml to make your life easier, because from there will no longer occur NullPointerException. If however you want to keep the fragment, should call BTsoma.setOnClickListener(new View.OnClickListener() { ... within a life cycle method of fragment and not of activity as it is currently doing. I don’t know exactly what method this is because I’m not used to messing with Fragments, maybe it’s onAttach() or else onActivityCreated() but even without knowing the exact method this recommendation already serves to illustrate the problem, remembering that the life cycle of Fragment is not fully linked to that of Activity and therefore there are times when one has no relationship with the other. You will need to understand the life cycle of Fragments to call this setOnClickListener at the right time.

0

Since you’re calling the layout (setContentView(R.layout.activity_main)) instead of calling the layout (setContentView(R.layout.fragment_main)), where it contains buttons and all objects and should extend to an Activity instead of extending to Actionbaractivity

Moult :

public class MainActivity extends ActionBarActivity
setContentView(R.layout.activity_main);

To :

public class MainActivity extends Activity
setContentView(R.layout.fragment_main);

-4

Before assigning the XML layout button, create an instance:

BTsoma = new Button(this);
BTsoma = (Button) findViewById(R.id.BTSomar);
  • 3

    The first line of your code is completely unnecessary, as in the second line the created object in the first line will be overwritten, regardless of whether the value you overwrite will be null or a valid reference to a button.

  • Actually I wrote a wrong detail, it’s not that the object shall be superscripted, the reference to the object will be overwritten, so the first object will be left aside.

  • I’ve been through problem like this for a while in Android development and needed to instantiate not to return null in reference to this. This only in the case of objects coming from an XML layout.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.