M Dilawar
M Dilawar

Reputation: 11

Unable to startActivity from BottomSheetDialog

every thing works fine with this context ctw = ContextThemeWrapper(cont, R.style.AppTheme) but when i want to start any activity my app is crashing. actually this ctw works fine with Toast in this method but when i pass this ctw to Intent for starting activity is gives error. i also trying cont variable directly getApplicationContext also but still have same error. code is in kotlin you can help me in java also. :)

Here is complete code

fun showBottomSheetDialog(url: String, cont: Context) {

    ctw = ContextThemeWrapper(cont, R.style.AppTheme)


    val lin_play: LinearLayout
    val lin_cancel: LinearLayout

    dialog = BottomSheetDialog(ctw!!)
    dialog.setContentView(R.layout.dialog_bottom_dailymotion)
    dialog.getWindow()!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
    dialog.getWindow()!!.setBackgroundDrawableResource(R.color.Transparent)

    lin_play = dialog.findViewById(R.id.lin_play_d)!!
    lin_cancel = dialog.findViewById(R.id.lin_cancel_d)!!


    lin_play.setOnClickListener {
        if (TextUtils.isEmpty(url)) {
            Toast.makeText(ctw, "Invalid/Empty URL", Toast.LENGTH_SHORT).show()
        } else {
            if (!url!!.isEmpty()) {


                    startActivity(Intent(ctw, MainActivity::class.java).putExtra("url", url))

                dialog.dismiss()
            }
        }
    }

    lin_cancel.setOnClickListener {
        dialog.dismiss()

    }
              try {
              dialog.show()
              }catch (e:Exception){
              e.printStackTrace()
             }
        }

and here is my error

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.browserforvideodownload, PID: 29839
              java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
                  at android.app.Activity.startActivityForResult(Activity.java:4367)
                  at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
                  at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:68)
                  at android.app.Activity.startActivityForResult(Activity.java:4312)
                  at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:751)
                  at android.app.Activity.startActivity(Activity.java:4716)
                  at android.app.Activity.startActivity(Activity.java:4631)
                  at com.browserforvideodownload.browserlighting.browser.activity.BrowserActivity$showBottomSheetDialogDailymotion$1.onClick(BrowserActivity.kt:3970)
                  at android.view.View.performClick(View.java:5740)
                  at android.view.View$PerformClick.run(View.java:22947)
                  at android.os.Handler.handleCallback(Handler.java:836)
                  at android.os.Handler.dispatchMessage(Handler.java:103)
                  at android.os.Looper.loop(Looper.java:232)
                  at android.app.ActivityThread.main(ActivityThread.java:6661)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1106)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)

Edit:

After a long Struggle i have found my mistke.

just replace

 startActivity(Intent(ctw, MainActivity::class.java).putExtra("url", url))

into

try{
ctw!!.startActivity(Intent(getActivity(ctw), 
MainActivity::class.java).putExtra("url", url))
   }catch (e:Exception){
     e.printStackTrace()
   }

Upvotes: 0

Views: 843

Answers (3)

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

java.lang.NullPointerException: Attempt to invoke virtual method

You should use Context object. A Context as its first parameter (this is used because the Activity class is a subclass of Context).

lin_play.setOnClickListener 
    {
        if (TextUtils.isEmpty(url)) {
            Toast.makeText(cont, "Invalid/Empty URL", Toast.LENGTH_SHORT).show()
        } else {
            if (!url!!.isEmpty()) {
                startActivity(Intent(cont, MainActivity::class.java).putExtra("url", url))
                dialog.dismiss()
            }
        }

NOTE

  1. For Activity--> You can set your Context is--> this@Activity Name
  2. For Fragment --> use getActivity()

Example

startActivity(Intent(getActivity(), MainActivity::class.java).putExtra("url", url))

Upvotes: 2

Khemraj Sharma
Khemraj Sharma

Reputation: 58974

You are using wrong context, pass activity instance in Intent.

if (activity != null) {
    startActivity(Intent(activity, MainActivity::class.java).putExtra("url", url))
}

Check parent activity for null for best practice.

Upvotes: 1

V-rund Puro-hit
V-rund Puro-hit

Reputation: 5534

Simply use your Activity Context while starting Activity

startActivity(Intent(this@CurrentClassName, MainActivity::class.java).putExtra("url", url))

Note : It is good practice to dismiss dialog before starting new Activity

Upvotes: 1

Related Questions