TimePickerDialogFragment:"无法解析方法.add(片段,字符串)

时间:2016-02-16 19:36:39

标签: java android android-fragments timepickerdialog

首先,通过详尽的搜索,我发现由于Fragment类的导入不正确而不是使用支持库,大多数人都会出现此错误。

另一方面,我正在遵循位于here的教程。

我的代码与他的代码完全相同,但我将在下面附加我的MainActivity和TimePickerDialogFragment。我跟随的教程是2012年8月,可能是我遇到这个问题的原因,我可以交换教程,但我真的很喜欢纠正这个的调试经验以及原因。为什么会发生。

代码//add the fragment object to the fragment transaction ft.add(timePicker, "time_picker");

发生错误

这是我的MainActivity:

package com.stembo.android.timepickerdialogdemo;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends FragmentActivity {

    int mHour = 15;
    int mMinute = 15;

    //This handles the message send from TimePickerDialogFragment (TPDF)
    Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message m){
            //create a bundle object to pass the current set time
            Bundle b = m.getData();
            //getting the hour of day from bundle
            mHour = b.getInt("set_hour");
            //getting the minute from bundle
            mMinute = b.getInt("set_minute");
            //displaying a short time message containing the time set by the TPDF
            Toast.makeText(getBaseContext(), b.getString("set_time"),Toast.LENGTH_LONG).show();
        }
    };

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

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        //click event handler for button
        OnClickListener listener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                //create a bundle object to pass current time set
                Bundle b = new Bundle();
                //add currently set hour to bundle object
                b.putInt("set_hour", mHour);
                //add currently set minute to bundle object
                b.putInt("set_minute", mMinute);
                //instantiate TPDF
                TimePickerDialogFragment timePicker = new TimePickerDialogFragment(mHandler);
                //setting the bundle object on timepicker fragment
                timePicker.setArguments(b);
                //get a fragment manager for this activity
                FragmentManager fm = getSupportFragmentManager();
                //start a fragment transaction
                FragmentTransaction ft = fm.beginTransaction();
                //add the fragment object to the fragment transaction
                ft.add(timePicker, "time_picker");
                //opening the timepicker fragment
                ft.commit();
            }
        };
        //get an instance of Set button
        Button btnSet = (Button)findViewById(R.id.btnSet);
        btnSet.setOnClickListener(listener);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

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

我认为不需要它,但这里是TimePickerDialogFragment

package com.stembo.android.timepickerdialogdemo;

import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TimePicker;

/**
 * Created by myxom on 16/02/16.
 */
public class TimePickerDialogFragment extends DialogFragment {
    Handler mHandler ;
    int mHour;
    int mMinute;

    //public TimePickerDialogFragment(){}; //solves a lint issue

    public TimePickerDialogFragment(Handler h){ //another lint issue
        /** Getting the reference to the message handler instantiated in MainActivity class */
        mHandler = h;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){

        /** Creating a bundle object to pass currently set time to the fragment */
        Bundle b = getArguments();

        /** Getting the hour of day from bundle */
        mHour = b.getInt("set_hour");

        /** Getting the minute of hour from bundle */
        mMinute = b.getInt("set_minute");

        TimePickerDialog.OnTimeSetListener listener = new TimePickerDialog.OnTimeSetListener() {

            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

                mHour = hourOfDay;
                mMinute = minute;

                /** Creating a bundle object to pass currently set time to the fragment */
                Bundle b = new Bundle();

                /** Adding currently set hour to bundle object */
                b.putInt("set_hour", mHour);

                /** Adding currently set minute to bundle object */
                b.putInt("set_minute", mMinute);

                /** Adding Current time in a string to bundle object */
                b.putString("set_time", "Set Time : " + Integer.toString(mHour) + " : " + Integer.toString(mMinute));

                /** Creating an instance of Message */
                Message m = new Message();

                /** Setting bundle object on the message object m */
                m.setData(b);

                /** Message m is sending using the message handler instantiated in MainActivity class */
                mHandler.sendMessage(m);
            }
        };

        /** Opening the TimePickerDialog window */
        return new TimePickerDialog(getActivity(), listener, mHour, mMinute, false);
    }
}

1 个答案:

答案 0 :(得分:1)

问题出在你身上 TimePickerDialogFragment.java

import android.app.DialogFragment

替换
import android.support.v4.app.DialogFragment;