【问题标题】:OnViewCreated never used causing issues in androidOnViewCreated 从未使用过导致 android 出现问题
【发布时间】:2016-03-24 15:35:31
【问题描述】:

在我的应用程序由于第四个选项卡是嵌套片段而崩溃之前遇到问题,因此我将方法移到了要在选择该选项卡时调用的主要活动,但现在它给了我一个错误从不使用 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"));
}
}

这是我的 fragmenthouts_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;
}



}

【问题讨论】:

  • 尝试在 Fragment 而不是 Activity 中覆盖 onViewCreated
  • 看Fragment的文档,看FragmentActivity的文档。哪个有 onViewCreated() 方法,哪个没有?
  • 如果您使用谷歌地图作为您的选项卡之一,请参阅此答案的最后一部分,了解如何在没有嵌套片段的情况下执行此操作,这样可以使其工作而不会崩溃:stackoverflow.com/a/32579020/4409409跨度>
  • @DanielNugent,我在我的课程中实现了你的选项卡类,我的主线程仍然过度工作,甚至删除了我拥有的一个选项卡,所以我的地图片段只有 2 个其他片段,它仍然崩溃
  • 崩溃的堆栈跟踪显示什么? @AlexanderRufus

标签: android google-maps main-activity


【解决方案1】:

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

【讨论】:

  • 我以前是这样设置的,我的应用程序会崩溃,这就是为什么我这样尝试我什至今天早些时候发布了一个问题,说明了这个问题
  • 我从我的 viewpageradapter 中删除了 Fragmentshouts_maps 选项卡,我的应用程序已经停止崩溃,所以我知道它实际上只是那个类。 @Kristiyan
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-18
  • 2015-04-03
  • 2015-09-07
  • 2012-11-01
  • 2011-05-22
  • 1970-01-01
相关资源
最近更新 更多