http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/由于onClickListener造成的崩溃
初学者的android dev在这里。 我目前正在按照本指南尝试创建一个返回用户位置的Android应用程序。
package com.example.newapp;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
Button btnShowLocation;
GPSTracker gps;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
btnShowLocation = (Button) findViewById(R.id.btnShowLocation);
//show location button click event
btnShowLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//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.showSettingsAlert();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
public void sendLocation(View view){
AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
builder1.setMessage("Your latitude is " + gps.getLatitude() + " and your longitude is " + gps.getLongitude());
builder1.setCancelable(true);
AlertDialog alert1 = builder1.create();
alert1.show();
}
}
我fragment_main.xml是像这样
<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.newapp.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<EditText android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
<Button
android:id="@+id/btnShowLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="118dp"
android:text="Button"
android:onClick="sendLocation" />
</RelativeLayout>
我注释掉onClickListener的东西,我的应用程序正常工作,所以我知道这个问题是这一点。我只是不确定它会在启动时崩溃我的应用程序。
我跟着这个,现在我的应用程序启动,但当我按下按钮时它崩溃。 我把onClickListener放在“gps = new GPSTracker(gps);”以及Toast.makeTest(gps,...);因为其他参数不适用于片段中的代码。 – Divide0 2014-11-23 07:45:37
没关系,我通过使用getActivity()来取得它的功能。非常感谢你的帮助! – Divide0 2014-11-23 08:04:28