未显示片段且未调用OnCreateView方法

时间:2016-01-08 09:38:40

标签: java android android-layout android-fragments android-activity

最近我正在学习如何在我的应用程序中使用片段,并且我遵循包含一个简单演示的教程,但是,在我运行它之后,片段没有显示,屏幕是全白的,我调试它发现它未调用OnCreateView()方法 这是我的代码: 的 activity_crime:

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

}

3 个答案:

答案 0 :(得分:4)

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

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

    return v;
}

答案 1 :(得分:1)

问题在于签名。它应该以小写字母开头。在片段

中替换它
$(".log_bak").load('inc/log_bak.php');
$("#log_bak_btn").one('click', function() {
  setInterval(function() {
    $(".log_bak").load('inc/log_bak.php');
  }, 10000);
});

答案 2 :(得分:1)

要调试它,请设置要覆盖的onCreateView方法。这样可以确保正确调用onCreateView方法并确保方法的大小正确。因此代码应该如下:

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

请注意,这应该只在片段中完成。完成更改后,应显示该片段。