Stefano Pisano
Stefano Pisano

Reputation: 438

IllegalArgumentException: no view found for id 0x7f07004e android

Activity

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linear"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@drawable/wallstart"
        android:orientation="vertical" >

        <fragment
            android:id="@+id/newMap"
            android:name="com.google.android.gms.maps.MapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_margin="8dp" />
</RelativeLayout>

Activity:

package com.example.mapstest;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;

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

public class NewRun extends ActionBarActivity {

    static GoogleMap map;
    static LocationManager lManager;
    static boolean alreadyStarted=false;
    static Listener listener;
    static String provider;
    static String _mapName;
    static Context context;
    static LatLng previous = null;
    static Button start, stop, newRun;

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

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
        context = getApplicationContext();
        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.newMap)).getMap();
        lManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

        Intent in= getIntent();
        Bundle b = in.getExtras();
        if(b!=null) {
            String name = (String) b.get("name");
            _mapName = name;
            this.setTitle("Map: " + _mapName);
            //String desc = (String) b.get("desc");
            //mapDesc.setText(desc);
        } 

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.new_run, menu);
        return true;
    }

    static void stopService() {
        Toast.makeText(context, "Stopping services", Toast.LENGTH_SHORT).show();
        lManager.removeUpdates(listener);
        lManager.removeGpsStatusListener(listener);
        map.setMyLocationEnabled(false);
    }

    public void onDestroy(){
        super.onDestroy();
        Toast.makeText(getApplicationContext(), "On Destroy" , Toast.LENGTH_SHORT).show();
        if(listener!=null)
            stopService();
        alreadyStarted=false;   
        finish();
    }

 public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()){
        case R.id.action_start:
            if(lManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
                if(!alreadyStarted){
                    Criteria criteria = new Criteria();
                    provider = lManager.getBestProvider(criteria, false);
                    map.setMyLocationEnabled(true);
                    listener = new Listener(getApplicationContext());
                    lManager.addGpsStatusListener(listener);
                    alreadyStarted = true;
                    DBAdapter dbAdapter = new DBAdapter(context);
                    dbAdapter.open();
                    dbAdapter.setCreate(_mapName, true);
                    dbAdapter.close();
                }
            }
            else {
                startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                Toast.makeText(getApplicationContext(), "Enable GPS and press again start", Toast.LENGTH_SHORT).show();
                alreadyStarted = false;
            }
            return true;
        case R.id.action_stop:
            stopService();  
            onDestroy();
            return true;
        }
        return false;

    } 

    /**
     * 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_new_run,
                    container, false);
            return rootView;
        }
    }

When I try to open this activity it call an exception IllegalArgumentException and the application stops: no view found for id 0x7f07004e android, what is wrong with it? I tried to search for this exception but I can't solve it

Upvotes: 0

Views: 417

Answers (2)

sjain
sjain

Reputation: 23344

You may have set the wrong layout in setContentView() of the onCreate() method.

Check - android-fragment-no-view-found-for-id.

Upvotes: 0

laalto
laalto

Reputation: 152817

Remove this:

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

and the PlaceholderFragment inner class.

Your activity_new_run layout does not contain a view with id container and you probably don't want the PlaceholderFragment there either. The exception is caused when the fragment transaction attempts to find the container by id from the current view hierarchy and fails.

Upvotes: 2

Related Questions