2016-02-16 1 views
0

Для начала, и с помощью исчерпывающего поиска я обнаружил, что большинство людей имеют эту ошибку из-за неправильного импорта класса фрагмента вместо использования библиотеки поддержкиTimePickerDialogFragment: «Не удается разрешить метод .Add (фрагмент, строка)

.

I с другой стороны, я следующий учебник, расположенный here.

Мой код в основном идентичен его, но я буду присоединять мою MainActivity и TimePickerDialogFragment ниже. в учебнике датирована августом 2012 я следующий, который, скорее всего, причина, почему я Я работаю над этим, я могу просто поменять учебники, но мне очень понравился опыт отладки исправления этого и причина того, почему он происходит.

Ошибка возникает с кодом //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

Проблема заключается в том с вами TimePickerDialogFragment.java

Заменить
import android.app.DialogFragment с
import android.support.v4.app.DialogFragment;

+0

Блин, так на самом деле я имел такую ​​же проблему, но не мог видеть его. Смотрел не в том месте. – Stembolt