Android 在移动设备上测试时,没有错误的代码会崩溃

Android 在移动设备上测试时,没有错误的代码会崩溃,android,location-services,Android,Location Services,对于Android Studio,我是个新手,但我已经成功地将两个活动组合在一起,它们在单独的应用程序下工作得很好,但是当加入时,当MainActivity使用Intent和startActivity调用AddressActivity时,它崩溃了 还有另一个问题可能与此相关,也可能与此无关。安装应用程序时,未请求任何权限 这是我的密码: AndroidManifest.xml <uses-permission android:name="android.permission.INTERNE

对于Android Studio,我是个新手,但我已经成功地将两个活动组合在一起,它们在单独的应用程序下工作得很好,但是当加入时,当
MainActivity
使用
Intent
startActivity
调用
AddressActivity
时,它崩溃了

还有另一个问题可能与此相关,也可能与此无关。安装应用程序时,未请求任何权限

这是我的密码:

AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COURSE_LOCATION" />
 <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    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=".AddressActivity"
        android:label="login">
    </activity>
</application>
地址活动类

package crproductionsptyltd.login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import static crproductionsptyltd.login.R.id.driverid;
import static crproductionsptyltd.login.R.id.login;

public class MainActivity extends Activity {
    EditText driverid;
    Button btnOk;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        // find View-elements
        btnOk = (Button) findViewById(login);
     //   btnOk.setOnClickListener(this)
        // create click listener
        View.OnClickListener oclBtnOk = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // change text of the TextView (tvOut)
                btnOk.setText("Logging IN.....");

                Intent i = new Intent(MainActivity.this, AddressActivity.class);
                i.putExtra("driver_id", driverid.getText().toString());
                startActivity(i);
            };

        };
        // assign click listener to the OK button (btnOK)
        btnOk.setOnClickListener(oclBtnOk);
        //   startActivity(new Intent(MainActivity.this, AddressActivity.class));
       // Intent i = new Intent(MainActivity.this, NewActivity.class);//
        // startActivity(i);
    };
}
package crproductionsptyltd.login;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Locale;
import android.app.Activity;
import android.content.Context;
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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.content.Intent;

public class AddressActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    String lat = "", lon = "";
    TextView tvView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gps);
        tvView = (TextView) findViewById(R.id.tvView);
        Intent intent = getIntent();
        String driverid = intent.getStringExtra("driver_id");
        tvView.setText("Your Driver ID is: " + driverid);

        Button btnLocation = (Button) findViewById(R.id.btnLocation);
        btnLocation.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                // Acquire a reference to the system Location Manager
                LocationManager locationManager = (LocationManager) AddressActivity.this.getSystemService(Context.LOCATION_SERVICE);
                // Define a listener that responds to location updates
                LocationListener locationListener = new LocationListener() {
                    public void onLocationChanged(Location location) {
                        // Called when a new location is found by the network location provider.
                        lat = Double.toString(location.getLatitude());
                        lon = Double.toString(location.getLongitude());
                        TextView tv = (TextView) findViewById(R.id.txtLoc);
                        tv.setText("Your Location is:" + lat + "--" + lon);
                    }

                    public void onStatusChanged(String provider, int status, Bundle extras) {
                    }

                    public void onProviderEnabled(String provider) {
                    }

                    public void onProviderDisabled(String provider) {
                    }
                };
                // Register the listener with the Location Manager to receive location updates

                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
            }
        });

        Button btnSend = (Button) findViewById(R.id.btnSend);
        btnSend.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                postData(lat, lon);
            }
        });

        Button btnAdd = (Button) findViewById(R.id.btnAddress);
        btnAdd.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                TextView tv = (TextView) findViewById(R.id.txtAddress);
                tv.setText(GetAddress(lat, lon));
            }
        });
    }


    public void postData(String la, String lo) {
        //URL url = new URL("https://www.autoflora.net/driver/gps.php?Driver_ID=877&latlong=" + lat + "*" + lon);
        int TIMEOUT_VALUE = 15000;
            try{
                URL myUrl = new URL("https://www.autoflora.net/driver/gps.php?Driver_ID=877&latlong=" + lat + "*" + lon);
                URLConnection connection = myUrl.openConnection();
                connection.setConnectTimeout(TIMEOUT_VALUE);
                connection.connect();
            } catch (Exception e) {
            }
        }

    public String GetAddress(String lat, String lon)
    {
        Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);
        String ret = "";
        try {
            List<Address> addresses = geocoder.getFromLocation(Double.parseDouble(lat), Double.parseDouble(lon), 1);
            if(addresses != null) {
                Address returnedAddress = addresses.get(0);
                StringBuilder strReturnedAddress = new StringBuilder("Address:\n");
                for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
                    strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
                }
                ret = strReturnedAddress.toString();
            }
            else{
                ret = "No Address returned!";
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            ret = "Can't get Address!";
        }
        return ret;
    }
}
package crproductionsptyltd.login;
导入java.io.IOException;
导入java.net.URL;
导入java.net.URLConnection;
导入java.util.List;
导入java.util.Locale;
导入android.app.Activity;
导入android.content.Context;
导入android.location.Address;
导入android.location.Geocoder;
导入android.location.location;
导入android.location.LocationListener;
导入android.location.LocationManager;
导入android.os.Bundle;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.TextView;
导入android.content.Intent;
公共类AddressActivity扩展活动{
/**
*在首次创建活动时调用。
*/
字符串lat=“”,lon=“”;
文本视图tvView;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gps);
tvView=(TextView)findViewById(R.id.tvView);
Intent=getIntent();
String driverid=intent.getStringExtra(“驱动程序id”);
setText(“您的驱动程序ID为:“+driverid”);
按钮btnLocation=(按钮)findViewById(R.id.btnLocation);
setOnClickListener(新的OnClickListener(){
公共void onClick(视图arg0){
//获取对系统位置管理器的引用
LocationManager LocationManager=(LocationManager)AddressActivity.this.getSystemService(Context.LOCATION\u服务);
//定义响应位置更新的侦听器
LocationListener LocationListener=新LocationListener(){
已更改位置上的公共无效(位置){
//当网络位置提供程序找到新位置时调用。
lat=Double.toString(location.getLatitude());
lon=Double.toString(location.getLongitude());
TextView tv=(TextView)findViewById(R.id.txtLoc);
tv.setText(“您的位置是:“+lat+”--“+lon”);
}
public void onStatusChanged(字符串提供程序、int状态、Bundle extra){
}
公共无效onProviderEnabled(字符串提供程序){
}
公共无效onProviderDisabled(字符串提供程序){
}
};
//向位置管理器注册侦听器以接收位置更新
locationManager.RequestLocationUpdate(locationManager.GPS\提供程序,0,0,locationListener);
}
});
按钮btnSend=(按钮)findviewbyd(R.id.btnSend);
setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
postData(纬度、经度);
}
});
按钮btnAdd=(按钮)findViewById(R.id.btnAddress);
setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
TextView tv=(TextView)findViewById(R.id.txtAddress);
tv.setText(GetAddress(lat,lon));
}
});
}
公共void postData(字符串la、字符串lo){
//URL=新URL(“https://www.autoflora.net/driver/gps.php?Driver_ID=877&latlong=“+lat+”*“+lon);
int TIMEOUT_值=15000;
试一试{
URL myUrl=新URL(“https://www.autoflora.net/driver/gps.php?Driver_ID=877&latlong=“+lat+”*“+lon);
URLConnection=myUrl.openConnection();
connection.setConnectTimeout(超时值);
connection.connect();
}捕获(例外e){
}
}
公共字符串GetAddress(字符串lat、字符串lon)
{
Geocoder Geocoder=新的地理编码器(this,Locale.ENGLISH);
字符串ret=“”;
试一试{
列表地址=geocoder.getFromLocation(Double.parseDouble(lat),Double.parseDouble(lon),1);
如果(地址!=null){
返回的地址地址=地址。获取(0);
StringBuilder strReturnedAddress=新StringBuilder(“地址:\n”);

对于(inti=0;i我想,使用android.support.design.widget.TextInputLayout 您应该使用这些依赖项

compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:design:22.2.0'
&也请更正

btnOk = (Button) findViewById(R.id.login);            //wrongly added
driverid = (EditText) findViewById(R.id.driverid);    // not added

根据您的代码,应用程序将在此处崩溃:
i.putExtra(“driver\u id”,driverid.getText().toString())
,导致。它崩溃是因为您在
onCreate
中没有对
EditText driverid;
进行初始化,因此,在未初始化的情况下使用时将引发异常

要解决此问题,您必须更新
onCreate
中的字段:

driverid = (EditText) findViewById(driverid);

如果应用程序访问用户的当前位置,您可能需要允许应用程序访问用户的位置。请尝试这样做,然后看看它是如何运行的。你好,温家宝,谢谢你的评论。我相信“在我的AndoidManifest.xml中就是这样的。对不对?应该是“是”,但你也可以在此处阅读更多信息:我建议你附加c太棒了。非常感谢你的故障排除露莲。太好了。非常感谢阿什
btnOk = (Button) findViewById(R.id.login);            //wrongly added
driverid = (EditText) findViewById(R.id.driverid);    // not added
driverid = (EditText) findViewById(driverid);