Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法在android上添加片段_Java_Android_Android Layout_Android Fragments_Fragmenttransaction - Fatal编程技术网

Java 无法在android上添加片段

Java 无法在android上添加片段,java,android,android-layout,android-fragments,fragmenttransaction,Java,Android,Android Layout,Android Fragments,Fragmenttransaction,这是主要的活动布局 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height=

这是主要的活动布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="gw.es.lasarenas.MainActivity">

    <fragment
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:name="gw.es.lasarenas.CalendarFragment"
        android:id="@+id/fragment"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

活动呢?

package gw.es.lasarenas;


import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import java.net.URL;

import javax.mail.MessagingException;

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentManager fm = getFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        CalendarFragment calendar = new CalendarFragment();
        fragmentTransaction.add(R.id.layout_calendar,calendar);//here is the proble
        fragmentTransaction.commit();
        //new Email().execute();
    }

    private class Email extends AsyncTask<URL, Integer, Long> {

        @Override
        protected Long doInBackground(URL... params) {
            SendMail mail = new SendMail();
            try {
                mail.sendEmail();
            } catch (MessagingException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

}
包gw.es.lasarenas;
导入android.app.FragmentManager;
导入android.app.FragmentTransaction;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.support.v4.app.FragmentActivity;
导入java.net.URL;
导入javax.mail.MessaginException;
公共类MainActivity扩展了FragmentActivity{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm=getFragmentManager();
FragmentTransaction FragmentTransaction=fm.beginTransaction();
CalendarFragment calendar=新的CalendarFragment();
fragmentTransaction.add(R.id.layout_calendar,calendar);//问题是
fragmentTransaction.commit();
//新建电子邮件().execute();
}
私有类电子邮件任务{
@凌驾
受保护的长doInBackground(URL…参数){
SendMail=newsendmail();
试一试{
mail.sendmail();
}捕获(消息异常e){
e、 printStackTrace();
}
返回null;
}
}
}
片段布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:id="@+id/layout_calendar"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context="gw.es.lasarenas.CalendarFragment">

    <!-- TODO: Update blank fragment layout -->

    <CalendarView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/calendarView"
        android:layout_gravity="center_horizontal" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/next"
        android:id="@+id/button"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

还有片段类

package gw.es.lasarenas;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link CalendarFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 */
public class CalendarFragment extends Fragment {

    private OnFragmentInteractionListener mListener;

    public CalendarFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_calendar, container, false);

        return view;
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}
包gw.es.lasarenas;
导入android.content.Context;
导入android.net.Uri;
导入android.os.Bundle;
导入android.support.v4.app.Fragment;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
/**
*一个简单的{@link Fragment}子类。
*包含此片段的活动必须实现
*{@link CalendarFragment.OnFragmentInteractionListener}接口
*处理交互事件。
*/
公共类CalendarFragment扩展了该片段{
私有OnFragmentInteractionListener mListener;
公共日历片段(){
//必需的空公共构造函数
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//为该碎片膨胀布局
视图=充气机。充气(R.layout.fragment\u日历,容器,false);
返回视图;
}
//TODO:重命名方法、更新参数并将方法挂接到UI事件中
public void onButtonPressed(Uri){
if(mListener!=null){
onFragmentInteraction(uri);
}
}
@凌驾
公共void-onAttach(上下文){
super.onAttach(上下文);
if(OnFragmentInteractionListener的上下文实例){
mListener=(OnFragmentInteractionListener)上下文;
}否则{
抛出新的RuntimeException(context.toString()
+“必须实现OnFragmentInteractionListener”);
}
}
@凌驾
公共无效连接(){
super.onDetach();
mListener=null;
}
/**
*此接口必须由包含以下内容的活动实现
*片段,以允许通信此片段中的交互
*该活动以及其中可能包含的其他片段
*活动。
*
*有关更多信息,请参阅Android培训课程。
*/
FragmentInteractionListener上的公共接口{
//TODO:更新参数类型和名称
void onFragmentInteraction(Uri);
}
}

当我尝试添加fagment
fragmentTransaction.add(R.id.layout\u calendar,calendar)
时,函数与任何函数都不匹配,但是add函数接收一个int和一个片段(CalendarFragment扩展了Fragmnet),看起来它是正确的,但它不正确。

问题是您正在使用:

import android.app.FragmentManager;
import android.app.FragmentTransaction;
您的Fragment类扩展了
support.v4
库中的
Fragment

import android.support.v4.app.Fragment;
这会导致错误,因为您不能将这两种情况混合使用。将所有内容更改为一个或另一个,例如,在此行中使用
getSupportFragmentManager
而不是
getFragmentManager

FragmentManager fm = getFragmentManager();

这样,您将从支持库获得一个
FragmentManager
。另外,别忘了将导入内容替换为来自support的导入内容。

我应该使用什么,android.app.Fragment还是android.support.v4.app.Fragment?你可能会在网上找到很多关于哪个更好、为什么更好的讨论(长话短说——它们非常相似)。可以肯定的是,您不能将来自
android.app
support.v4
的元素混用在一起。一旦你决定了,就坚持下去。我个人更喜欢我的项目中的支持库。我认为最好尝试自己搜索一点错误,然后问一个包含完整日志的新问题,因为我无法通过评论像这样修复它,抱歉:/竖起大拇指!这个小指针解决了一个我关注太久的问题。谢谢