我制作了一个Android应用程序,可以找到atm,银行在您当前的位置或自定义位置。但是当我点击按钮时,应用程序会在不在我设备上的其他设备上崩溃
这是我的logcat
FATAL EXCEPTION: main
Process: com.example.udaygarg.locationplacedetailsv2, PID: 27580java.lang.NullPointerException
at com.example.udaygarg.locationplacedetailsv2.StartActivity.onClick(StartActivity.java:158)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18439)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5146)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
at dalvik.system.NativeStart.main(Native Method)
这是布局文件activity_start.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ATM"
android:id="@+id/button"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:textSize="15dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/button4"
android:layout_alignEnd="@+id/button4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Airport"
android:id="@+id/button2"
android:layout_alignTop="@+id/button"
android:layout_centerHorizontal="true"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:textSize="15dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bank"
android:id="@+id/button3"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_alignTop="@+id/button2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignLeft="@+id/button6"
android:layout_alignStart="@+id/button6"
android:textSize="15dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bus Station"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:id="@+id/button4"
android:layout_below="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="15dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Doctor"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:id="@+id/button5"
android:layout_alignBottom="@+id/button4"
android:layout_alignRight="@+id/button2"
android:layout_alignEnd="@+id/button2"
android:textSize="15dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hospital"
android:id="@+id/button6"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignTop="@+id/button5"
android:textSize="15dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Find new location"
android:id="@+id/button8"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_below="@+id/editText"
android:layout_toRightOf="@+id/button2"
android:layout_toEndOf="@+id/button2"
android:layout_marginTop="54dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Church"
android:id="@+id/button9"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/button4"
android:layout_alignRight="@+id/button4"
android:layout_alignEnd="@+id/button4"
android:textSize="15dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gym"
android:id="@+id/button10"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_below="@+id/button4"
android:layout_alignLeft="@+id/button5"
android:layout_alignStart="@+id/button5"
android:layout_alignRight="@+id/button5"
android:layout_alignEnd="@+id/button5"
android:layout_alignBottom="@+id/button9"
android:textSize="15dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="School"
android:id="@+id/button12"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_below="@+id/button9"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/button9"
android:layout_alignEnd="@+id/button9"
android:textSize="15dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Park"
android:id="@+id/button13"
android:layout_alignTop="@+id/button12"
android:layout_alignLeft="@+id/button10"
android:layout_alignStart="@+id/button10"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_alignRight="@+id/button10"
android:layout_alignEnd="@+id/button10"
android:layout_alignBottom="@+id/button12"
android:textSize="15dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bar"
android:id="@+id/button14"
android:layout_alignTop="@+id/button13"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_alignBottom="@+id/button13"
android:layout_alignLeft="@+id/button6"
android:layout_alignStart="@+id/button6"
android:textSize="15dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mosque"
android:id="@+id/button11"
android:layout_below="@+id/button6"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="15dp"
android:layout_toEndOf="@+id/button10"
android:layout_alignLeft="@+id/button14"
android:layout_alignStart="@+id/button14" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:hint="Enter prefered radius between 2 - 50 km"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:layout_alignBottom="@+id/button8"
android:layout_toLeftOf="@+id/button8"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:hint="Enter location eg. sector-55,Noida/Delhi"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set to Current Location"
android:id="@+id/button7"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_below="@+id/button8"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toRightOf="@+id/editText2"
android:layout_toEndOf="@+id/editText2" />
</RelativeLayout>
manifestfile.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.udaygarg.locationplacedetailsv2">
<permission
android:name="com.example.udaygarg.locationplacedetailsv2.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.udaygarg.locationplacedetailsv2.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name1"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Splashscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".StartActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".PlaceDetailsActivity">
<intent-filter>
<action android:name=".PlaceDetails" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBcL4XaHzcJCslvOq5Q9jfT2XIXf3TmVIo"/>
</application>
</manifest>
StartActivity.java
package com.example.udaygarg.locationplacedetailsv2;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.io.IOException;
import java.util.List;
public class StartActivity extends Activity implements View.OnClickListener, LocationListener {
Button bt1, bt2, bt3, bt4, bt5, bt6, bt7, bt8, bt9, bt10, bt11, bt12, bt13, bt14;
ConnectionDetector cd;
Boolean isInternetPresent = false;
int radius = 2;
EditText et1, et2;
double latitude, longitude;
Intent in;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
bt1 = (Button) findViewById(R.id.button);
bt1.setOnClickListener(this);
bt2 = (Button) findViewById(R.id.button2);
bt2.setOnClickListener(this);
bt3 = (Button) findViewById(R.id.button3);
bt3.setOnClickListener(this);
bt4 = (Button) findViewById(R.id.button4);
bt4.setOnClickListener(this);
bt5 = (Button) findViewById(R.id.button5);
bt5.setOnClickListener(this);
bt6 = (Button) findViewById(R.id.button6);
bt6.setOnClickListener(this);
bt7 = (Button) findViewById(R.id.button9);
bt7.setOnClickListener(this);
bt8 = (Button) findViewById(R.id.button10);
bt8.setOnClickListener(this);
bt9 = (Button) findViewById(R.id.button11);
bt9.setOnClickListener(this);
bt10 = (Button) findViewById(R.id.button12);
bt10.setOnClickListener(this);
bt11 = (Button) findViewById(R.id.button13);
bt11.setOnClickListener(this);
bt12 = (Button) findViewById(R.id.button14);
bt12.setOnClickListener(this);
bt13= (Button) findViewById(R.id.button7);
bt13.setOnClickListener(this);
bt14= (Button) findViewById(R.id.button8);
bt14.setOnClickListener(this);
et1 = (EditText) findViewById(R.id.editText);
et2 = (EditText) findViewById(R.id.editText2);
cd = new ConnectionDetector(getApplicationContext());
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.button) {
isInternetPresent = cd.isConnectingToInternet();
// check for Internet status
if (isInternetPresent) {
// Internet Connection is Present
// make HTTP requests
sendquerydata("atm", radius, latitude, longitude);
} else {
// Internet connection is not present
// Ask user to connect to Internet
showAlertDialog(StartActivity.this, "No Internet Connection",
"You don't have internet connection. Connect to the internet", false);
}
} else if (v.getId() == R.id.button8) {
isInternetPresent = cd.isConnectingToInternet();
if (isInternetPresent) {
Geocoder geocoder = new Geocoder(this);
String address = et2.getText().toString();
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocationName(address, 1);
} catch (IOException e) {
e.printStackTrace();
}
if (addresses.size() > 0) {
latitude = addresses.get(0).getLatitude();
longitude = addresses.get(0).getLongitude();
}
sendquerydata("", 2, latitude, longitude);
}
else{
showAlertDialog(StartActivity.this, "No Internet Connection",
"You don't have internet connection. Connect to the internet", false);
}
}
else if (v.getId() == R.id.button7) {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
Location location;
location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
latitude = location.getLatitude();
longitude = location.getLongitude();
sendquerydata("", 2, latitude, longitude);
}
点击按钮7和8时应用程序崩溃了。请帮忙解决这个问题 ps:我第一次在这里问一个问题
答案 0 :(得分:0)
为块按钮7和按钮8编写单独的函数,然后检查权限是否未授予调用requestPermissions,如果用户授予它,则调用您私有写入的函数,如果已经授予权限,则立即运行该函数。请注意,在api 23编写if语句以检查SDK_INT是否高于23之前,您不需要检查权限
答案 1 :(得分:0)
将IF置于上面的Try中,也许在Try内部的地址分配不正常,所以你有空地址,这里你得到了异常