SupportfragmentManager变为null android kotlin

时间:2017-10-24 15:02:18

标签: android android-asynctask kotlin android-dialogfragment

我正在尝试从onPostExecute启动dialogFragment,但supportFragmentManager由于某种原因返回null。我尝试从主活动和其他片段启动dialogFragment,它完美地工作但我无法从AsyncTask启动它。这是AsyncTask: -

class SubscribeTask: AsyncTask<Void, Void, Void> {
    var mContext: Context
    var mTextView_temp: TextView
    var mTextView_humidity: TextView
    var mWeather_image: ImageView
    var mMessage: JsonObject
    var mResult = JsonObject()
    var mTemp: String = ""
    var mHumidity: String = ""
    var mLdr: Boolean = true
    var mRain: Boolean = false
    var mWeather: Int = 0
    var tester: Boolean = false
    var mFire: Boolean = false

    constructor(context: Context, textView_temp: TextView, textView_humidity: TextView, weather_image: ImageView, message: PNMessageResult){
        mContext = context
        mMessage = message.message.asJsonObject
        mTextView_temp = textView_temp
        mTextView_humidity = textView_humidity
        mWeather_image = weather_image
    }

    override fun doInBackground(vararg p0: Void?): Void? {
        if (mMessage.has("pi")){
            mResult = mMessage.getAsJsonObject("pi")
            mTemp = mResult.get("temp").asString
            mHumidity = mResult.get("humidity").asString
            mLdr = mResult.get("ldr").asBoolean
            mRain = mResult.get("rain").asBoolean
            mFire = mResult.get("fire").asBoolean
            tester = true
            if(mRain){
              mWeather = R.drawable.rainy
            }else if(mLdr){
                mWeather = R.drawable.suny
            }else{
                mWeather = R.drawable.cloudyy
            }
        }
        return null
    }

    override fun onPostExecute(result: Void?) {
        if(tester) {
            mTextView_temp.text = mTemp
            mTextView_humidity.text = mHumidity
            mWeather_image.setImageResource(mWeather)
            if(mFire){
                MainActivity().showAlertDialog("Fire", "There might be a fire in the house", R.drawable.house_fire, "Stop Alarm")
            }
        }
    }

}

这是showAlertDialog()函数: -

fun showAlertDialog(title: String, content: String, imageid: Int, button: String){
        val fm = supportFragmentManager
        val alertDialogFragment = AlertFragment(title, content, imageid, button).newInstance(title, content, imageid, button)
        alertDialogFragment.show(fm, "fragment_alert")
    }

这是dialogFragment: -

class AlertFragment(val mTitle: String, val mContent: String, val mImageId: Int, val mButton: String) : DialogFragment() {

    fun newInstance(title: String, content: String, imageid: Int, button: String): AlertFragment {
        val alertfrag = AlertFragment(title, content, imageid, button)
        val args = Bundle()
        args.putString("title", title)
        alertfrag.arguments = args
        return alertfrag
    }

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        return inflater!!.inflate(R.layout.fragment_alert, container, false)
    }

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        title.text = mTitle
        alertContent.text = mContent
        alertImageId.setImageResource(mImageId)
        alertButton.text = mButton
    }
}

有人可以帮助我。 Java中的答案也没关系。

2 个答案:

答案 0 :(得分:1)

type="submit"功能中,您有以下代码:

postExecute

这个创建一个新的活动对象,这绝对不是应该做的。由于该对象是新创建的并且没有被android正确初始化,因此未初始化像supportFragmentManager这样的内部对象,导致您看到的MainActivity().showAlertDialog(...)

答案 1 :(得分:0)

您可以使用观察者模式。

创建一个类似

的界面
interface myExecListener {
 fun onFinish(title: String, content: String, imageid: Int, button: 
 String)
 }

将这些行添加到SubscribeTask

class SubscribeTask: AsyncTask<Void, Void, Void>() {
 var listener: myExecListener? = null

  fun exec(listener: myExecListener){
    this.listener = listener
    this.execute()
  }

  override fun doInBackground(vararg params: Void?): Void? {


    return null
  }

  override fun onPostExecute(result: Void?) {
    super.onPostExecute(result)
    listener?.onFinish("Fire", "There might be a fire in the house", 333, "Stop Alarm")
  }
 }

并在您的活动中执行

SubscribeTask().exec(object : myExecListener{
  override fun onFinish(title: String, content: String, imageid: Int, button: String) {
    println("Observer triggered")
    showAlertDialog(title, content, imageid, button)
  }
})