尝试调用虚方法' void android.widget.LinearLayout.setVisibility(int)'在null对象引用上

时间:2017-03-03 22:22:20

标签: android

我有一个导航抽屉活动,我试图用另一个片段替换背景中显示的mapFragment。该应用程序总是崩溃。

导航抽屉活动:

package georgia.languagelandscape;

//import android.app.FragmentManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;

public class NavigationDrawerActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener, ProfileFragment.OnFragmentInteractionListener, ReplaceFragment.OnFragmentInteractionListener{

    MapFragment mapFragment= new MapFragment();
    FragmentManager fm = getSupportFragmentManager();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation_drawer);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        //me
        navigationView.setItemIconTintList(null);
        getSupportActionBar().setDisplayShowTitleEnabled(false);


        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.content_replace, mapFragment);
        ft.commit();
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.navigation_drawer, 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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();


        FragmentTransaction ft = fm.beginTransaction();

        ReplaceFragment replaceFragment=new ReplaceFragment();
        ft.replace(R.id.content_replace, replaceFragment);

        if (id == R.id.nav_feed) {
            // Handle the camera action
        } else if (id == R.id.nav_my_profile) {
            LinearLayout map_layout=(LinearLayout)this.findViewById(R.id.dummyLayout);
            map_layout.setVisibility(1);
            fm.beginTransaction().hide(fm.findFragmentById(R.id.mapView)).commit();
           // fm.beginTransaction().hide(getSupportFragmentManager().findFragmentById(mapFragment)).commit();
            //fm.beginTransaction().hide(getSupportFragmentManager().findFragmentById(‌​mapFragment)).c‌​ommit();
            //or getSupportFragmentManager().findFragmentById(R.id.mapFragmen‌​t).getView().setVisi‌​bility(View.INVISIBL‌​E);
            ProfileFragment profileFragment= new ProfileFragment();
            ft.replace(R.id.content_replace, profileFragment);

        } else if (id == R.id.nav_my_projects) {

        } else if (id == R.id.nav_seetings) {

        } else if (id == R.id.nav_log_out) {

        }

        ft.commit();

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }

}

MapFragment:

package georgia.languagelandscape;

//import android.app.Fragment;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

//import android.support.v4.app.Fragment;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link MapFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link MapFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MapFragment extends SupportMapFragment implements OnMapReadyCallback {

    private GoogleMap mGoogleMap;

    @Override
    public void onResume() {
        super.onResume();
        if (mGoogleMap == null) {
            getMapAsync(this);
        }
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mGoogleMap = googleMap;
        // latitude and longitude
        double latitude = 17.385044;
        double longitude = 78.486671;

        // create marker
        MarkerOptions marker = new MarkerOptions().position(
                new LatLng(latitude, longitude)).title("Hello Maps");

        // Changing marker icon
        marker.icon(BitmapDescriptorFactory
                .defaultMarker(BitmapDescriptorFactory.HUE_ROSE));

        // adding marker
        googleMap.addMarker(marker);
        CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(new LatLng(17.385044, 78.486671)).zoom(12).build();
        googleMap.animateCamera(CameraUpdateFactory
                .newCameraPosition(cameraPosition));
    }
}

地图片段布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="georgia.languagelandscape.MapFragment">


    <LinearLayout
        android:id="@+id/dummyLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:visibility="gone"  >
        <fragment xmlns:android="http://schemas.android.com/apk/res/android"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:id="@+id/mapView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    </LinearLayout>
</FrameLayout>

我尝试在没有xml文件的linearLayout的情况下这样做,但它仍然无法正常工作。我不能使用destroy方法或其他任何东西,因为基本上我没有返回View。我该如何隐藏地图?

2 个答案:

答案 0 :(得分:1)

删除此行:

LinearLayout map_layout=(LinearLayout)this.findViewById(R.id.dummyLayout);
map_layout.setVisibility(1);
fm.beginTransaction().hide(fm.findFragmentById(R.id.mapView)).commit();

答案 1 :(得分:0)

再次检查XML文件,如果您使用的是v-16或 v-21 v-13 或某些其他配置文件,则会显示此NullPointer错误。 我遇到了这个问题,我将文件更改为不同的配置,并且可以正常工作。