gpt4 book ai didi

java - Fragment 未显示且 OnCreateView 方法未调用

转载 作者:行者123 更新时间:2023-12-01 07:52:42 24 4
gpt4 key购买 nike

最近我正在学习如何在我的应用程序中使用 fragment ,我遵循了一个包含简单演示的教程,但是,在我运行它之后, fragment 不显示,屏幕是全白的,我调试发现未调用 OnCreateView() 方法。
这是我的代码: Activity 犯罪:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:id="@+id/fragmentContainer"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".CrimeActivity">

</FrameLayout>

fragment_crime XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/crime_title_label"/>


<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/crime_title"
android:hint="@string/crime_title_hint"
style="?android:listSeparatorTextViewStyle"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/crime_details_label"
style="?android:listSeparatorTextViewStyle"/>
<Button
android:id="@+id/crime_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/crime_solved"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:text="@string/crime_solved_label"
/>

</LinearLayout>

CrimeActivity.java

package com.example.ryden.criminalintent;

import android.support.v4.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class CrimeActivity extends FragmentActivity {

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

FragmentManager fm = getSupportFragmentManager();

CrimeFragment fragment = new CrimeFragment();
fm.beginTransaction().add(R.id.fragmentContainer,fragment).commit();
fm.executePendingTransactions();
Log.v("test","add");


}






@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
// if (id == R.id.action_settings) {
// return true;
// }

return super.onOptionsItemSelected(item);
}
}

CrimeFragment.java

package com.example.ryden.criminalintent;


import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.support.v4.app.FragmentActivity;
import android.widget.EditText;
import android.support.v4.app.Fragment;


import java.text.SimpleDateFormat;

/**
* Created by ryden on 12/23/2015.
*/
public class CrimeFragment extends Fragment{
private Crime mCrime;
private EditText mTitleField;
private Button mDateButton;
private CheckBox mSolvedCheckBox;

@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
mCrime = new Crime();
Log.v("1","create");
}

public View OnCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
Log.v("222","oncreateview");
View v = inflater.inflate(R.layout.fragment_crime, parent, false);
Log.v("inflater?","inflater?");
mTitleField = (EditText)v.findViewById(R.id.crime_title);
mTitleField.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
mCrime.setmTitle(charSequence.toString());
}

@Override
public void afterTextChanged(Editable editable) {

}
});
SimpleDateFormat dateFormat = new SimpleDateFormat("yy/mm/dd HH:mm");
mDateButton = (Button)v.findViewById(R.id.crime_date);
mDateButton.setText(dateFormat.format(mCrime.getmDate().toString()));
mDateButton.setEnabled(false);
mSolvedCheckBox = (CheckBox)v.findViewById(R.id.crime_solved);
mSolvedCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mCrime.setmSolved(isChecked);
}
});
return v;
}

}

最佳答案

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View v = inflater.inflate(R.layout.fragment_tab_1, null);

return v;
}

关于java - Fragment 未显示且 OnCreateView 方法未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34673721/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com