我的代码在模拟器上编译并运行,但是当按下按钮时,它会停止并显示消息"the application stopped on unexpectedly"
。我经常搜索并阅读一些相关的帖子,但他们没有帮助。你可以帮帮我吗?我收到以下消息:"Fatal Exception:main"message
main XML file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.testing.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/btnShowLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="93dp"
android:paddingLeft="20dp"
android:paddingRight="20dp" />
</RelativeLayout>
我的活动是
package com.example.testing;
import java.util.Arrays;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
double latituden;
double longtituden;
double [][] matrix_a = {{33.977517,51.456953},
{33.980738,51.426285},
{33.989817,51.433273},
{33.994729,51.437565},
{33.995928,51.447210},
{33.992708,51.446357},
{33.984604,51.436315},
{33.974430,51.433501},
{33.983399,51.445335},
{33.978474,51.466491}
};{
for (int i=0;i<matrix_a.length;i++)
{
latituden=i;
for (int j=0;j<matrix_a.length;j++){
longtituden=j;
}}};
Button btnShowLocation;
// GPSTracker class
GPSTracker gps;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(getApplicationContext(), "welcome to this app" , Toast.LENGTH_LONG).show();
btnShowLocation = (Button) findViewById(R.id.btnShowLocation);
// show location button click event
btnShowLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// create class object
gps = new GPSTracker(MainActivity.this);
// check if GPS enabled
if(gps.canGetLocation()){
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
// \n is for new line
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
}else{
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
gps.showSettingsAlert();
}
for (int i=0;i<matrix_a.length;i++)
{
for (int j=0;j<matrix_a.length;j++){
Distclass distclass= new Distclass();
double[] distance = distclass.Calculate(gps.getLatitude(),gps.getLongitude(),latituden,longtituden);
Arrays.sort(distance);
double min = distance[0];
System.out.println(min);
Toast.makeText(getApplicationContext(),
"min distance from your location is - \nmin: " + min, Toast.LENGTH_LONG).show();
}
};
}});}}
答案 0 :(得分:0)
这可能会导致崩溃的原因有很多,而且您提供的详细信息我可以想到几个:
您是否已在Activity
中声明了自己的活动。您的应用程序中的所有活动都必须在AndroidManifest.xml
标记内(如此
<application></application>
什么是GPSTracker?它是从某个地方进口的吗?崩溃可能与该类有关。
检查您的logcat。那里应该可以看到崩溃。可以从Android Studio访问Logcat功能。有关详情,请查看this