I have two activities, QuizQuestions, and MainActivity2. When I set up QuizQuestions in the Manifest intent filter the app opens and launches fine. However when I set up my app to open with MainActivity2 it fails to initialize at all. I have looked at the files side by side and I cannot find what the difference in them is to see why one would launch and the other wont.
I am brand new to Kotlin so I am sure it is something very obvious that I am missing. Any help is greatly appreciated.
Thank you.
Here is my manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.crazywkids.officequiz">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.OfficeQuiz">
<activity
android:name=".QuizQuestions"
android:exported="true"
/>
<activity
android:name=".MainActivity2"
android:exported="true"
android:theme="#style/NoActionBarTheme"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Here is MainActivity2
package com.crazywkids.officequiz
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
class MainActivity2 : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
val beginBtn = findViewById<Button>(R.id.beginBtn)
beginBtn.setOnClickListener {
val intent = Intent(this, QuizQuestions::class.java)
startActivity(intent)
}
}
}
Here is QuizQuestions
package com.crazywkids.officequiz
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
class QuizQuestions : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_quiz_questions)
val question=Constants.getQuestions()
Log.i("Question Size", "${question.size}")
}
}
Related
This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
Android marshmallow request permission?
(26 answers)
Closed 12 months ago.
I'm trying to figure out permission in android. I have written a code that allows you to access the gallery with images. I didn't add any permission in the manifest. But from the app, I can access the gallery, although I shouldn't have (tried on an emulator and on a physical device).
Then I tried to do the same for a phone call. Here without <uses-permission android:name="android.permission.CALL_PHONE" /> an exception is thrown.
Can't figure out why I can access the gallery
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.activity.result.ActivityResultCallback
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import ru.artrostudio.testgalery.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var uri: Uri
private lateinit var mBinding:ActivityMainBinding
var resultLauncher =registerForActivityResult(
ActivityResultContracts.GetContent(),
ActivityResultCallback {
println("${it.toString()}")
uri=it
mBinding.imageView.setImageURI(uri)
}
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mBinding = ActivityMainBinding.inflate(layoutInflater)
setContentView( mBinding.root)
mBinding.button.setOnClickListener {
resultLauncher.launch("image/*")
}
mBinding.button2.setOnClickListener { call() }
}
private fun call()
{
var tel="tel:0000000"
var intent=Intent(Intent.ACTION_CALL)
intent.data=Uri.parse(tel)
startActivity(intent)
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.TestGalery">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I'm just setting out with teaching myself about Android audio development, and I'm rather baffled as to why neither the onConnected nor the onConnectionFailed method of connectionCallback is called here. PodcastService is instantiated and its onGetRoot method is called, but the callback is not called. No error messages are given; the callback is simply not called. Any help would be great!
MainActivity.kt:
package com.davidwillett.audioapp
import android.content.ComponentName
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.media.MediaBrowserCompat
class MainActivity : AppCompatActivity() {
private val connectionCallback = object : MediaBrowserCompat.ConnectionCallback() {
override fun onConnected() {
println("ConnectionCallback.onConnected")
super.onConnected()
}
override fun onConnectionFailed() {
println("ConnectionCallback.onConnectionFailed")
super.onConnectionFailed()
}
}
private lateinit var mediaBrowser: MediaBrowserCompat
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mediaBrowser = MediaBrowserCompat(
this,
ComponentName(this, PodcastService::class.java),
connectionCallback,
null)
}
override fun onStart() {
println("MainActivity.onStart")
super.onStart()
mediaBrowser.connect()
}
override fun onStop() {
println("MainActivity.onStop")
super.onStop()
mediaBrowser.disconnect()
}
}
PodcastService.kt:
package com.davidwillett.audioapp
import android.os.Bundle
import android.support.v4.media.MediaBrowserCompat
import androidx.media.MediaBrowserServiceCompat
class PodcastService : MediaBrowserServiceCompat() {
override fun onGetRoot(
clientPackageName: String,
clientUid: Int,
rootHints: Bundle?
): BrowserRoot? {
println("PodcastService.onGetRoot")
return BrowserRoot("root", null)
}
override fun onLoadChildren(
parentId: String,
result: Result<MutableList<MediaBrowserCompat.MediaItem>>
) {
TODO("Not yet implemented")
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.davidwillett.audioapp">
<application
android:allowBackup="false"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".PodcastService"
android:exported="false">
<intent-filter>
<action android:name="android.media.browse.MediaBrowserService" />
</intent-filter>
</service>
</application>
</manifest>
Sorry, I didn't do my research well enough!
A comment in the UAMP demo project states that: "In order for MediaBrowserCompat.ConnectionCallback.onConnected to be called, a MediaSessionCompat.Token needs to be set on the MediaBrowserServiceCompat."
So that'll be what's missing!
i want to move "detail" activity when button was clicked.
but, when button was clicked, app(?) crashed.
I looked over for the internet but couldn't find the answer.
What should I do?
(MainActivity.kt)code
package com.example.practice
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.practice.dataclass.*
import kotlinx.android.synthetic.main.activity_main.*
import android.content.Intent
import com.example.practice.detail
import kotlinx.android.synthetic.main.activity_detail.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button3.setOnClickListener {
val i = Intent(this, detail::class.java)!!
startActivity(i)
}
}
}
logcat
2020-04-12 19:09:39.388 13201-13201/com.example.practice E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.practice, PID: 13201
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
at com.example.practice.MainActivity$onCreate$1.onClick(MainActivity.kt:18)
at android.view.View.performClick(View.java:7350)
at android.view.View.performClickInternal(View.java:7327)
at android.view.View.access$3600(View.java:807)
at android.view.View$PerformClick.run(View.java:28166)
at android.os.Handler.handleCallback(Handler.java:907)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7464)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:955)
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.practice">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".detail"></activity>
</application>
ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ
Your button click listener should be like this:
button3.setOnClickListener {
val i = Intent(this#MainActivity, detail::class.java)
startActivity(i)
}
where DetailActivity is your next activity on which you want to after clicking
button3
I apologize for my English.
I have a problem with redirecting to a subpage of Google results, SMS to WebView Android applications.
Example: The page with the address https://siteadress.pl/category in the Google results opens the WebView application and shows the homepage (https://siteadress.pl/)
LOOK PICTURE
Example: A page with the exact product https://siteadress.pl/shop/productxyz in Google results also opens the WebView application and shows the homepage. Why?
The link (https://siteadress.pl/shop/productxyz) from the sms message also opens the WebView application and shows the main page.
I want to point to the exact page of the application in WebView, not the homepage. :(
My AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pl.APP">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="APP"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<meta-data
android:name="asset_statements"
android:resource="#string/asset_statements" />
<activity
android:name=".SplashScreen"
android:launchMode="singleTop"
android:noHistory="true"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.shortcuts"
android:resource="#xml/shortcuts" />
</activity>
<activity android:name=".ContactActivity" />
<activity android:name=".CategoryActivity" />
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:screenOrientation="portrait">
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="https"
android:host="siteadress.pl" />
</intent-filter>
</activity>
</application>
</manifest>
My MainActivity.xml
package pl.APP
import android.content.ActivityNotFoundException
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.support.annotation.RequiresApi
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.webkit.URLUtil
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
webView.webViewClient = MyWebViewClient()
webView.loadUrl("https://siteadress.pl/")
webView.settings.javaScriptEnabled = true
}
override fun onBackPressed() {
if (webView.canGoBack()) {
webView.goBack()
} else {
super.onBackPressed()
}
}
inner class MyWebViewClient : WebViewClient()
{
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean
{
if(URLUtil.isNetworkUrl(url))
{
return false
}
try
{
val shareIntent= Intent()
shareIntent.action=Intent.ACTION_VIEW
shareIntent.data= Uri.parse(url)
startActivity(shareIntent)
}
catch(e: ActivityNotFoundException)
{
Toast.makeText(this#MainActivity, "Appropriate app not found", Toast.LENGTH_LONG).show()
Log.e("AndroidRide",e.toString())
}
return true
}
#RequiresApi(Build.VERSION_CODES.N)
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean
{
val url=request?.url.toString()
if(URLUtil.isNetworkUrl(url))
{
return false
}
try
{
val shareIntent= Intent()
shareIntent.action=Intent.ACTION_VIEW
shareIntent.data= Uri.parse(url)
startActivity(shareIntent)
}
catch(e: ActivityNotFoundException)
{
Toast.makeText(this#MainActivity, "Appropriate app not found", Toast.LENGTH_LONG).show()
Log.e("AndroidRide",e.toString())
}
return true
}
}
}
Thanks for help :)
The solution is you need to handle app link first, get the correct link and then load that link into your WeView:
Uri data = getIntent().getData();
if (data != null) {
String url = data.toString();
webView.loadUrl(url);
} else {
webView.loadUrl("https://siteadress.pl/");
}
Done! This version in "Kotlin" work:
val url = intent.data.toString()
if (URLUtil.isValidUrl(url)) {
webView.loadUrl(url)
} else {
webView.loadUrl("https://siteadress.pl/")
}
Thanks for help :) ufff
There are quite a few examples of using BOOT_COMPLETED to start an application when the device boots..
I have attempted to use these example against my Flutter application. Having it start the App. This is for a simply signage app that shows images. Basically similar to a picture frame.
In the example code below, the application is compiling, however, when I reboot a simulator, for example, the code does not appear to have any effect.
My guess is that I am not calling the right code to actually start the application.. I am not a Android developer, so am having issues figuring what is exactly going on.
Manifest follows..
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="au.net.digitall.cmplayer">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:name="io.flutter.app.FlutterApplication"
android:label="cm_player"
android:icon="#mipmap/ic_launcher"
tools:ignore="GoogleAppIndexingWarning">
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="#style/cmTheme2"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<meta-data
android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
android:value="true" />
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver
android:enabled="true"
android:name=".StartCmPlayerServiceAtBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
Then the StartCmPlayerServiceAtBootReceiver class to start the APP..
package au.net.digitall.cmplayer;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class StartCmPlayerServiceAtBootReceiver extends BroadcastReceiver {
private static final String TAG = StartCmPlayerServiceAtBootReceiver.class.getSimpleName();
#Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "BOOT detected");
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent serviceIntent = new Intent(context, MainActivity.class);
context.startService(serviceIntent);
}
}
}
This all compiles and runs, but nothing happens on reboot.
Appreciate the help..
Thank to very much to Mike M.
His suggestion and pointing at the other android based discussion gave me enough info to archive autostart on boot. The code change to the above example follows..
In the StartCmPlayerServiceAtBootReceiver class,
Change to
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent mIntent = new Intent(context, MainActivity.class);
mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mIntent);
}
}
Thanks again, and I hope other flutter devs find this useful.
Create receiver class on kotlin path project_name/android/app/src/main/kotlin/com/example/app/
package com.example.app
import android.content.BroadcastReceiver
import android.content.Context;
import android.content.Intent;
class BootReceiver: BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == Intent.ACTION_BOOT_COMPLETED) {
val i = Intent(context, MainActivity::class.java)
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(i)
}
}
}
Add permission on your manifest
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
Add receiver tag inside application tag on your manifest
<receiver
android:enabled="true"
android:exported="true"
android:name="com.example.app.BootReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Add code to open your app after booting was completed on your MainActivity.kt, you can find it on project_name/android/app/src/main/kotlin/com/example/app/
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
var REQUEST_OVERLAY_PERMISSIONS = 100
if (!Settings.canDrawOverlays(getApplicationContext())) {
val myIntent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION)
val uri: Uri = Uri.fromParts("package", getPackageName(), null)
myIntent.setData(uri)
startActivityForResult(myIntent, REQUEST_OVERLAY_PERMISSIONS)
return
}
}
don't forget to import this code below on your MainActivity.kt because you need Bundle and Settings package
import android.os.Bundle
import android.provider.Settings
Works on my flutter app, tested on android 11.