"No view found" error closes my application

Asked

Viewed 196 times

-1

I’m creating a project with two screens, one with a button and the other with a list of phone numbers. Only when I click the button, the error message appears "Unfortunately app has stopped".

My Mainactivity

package com.example.babr;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
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.widget.Button;
import android.os.Build;

public class MainActivity extends ActionBarActivity {


    Button botao;

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


       botao = (Button) this.findViewById (R.id.Botao01);
       botao.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


            Intent i = new Intent(MainActivity.this, Segundo.class);
            startActivity(i);
            finish();


        }
    });

        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 Second Activity

package com.example.babr;

import com.example.babr.MainActivity.PlaceholderFragment;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
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.widget.Button;
import android.os.Build;


public class Segundo extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.segundo);





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

}

Logcat with the bug

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

Androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.babr"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.babr.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>
         <activity
            android:name="com.example.babr.Segundo"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN1" />

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

    </application>

</manifest>

1 answer

0

From time to time when you try to create an Android project containing a activity in the Eclipse ADT, this now includes a fragment along with the activity. For beginners, however, I find it very complicated to mess with the life cycle of fragment so right away. Therefore recommend removing all this code referring to fragment, that is, remove the whole class PlaceholderFragment from within MainActivity, also remove the file res/layout/fragment_main.xml and also remove any reference to fragment from inside the archive activity_main.xml. This will simplify your project and probably eliminate the error that is happening.

Browser other questions tagged

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