findFragmentById()不是null

时间:2018-08-31 17:46:58

标签: android android-fragments android-activity

如果方向为纵向,则为单个片段;如果方向为横向,则为两个片段。

在第一次时,它可以很好地工作,无论是纵向还是横向都可以,但是我又回到纵向,但是失败了。

问题出在这里:当我具有对FragmentDetails的引用时(应为null),应为null,因为在纵向模式下已删除,但不为null。

<?php

use Faker\Generator as Faker;

use App\Models\User;
use App\Models\Company;

$factory->define(App\Models\Order::class, function (Faker $faker) {
    $company = Company::get()->random(1);
    $users = $company->users();
    $user = array_random($users);
    return [
        'company_id' => $company,
        'seller_id' => $user->id,
        'code' => strtoupper(str_random(10)),
        'description' => $faker->sentence($nbWords = rand(2, 4), $variableNbWords = true),
        'created_at' => $faker->dateTimeBetween($startDate = '-1 year', $endDate = 'now', $timezone = null)
    ];
});

这是FragmentDetails

php artisan db:seed

这是主要活动

export function stateSelected(state) {
  const features = this.stateGeoJSON.features;
  const currentFeature = features.find(s => s.properties.NAME === state);

  if (!currentFeature) {
    return;
  }

  // removes active layer
  this.removeLayersContaining('state-borders-active');
  this.drawActiveStateLayer(currentFeature);
}


export function drawActiveStateLayer(feature) {
  this.map
    .addLayer({
      id: 'state-borders-active',
      type: 'line',
      source: {
        type: 'geojson',
        data: feature
      },
      paint: {
        'line-color': '#8d8b76',
        'line-width': 6,
      },
    });
}

export function drawStateBorders() {
  $.getJSON('states.json').then((data) => {

    this.stateGeoJSON = data;

    this.map
      .addSource('states', {
        type: 'geojson',
        data,
      })
      .addLayer({
        id: 'state-borders',
        type: 'line',
        source: 'states',
        paint: {
          'line-color': '#bfe2ab',
          'line-width':  3,
        },
      });
  });

这是纵向模式下的主要活动布局

getSupportFragmentManager().findFragmentById(R.id.fragmentMainActivityDetailsMail);

这是横向模式下的主要活动布局

public class DetailsMailFragment extends android.support.v4.app.Fragment {

private TextView textViewDetailsStaticSubject;
private TextView textViewDetailsSubject;
private TextView textViewDetailsStaticFrom;
private TextView textViewDetailsFrom;
private TextView textViewDetailsMailBody;



public DetailsMailFragment() {
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.fragment_details_mail, container, false);

    textViewDetailsStaticSubject=(TextView)view.findViewById(R.id.textViewDetailsStaticSubject);
    textViewDetailsSubject=(TextView)view.findViewById(R.id.textViewDetailsSubject);
    textViewDetailsStaticFrom=(TextView)view.findViewById(R.id.textViewDetailsStaticFrom);
    textViewDetailsFrom=(TextView)view.findViewById(R.id.textViewDetailsFrom);
    textViewDetailsMailBody=(TextView)view.findViewById(R.id.textViewDetailsMailBody);
    return view;
}

public void bindMailData(String subject,String senderName,String message) {
    textViewDetailsStaticSubject.setText("Subject:");
    textViewDetailsSubject.setText(subject);
    textViewDetailsStaticFrom.setText("From:");
    textViewDetailsFrom.setText(senderName);
    textViewDetailsMailBody.setText(message);

}   
}

错误日志

public class MainActivity extends FragmentActivity implements MailFragment.MailClickListener {

private boolean isMultiPanel;


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

@Override
public void onMailClickListener(Mail email) {
    setMultiPanel();

    if(isMultiPanel){
        DetailsMailFragment details=(DetailsMailFragment) getSupportFragmentManager().findFragmentById(R.id.fragmentMainActivityDetailsMail);
        details.bindMailData(email.getSubject(),email.getSenderName(),email.getMessage());
    }else{
        Intent intent=new Intent(MainActivity.this,DetailsActivity.class);
        intent.putExtra("subject",email.getSubject());
        intent.putExtra("senderName",email.getSenderName());
        intent.putExtra("message",email.getMessage());
        startActivity(intent);
    }
}

public void setMultiPanel(){
    DetailsMailFragment details=(DetailsMailFragment) getSupportFragmentManager().findFragmentById(R.id.fragmentMainActivityDetailsMail);
    isMultiPanel=details!=null;
}
}

感谢您的支持。

0 个答案:

没有答案