OnViewCreated从未使用导致android中的问题

时间:2016-03-24 15:35:32

标签: android google-maps main-activity

由于第四个标签是嵌套片段,我的应用程序崩溃之前遇到了问题所以我将方法移动到主要活动,当选择该标签时调用它但是现在它给出了我从未使用过onviewcreated方法的错误。

这是我的主要活动类:

package com.example.hp_user.shoutfinal28;


import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.maps.GoogleMap;
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 MainActivity extends FragmentActivity implements OnMapReadyCallback {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from activity_main.xml
    setContentView(R.layout.activity_main);

    // Locate the viewpager in activity_main.xml
    ViewPager viewPager = (ViewPager) findViewById(R.id.pager);

    // Set the ViewPagerAdapter into ViewPager
    viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));

}

public void btnShout(View v) {
    //allows for label to be changed to shouted once button is pressed
    EditText txtInput = (EditText) findViewById(R.id.txtInput);
    TextView lblShout = (TextView) findViewById(R.id.lblShout);
    lblShout.setText("Shouted! ");

    //allows for toast to be displayed once button is clicked
    Toast toast = new Toast(getApplicationContext());
    toast.setGravity(Gravity.TOP | Gravity.LEFT, 0, 0);
    toast.makeText(MainActivity.this, txtInput.getText() + " Has Been Shouted.", toast.LENGTH_SHORT).show();

}

@Override
public void onViewCreated(View v_maps, Bundle savedInstanceState) {
    super.onViewCreated(v_maps, savedInstanceState);
    SupportMapFragment supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.Maps1);
    if (supportMapFragment!= null) {
        supportMapFragment.getMapAsync(this);
    }
}

@Override
public void onMapReady (GoogleMap map) {
    map.addMarker(new MarkerOptions()
            .position(new LatLng(0, 0))
            .title("Marker"));
}
}

这是我的fragmentshouts_maps类:

public class FragmentShouts_Maps extends Fragment  {


public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Get the view from fragment shouts.xml
    View root_view = inflater.inflate(R.layout.fragmentshouts_maps, container, false);

    return root_view;
}



}

1 个答案:

答案 0 :(得分:0)

您的片段中必须@override onViewCreated()不在Activity我认为您的错误来自此。