[Q] Seeking help for writing a TabLayout using Fragment - Android Q&A, Help & Troubleshooting

I am new to Android development. Recently I have been trying to rewrite the following tutorial with Fragment class. The tutorial utilizs the old TabActivity class and TabHost and TabWidget annontations.
TabLayout Google Tutorial
I followed some online Fragment sample codes, I converted all my Activity class with Fragment class, but I couldn't make it to work.
Here are few questions
1. Should I define the <Fragment> class or calling ActionBar.addBar(...), or both?
2. What would a complete res/layout/main.xml look like? What would be the root element (i.e. LinearLayout, Framelayout..etc)?
3. Any additional info would be grateful.

Related

[Q] Call method from view to activity

hi, how can i call a method that is in a view.java going to my activity.java? is there a command for that?
please help =(
In general, if the classes can "see" each other, and the method you want to call is accessible by the caller class, then just call the method via an instance of the according class. So let's say there were class A in file A.java with the method a() and you want to call it from class B in B.java you need to make sure that a() is not a private method (declared public, protected or not explicitly declared) and also it were good if the classes shared the same package.
Then just create an instance of A accessible by a method of B. (only necessary if a() isn't static!) For example like:
Code:
class B {
B() {
A objectOfTypeA = new A();
objectOfTypeA.a();
// voila
}
}
So as you can see there's no special command necessary to call a method of another class. Also I recommend you to read some tutorials about controlling access to members of a class.

[Q] NullPointerException when trying to access MainActivity from different class

Hi!
I am trying to develop an android app with a google map v2, location service and some control buttons.
But I don't want to put all these things inside one MainActivity class. So I thought I could split all the code into some more classes. The MainActivity shall controll all the GUI things and react on map or location events...
Now I have the following problem. Inside my onCreate I instanziate the additional classes:
Code:
// Preferences as singleton
pref = Prefs.getInstance(this.getApplicationContext());
pref.loadSettings();
// Set up the location
loc = new Locations(pref);
loc.setCallback(this);
map = new MyMap(pref);
It seems to work fine. But inside the MyMap class every time I start the app a null pointer exception is thrown. When I am calling MyMap() the following code will be executed:
Code:
[...]
private Prefs pref;
private GoogleMap mMap;
[...]
public MyMap(Prefs prefs) {
pref = (Prefs) prefs;
if (mMap == null) {
FragmentManager fmanager = getSupportFragmentManager();
mMap = ((SupportMapFragment) fmanager.findFragmentById(R.id.map)).getMap();
[...]
}
The line with the findFragmentById is the one that causes the exception.
If I write
Code:
SupportMapFragment f = ((SupportMapFragment) fmanager.findFragmentById(R.id.map));
f is allways null. But how can I access the fragments and view elements defined within my MainActivity?
It works if I put the code inside my MainAcitivity.
Every class extends "android.support.v4.app.FragmentActivity"
I tried to save the application context within my Prefs() class, so that I can access it from everywhere.
But I don't know how to use it inside my additional classes.
How to share the "R" across all my classes?
Can someone help me please?
Thank you very much!!
Thorsten
Are you having trouble adding a Map to a Fragment? If so, then you may take a look at this tutorial. I haven't tried it myself since I couldn't install Google Play Services on my development device. If it helps, do write back, as I am definitely going to try it myself soon.

[AppsUP] Frequently Asked Questions About Huawei Map Kit

More information about the AppsUP contest, you can visit HUAWEI Developer Forum.​
The AppsUP contest has already been online. Maybe many developers encounter the following questions in the process. Today I want to share the frequently asked questions of Map Kit with you. Hope they can help you.
Q: Can I transfer a mark in Google Map to HUAWEI Map?
A: The mark in Google Map cannot be transferred to HUAWEI Map as Google Map does not support data export.
Q: Huawei map not working after upload the app to Huawei gallery. If i installed the same apk directly to phone everything working perfectly.
A: The fingerprint signatures of the two apps are different. That means you can use them separately.
Q: I am using HMS Map kit in my application when i open the particular activity. i am getting some messages repeatedly and UI is getting hanged and ANR is happening.
A: This issue can occur when the UI thread is blocked.
There could be below reasons for this issue:
1> Not using the MapAsync for handlling the callback.
2> While using the MapInitializer, context is null.
3> This provided URL is not correct.
Please check below for the reference
https://developer.huawei.com/consumer/en/doc/development/HMS-References/hms-map-mapsinitializer
https://developer.huawei.com/consumer/en/doc/development/HMS-References/hms-map-return-codes
Q: Map is not rendering and displays white screen returns http status code 502
A: If HTTP status code 502 is returned, a network error has occurred. Check your network environment.
https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides-V5/open-platform-error-0000001050121484-V5
Q: I find gesturing on Map Kit a bit difficult? Is there a solution to make the gesturing similar to what is available on the Google Maps?
A: Some check points for you:
1. Please download sample code and use SupportMapFragment by referring to sample code. Link: https://developer.huawei.com/consumer/en/doc/development/HMS-Examples/hms-map-v4-example
2. Also please update the HMS Core version to the lastest and try.
Q: I followed tutorial about Map Kit , i check everything are same with the tutorial but , my map still blank
A: 1. Check wether you declared app id in manifest.xml file; 2. Enable map service in AGC.
Q: Is it possible to tilt the map more than 45 degrees?
A: Different tilt angles are required for different zoom levels.
when zoom < 10: it not allowed to tilt.
when zoom >= 10: 0 <= tilt <= 45.
So when you set tile to 70f, it can be max to 45f.
Q: I am developing an app with flutter and I decided to link it to a map kit, I followed the documentation and I still get this black screen that shows blank cells and I can't fix this problem
A: You probably haven't set up your signature certificate fingerprint
Please have a look at this guide:
https://developer.huawei.com/consumer/en/doc/development/HMS-Plugin-Guides/config-agc-0000001050296920
Q: map kit 'integrated with flutter' doesn't work with all mobile phones
A: For the build apk, you should config your build.gradle like this (https://forums.developer.huawei.com/forumPortal/en/topicview?tid=0201289420207900111&fid=0101187876626530001)
Q: I couldn't find the complete list of mapFeatures in MapStyle JSON. Can I know where can I obtain the MapStyle JSON with all the mapFeatures?
A: You can refer to the official website:
https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/android-sdk-map-style-customization-0000001050728424
There is also a Style Editor that can be used to generate the JSON files in a quick manner located at the bottom of the documentation.
Q: How to convert from com.huawei.hms.maps.model.PointOfInterest to com.huawei.hms.site.api.model.Site ?
A: You can use the Site Kit Place Details to obtain the site for a given POI
Code:
<p style="line-height: 1.5em;">override fun onPoiClick(poi: PointOfInterest) {
//Obtain a Search service Instance
val searchService = SearchServiceFactory.create(requireContext(),
URLEncoder.encode(API_KEY, "UTF-8") )
//Create the request for the given POI
val request = DetailSearchRequest()
request.setSiteId(poi.placeId)
// Create a search result listener.
val resultListener =
object : SearchResultListener<DetailSearchResponse?> {
// Return search results upon a successful search.
override fun onSearchResult(result: DetailSearchResponse?) {
var site: Site?=null
if (result == null || result.site.also { site = it } == null) {
return
}
//here you can get the site information
Log.i("SITE","${site?.formatAddress}")
}
// Return the result code and description upon a search exception.
override fun onSearchError(status: SearchStatus) {
Log.e(
"TAG",
"Error : " + status.errorCode + " " + status.errorMessage
)
}
}
searchService.detailSearch(request, resultListener)
}</p>
You can check the guide here:
https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides-V5/android-sdk-place-details-0000001050156636-V5
More information like above, follow me. You can get the updated collection about HMS Kits.
Any doubts or questions about this, you can leave your comment below.

Getting Place Details with HMS Site Kit

If we briefly talk about what HMS Site Kit is, you can provide users to explore the world faster with Site Kit. You can search for locations by keywords, find places which are close to the specified coordinate point, get detailed information about a place and get suggestions for places by keyword.
We can get detailed information about a place with Place Detail Search, another feature of Site Kit. The only condition for this, we need to know Site model’s id value that belongs to the place we want to search.
Before I explain the use of Place Detail Search, I would like to share with you a function that we can use this feature.
Code:
fun placeDetail(siteId: String){
val searchService = SearchServiceFactory.create(context,
URLEncoder.encode(
"Your-API-KEY",
"utf-8"))
var request = DetailSearchRequest()
request.siteId = siteId
request.language = Locale.getDefault().language // Getting system language
searchService.detailSearch(request, object: SearchResultListener<DetailSearchResponse>{
override fun onSearchError(searchStatus: SearchStatus?) {
Log.e("SITE_KIT","${searchStatus?.errorCode} - ${searchStatus?.errorMessage}")
}
override fun onSearchResult(detailSearchResponse: DetailSearchResponse?) {
var site = detailSearchResponse?.site
site?.let {
Log.i("SITE_KIT", "Name => ${it.name}," +
"Format address => ${it.formatAddress}, " +
"Coordinate => ${it.location.lat} - ${it.location.lng}, " +
"Phone => ${it.poi.phone}, " +
"Photo URLS => ${it.poi.photoUrls}, " +
"Rating => ${it.poi.rating}, " +
"Address Detail => ${it.address.thoroughfare}, ${it.address.subLocality}, " +
"${it.address.locality}, ${it.address.adminArea}, ${it.address.country}")
} ?: kotlin.run {
Log.e("SITE_KIT","Site Place couldn't find with the given site ID")
}
}
})
}
First, we need to create a SearchService object from the SearchServiceFactory class. For this, we can use the create() method of the SearchServiceFactory class. We need to declare two parameters in create() method.
The first of these parameters is context value. It is recommended that Context value should be in Activity type. Otherwise, when HMS Core(APK) needs to be updated, we can not receive any notification about it.
The second parameter is API Key value that we can access via AppGallery Connect. This value is generated automatically by AppGallery Connect when a new app is created. We need to encode API parameter as encodeURI.
After creating our SearchService object as I described above, we can create a DetailSearchRequest object. We will specify the necessary parameters on this object related to the place which we want want to get information.
After creating our DetailSearchRequest object, we can determine parameters for a place that we want to get information. Two parameters are specified here:
SiteId: There is a unique id value for each Site in Site Kit. This parameter is used to specify the id value of the place whose information is to be obtained.
Language: It is used to specify the language that search results have to be returned. If this parameter is not specified, language of the query field we have specified in the query field is accepted by default. In example code snippet in above, language of device has been added automatically in order to get a healthy result.
After entering the id value and language parameter of the place that we want to learn in detail, we can start learning the details. For this, we will use detailSearch() method of the SearchService object. This method takes two parameters.
For the first parameter, we must specify DetailSearchRequest object we have defined above.
For the second parameter, we have to implement SearchResultListener interface. Since this interface has a generic structure, we need to specify class belonging to the values to be returned. We can get the incoming values by specifying DetailSearchResponse object. Two methods should be override with this interface. onSearchError() method is executed if operation fails, and onSearchResult() method is executed if operations is successful. There is one value in DetailSearchResponse. This value is Site object that belongs to the id value. With the Site variable of DetailSearchResponse object, we can access information belong to place we have searched.
sujith.e said:
Hi,Why API key is required?
Click to expand...
Click to collapse
API key is a simple credential for accessing Huawei services. Your API key is creating automatically on the AppGallery Connect when you create an application, and then your app can use the key to call public APIs provided by Huawei.
When an app calls a public API provided by Huawei, we should give this information to API to help Huawei to identify our application.

Integrating HUAWEI Game Service Based on Cocos SDKHub - Initialization

There are two key steps required for integrating HUAWEI Game Service. The first is adding code to the onCreate method of the Application class to register the callback listening function for the activity. And the second is calling the JosAppsClient.init method to initialize the game.
Cocos SDKHub helps you complete the first step and provides the initialization method you need for the second step.
Here’s the sample code for calling the method:
JavaScript:
sdkhub.getUserPlugin().callFuncWithParam(“init”);
Please note that your game should be initialized prior to its launch. Furthermore, if the method contains a user privacy agreement, it will need to be called upon the user’s consent. This method is used to display bulletins.
After initialization, the following information is recorded in the log:
Wiki:
I/HMSSDK_HMSBIInitializer: Builder->biInitFlag :true
I/HMSSDK_JosAppsClientImpl: request Jos Notice.
I/HMSSDK_HmsClient: post msg api_name:core.getNoticeIntent, app_id:103468471|, pkg_name:com.cocos2d.checkUpdate, sdk_version:50000302, session_id:*, transaction_id:103468471Intent20201214203645835197672, kitSdkVersion:50001302, apiLevel:1
I/HMSSDK_BaseAdapter: in baseRequest + uri is :core.getNoticeIntent, transactionId is : 103468471Intent20201214203645835197672
I/HMSSDK_PendingResultImpl: init uri:core.getNoticeIntent
I/HMSSDK_PendingResultImpl: setResultCallback
I/HMSSDK_PendingResultImpl: setResult:0
I/HMSSDK_BaseAdapter: baseCallBack.onComplete
I/HMSSDK_HmsClient: receive msg status_code:0, error_code8002, api_name:core.getNoticeIntent, app_id:103468471|, pkg_name:com.cocos2d.checkUpdate, session_id:*, transaction_id:103468471Intent20201214203645835197672, resolution:null
W/HMSSDK_NoticeTaskApiCall: Jos Notice onResult failed:8002,ErrReason:
Note: No results will display after init is called, and no action is required from your app.

Categories

Resources