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

OnViewCreated从未在android中使用导致问题,android,google-maps,main-activity,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.vi

我以前遇到过一个问题,由于第四个选项卡是嵌套的片段,我的应用程序会崩溃,所以我将方法移动到主活动,在选择该选项卡时调用,但现在它给了我一个错误,即从未使用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"));
}
}
这是我的碎片呼喊地图课:

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



}

您必须在片段中而不是在
活动中重写onViewCreated()
,我认为您的错误是由此产生的。

尝试重写在片段中而不是在活动中创建的onViewCreated()查看有关片段的文档,并查看有关FragmentActivity的文档。哪一个有onViewCreated()方法,哪一个没有?如果您使用Google地图作为选项卡之一,请参阅此答案的最后一节,了解如何在没有嵌套片段的情况下执行此操作,从而使其在不崩溃的情况下工作:@DanielNugent,我在我的类中实现了您的选项卡类,我的主线程仍然工作过度,甚至删除了我的一个选项卡,所以我的地图片段中只有2个其他片段,它仍然崩溃堆栈跟踪显示崩溃的原因是什么@AlexanderRufusi之前就这样设置了,我的应用程序会崩溃,这就是为什么我尝试这样做。今天早些时候,我甚至发布了一个问题,指出问题。我从我的viewpageradapter中删除了fragmentshouts_maps选项卡,我的应用程序已经停止崩溃,所以我知道实际上只有那个类@克里斯蒂安