Issue
I'm writing a program where after clicking this button another program will be started.
However, the program force closed when I click the corresponding button.
here's the code.
package com.example.pc.multi_functionapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button)findViewById(R.id.button_1);
Button button2 = (Button)findViewById(R.id.button_2);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent int1 = new Intent(MainActivity.this, Accelerometer.class);
startActivity(int1);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent int2 = new Intent(MainActivity.this, Locator.class);
startActivity(int2);
}
});
}
}
Here's the XML code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:layout_gravity="center_horizontal"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="@+id/button_1"
android:text="Android Accelerometer"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="@+id/button_2"
android:text="Android Current Location"
/>
</LinearLayout>
Here's the android manifest code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pc.multi_functionapp">
<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=".Locator"></activity>
<activity android:name=".Accelerometer" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</man
For the program itself, Accelerometer and Locator work properly if I running it one by one.
I'm creating this program in purpose, these two programs can be run in one app, no need to open it in a different app.
I'm expecting after I click the first button, the accelerometer program will be started and when I click the second button, the Locator program will be started.
what happened is, when I click it the program just force closed.
please help me so the button works properly when clicked.
Edit : Accelerometer Code
package com.example.pc.multi_functionapp;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class Accelerometer extends AppCompatActivity implements SensorEventListener {
private static final String TAG = "MainActivity";
private SensorManager sensorManager;
Sensor accelerometer;
TextView xValue, yValue, zValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xValue = (TextView) findViewById(R.id.xValue);
yValue = (TextView) findViewById(R.id.yValue);
zValue = (TextView) findViewById(R.id.zValue);
Log.d(TAG, "onCreate: initializing Sensor Services");
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorManager.registerListener(Accelerometer.this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
Log.d(TAG, "onCreate: Registered accelerometer listener");
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
Log.d(TAG, "onSensorChanged: X: " + sensorEvent.values[0] + "Y: " + sensorEvent.values[1] + "Z: " + sensorEvent.values[2]);
xValue.setText("xValue: " + sensorEvent.values[0]);
yValue.setText("yValue: " + sensorEvent.values[1]);
zValue.setText("xValue: " + sensorEvent.values[2]);
}
}
Locator Code
package com.example.pc.multi_functionapp;
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
public class Locator extends AppCompatActivity {
TextView textView1, textView2, textView3, textView4, textView5;
FusedLocationProviderClient fusedLocationProviderClient;
Button btLocation;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btLocation = findViewById(R.id.bt_location);
textView1 = findViewById(R.id.text_view1);
textView2 = findViewById(R.id.text_view2);
textView3 = findViewById(R.id.text_view3);
textView4 = findViewById(R.id.text_view4);
textView5 = findViewById(R.id.text_view5);
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
btLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ActivityCompat.checkSelfPermission(Locator.this
, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
getLocation();
}
else {
ActivityCompat.requestPermissions(Locator.this
, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 44);
}
}
});
}
private void getLocation() {
fusedLocationProviderClient.getLastLocation().addOnCompleteListener(new OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
Location location = task.getResult();
if (location != null){
try {
Geocoder geocoder = new Geocoder(Locator.this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1 );
textView1.setText(Html.fromHtml("<font color='#6200EE'><B>Latitude :</b><br></font>" + addresses.get(0).getLatitude()));
textView2.setText(Html.fromHtml("<font color='#6200EE'><B>Longitude :</b><br></font>" + addresses.get(0).getLongitude()));
textView3.setText(Html.fromHtml("<font color='#6200EE'><B>Country Name :</b><br></font>" + addresses.get(0).getCountryName()));
textView4.setText(Html.fromHtml("<font color='#6200EE'><B>Locality :</b><br></font>" + addresses.get(0).getLocality()));
textView5.setText(Html.fromHtml("<font color='#6200EE'><B>Address :</b><br></font>" + addresses.get(0).getAddressLine(0)));
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
}
}
Solution
Oh, your error is simple.
I think you have copied the Activities and you have forgotten to change the layout in the setContentView()
of all of your Activities. all of them are R.layout.activity_main
so the app is starting the activity_main.xml but the view are getting findViewById()
from their real resources.
Answered By - Amir Hossein
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.