活动

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


活动:

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


当我尝试打开此活动时,它将调用异常IllegalArgumentException,并且应用程序停止:未找到ID为0x7f07004e android的视图,这是怎么回事?
我试图搜索此异常,但无法解决

最佳答案

删除此:

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


PlaceholderFragment内部类。

您的activity_new_run布局不包含ID为container的视图,并且您可能也不希望PlaceholderFragment存在。当片段事务尝试从当前视图层次结构中按ID查找容器并失败时,将导致异常。

07-28 00:47