实现自定义日历时片段出错

时间:2016-09-03 18:15:56

标签: android calendar fragment

我正在尝试在片段中实现日历。

Java文件

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;

public class FindPeopleFragment extends Fragment {

public FindPeopleFragment(){}

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

    HashSet<Date> events = new HashSet<>();
    events.add(new Date());
    setHasOptionsMenu(true);

    CalendarView cv = ((CalendarView)getView().findViewById(R.id.calendar_view));
    cv.updateCalendar(events);

    // assign event handler
    cv.setEventHandler(new CalendarView.EventHandler()
    {
        @Override
        public void onDayLongPress(Date date)
        {
            // show returned day
            DateFormat df = SimpleDateFormat.getDateInstance();
            Toast.makeText(FindPeopleFragment.this.getActivity(), df.format(date), Toast.LENGTH_SHORT).show();
        }
    });

}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
    // Inflate the menu; this adds items to the action bar if it is present.
    super.onCreateOptionsMenu(menu, inflater);
}

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

}

这是我得到的错误:

错误记录

/Users/stiffmaster/AndroidStudioProjects/Main/app/src/main/java/tiphinbox    /main/FindPeopleFragment.java
Error:(48, 5) error: missing return statement
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
:app:compileDebugJavaWithJavac FAILED
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

问题

如何解决错误?

1 个答案:

答案 0 :(得分:0)

您应该尝试了解错误试图说出的内容。它说返回声明丢失了。因此,您应该检查所有预期在其方法签名中返回值但未返回的函数。

快速检查表明您没有使用View方法返回onCreateView

祝你好运!