I am very new to android development and java. I am trying to learn some android code from Build your first app.
Now, the problem is, as soon as I add FusedLocation
(the commanted section in the send code), it crashes. The current state of the code is (mostly the first app, and few line from getting the last location) is:
MainActivity.java
package com.example.myfirstapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user taps the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
and
DisplayMessageActivity.java
package com.example.myfirstapp;
import android.content.Intent;
import android.location.Location;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnSuccessListener;
public class DisplayMessageActivity extends AppCompatActivity {
private FusedLocationProviderClient mFusedLocationClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
//Location
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
TextView textView = findViewById(R.id.textView);
textView.setText(message);
}
}
The building of the app goes without error, but when app in avd
crashes when I press send button. The app launching log is:
02/08 13:00:44: Launching app
$ adb install-multiple -r -t -p com.example.myfirstapp /home/rudra/AndroidStudioProjects/MyFirstApp/app/build/intermediates/split-apk/debug/slices/slice_9.apk
Split APKs installed
$ adb shell am start -n "com.example.myfirstapp/com.example.myfirstapp.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Waiting for process to come online
Connected to process 6972 on device emulator-5554
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/InstantRun: starting instant run server: is main process
D/OpenGLRenderer: HWUI GL Pipeline
I/zygote: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
D/OpenGLRenderer: Swap behavior 0
D/EGL_emulation: eglCreateContext: 0x9fe040c0: maj 3 min 0 rcv 3
D/EGL_emulation: eglMakeCurrent: 0x9fe040c0: ver 3 0 (tinfo 0x9fe032c0)
E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824 E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
D/EGL_emulation: eglMakeCurrent: 0x9fe040c0: ver 3 0 (tinfo 0x9fe032c0)
V/View: dispatchProvideAutofillStructure(): not laid out, ignoring 0 children of 1073741830
I/AssistStructure: Flattened final assist data: 2144 bytes, containing 1 windows, 8 views
I/zygote: Do partial code cache collection, code=27KB, data=30KB
I/zygote: After code cache collection, code=27KB, data=30KB
I/zygote: Increasing code cache capacity to 128KB
(The quoted section in this log is in red color, possibly error.)
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<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/AppTheme">
<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=".DisplayMessageActivity"
android:parentActivityName=".MainActivity">
<!-- The meta-data tag is required if you support API level 15 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
</application>
</manifest>
Also, please tell me, if I can put the location
in the textview as string.
Update After adding the permission, my DisplayMessege is:
package com.example.myfirstapp;
import android.content.Intent;
import android.location.Location;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnSuccessListener;
public boolean checkLocationPermission() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_FINE_LOCATION)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
new AlertDialog.Builder(this)
.setTitle(R.string.title_location_permission)
.setMessage(R.string.text_location_permission)
.setPositiveButton(R.string.ok_dialog_location, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//Prompt the user once explanation has been shown
ActivityCompat.requestPermissions(PlaceOrder.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION);
}
})
.create()
.show();
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION);
}
return false;
} else {
return true;
}
}
public class DisplayMessageActivity extends AppCompatActivity {
private FusedLocationProviderClient mFusedLocationClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
//Location
if (checkLocationPermission()) {
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
mFusedLocationClient.getLastLocation()
.addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
// Got last known location. In some rare situations this can be null.
if (location != null) {
// Logic to handle location object
}
}
});
}
else {
Toast.makeText(this, "Location Permision is not granted", Toast.LENGTH_SHORT).show();
}
}
// Capture the layout's TextView and set the string as its text
TextView textView = findViewById(R.id.textView);
textView.setText(message);
}
}
Which is giving build error:
/home/rudra/AndroidStudioProjects/MyFirstApp/app/src/main/java/com/example/myfirstapp/DisplayMessageActivity.java
Error:(13, 8) error: class, interface, or enum expected
Error:(30, 31) error: class, interface, or enum expected
Error:(35, 24) error: class, interface, or enum expected
Error:(41, 8) error: class, interface, or enum expected
Error:(46, 9) error: class, interface, or enum expected
Error:(48, 5) error: class, interface, or enum expected
Error:(50, 5) error: class, interface, or enum expected
Error:(89, 25) error: <identifier> expected
Error:(89, 33) error: <identifier> expected
Error:(91, 1) error: class, interface, or enum expected
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
Information:BUILD FAILED in 0s
I know this is idotic and i am doing some silly error, but kindly help Regards,