Error: Sorry the application ... has stopped unexpectedly please try again Java Eclipse

-4

I have a problem with my project and I do not know how to solve it. I am trying to create a calculator using some internet tutorials but, it is giving this error when I try to run in the emulator: Sorry The Aplication calculator (process 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 bin / res AndroidManifest.xml

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

}

My 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?

    
asked by anonymous 12.04.2014 / 17:23

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 is because the layout you pass to setContentView() DOES NOT CONTAIN a Button with id @+id/BTSomar .

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

Note: The fact that you have a fragment layout suggests that you have fragment within your activity . An alternative is to remove the elements Button , TextView etc. of fragment_main.xml and put in activity_main.xml to make your life easier, because it will not occur thereafter NullPointerException . If, however, you want to keep fragment , you should call BTsoma.setOnClickListener(new View.OnClickListener() { ... within a fragment lifecycle method and not activity as you are currently doing. I do not know exactly which method this is because I am not used to handling fragments, maybe it is onAttach() or onActivityCreated() but even without knowing the exact method this recommendation already serves to illustrate the problem, remembering that the fragment life cycle is not totally linked to that of activity and therefore there are times when one has no relation to the other. You will need to understand the fragments lifecycle to call this setOnClickListener at the right time.

    
13.04.2014 / 17:32
0

Once you are calling the (setContentView (R.layout.activity_main)) layout instead of calling the layout (setContentView ( R.layout.fragment_main)) , where it contains the buttons and all objects and should extend to an Activity instead of extending to ActionBarActivity

Changes from:

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

To:

public class MainActivity extends Activity
setContentView(R.layout.fragment_main);
    
12.09.2014 / 18:06
-4

Before assigning the XML layout button, create an instance:

BTsoma = new Button(this);
BTsoma = (Button) findViewById(R.id.BTSomar);
    
13.04.2014 / 16:53