无法使用Mainactivity和Navigation抽屉启动活动ComponentInfo?

时间:2015-05-20 01:23:23

标签: java android android-fragments

我的MainActivity启动和片段导航抽屉与谷歌地图一起使用时出现问题。我得到的错误实际上是两个错误,看起来像这样:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.examples.blahblah.blahblah/com.examples.blahblah.blahblah.MainActivity2Activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.MapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference

现在我相信这与我的片段文件有关,如下所示:

    package com.examples.blahblah.blahblah;

import android.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.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;


    public class menu_1_fragment extends Fragment implements OnMapReadyCallback {

        View rootview;

        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            rootview = inflater.inflate(R.layout.menu1_layout, container, false);

            MapFragment mapFragment = (MapFragment) getFragmentManager()
                    .findFragmentById(R.id.map);
            mapFragment.getMapAsync(this);

            return rootview;
        }

        @Override
        public void onMapReady(GoogleMap map) {
            LatLng sydney = new LatLng(-33.867, 151.206);

            map.setMyLocationEnabled(true);
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));

            map.addMarker(new MarkerOptions()
                    .title("Sydney")
                    .snippet("The most populous city in Australia.")
                    .position(sydney));
        }
    }

但是我会发布我的MainActivity,如果有人认为这可能是我得到这个错误的原因。但是在我开始更改Fragment文件之前,我没有收到此错误。

编辑:更新的代码遇到getSupportFragment的问题

    import android.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.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;


    public class menu_1_fragment extends Fragment implements OnMapReadyCallback {

        View rootview;

        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            rootview = inflater.inflate(R.layout.menu1_layout, container, false);


            return rootview;
        }

        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            SupportMapFragment mapFragment = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map));
            mapFragment.getMapAsync(this);
        }

        @Override
        public void onMapReady(GoogleMap map) {
            LatLng sydney = new LatLng(-33.867, 151.206);

            map.setMyLocationEnabled(true);
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));

            map.addMarker(new MarkerOptions()
                    .title("Sydney")
                    .snippet("The most populous city in Australia.")
                    .position(sydney));
        }
    }

1 个答案:

答案 0 :(得分:0)

尝试更改

public class MainActivity extends ActionBarActivity {
private String tudo = " ";

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

     textView = (TextView)findViewById(R.id.textView);

}

public void onClick(View view){
    new Thread(new Runnable() {
     @Override
    public void run() {
        insert();
        // textView.setText(select());
    }
    }).start();

}
protected void select(){
    try{
        Class.forName("oracle.jdbc.driver.OracleDriver");
        String url = "XXXXXXX";
        Connection c = DriverManager.getConnection(url, "XXXX", "XXXX");

        Statement stmt = c.createStatement();
        String teste;
        ResultSet rset = stmt.executeQuery("SELECT * FROM TTESTE");
        while (rset.next())
        {
            teste = (" " + rset.getString("NOME") + rset.getInt("ID"));
            tudo = tudo + teste;
        }
        rset.close();
        stmt.close();
        c.close();
        textView.setText(tudo);
    }
    catch (ClassNotFoundException | SQLException e){
        e.printStackTrace();
    }
   }
 }

到这个

MapFragment mapFragment = (MapFragment) getFragmentManager()
                .findFragmentById(R.id.map);

别忘了做必要的进口。

编辑:尝试在onActivityCreated()中移动此代码并从onCreateView()中删除它

SupportMapFragment mapFragment = ((SupportMapFragment) getChildFragmentManager()
    .findFragmentById(R.id.map));