0

我正在尝试编写代码以将我的电源线放在手机上。

我试图调试代码,我猜错误在 LocationListener ll=new myLocationListener();

轨道移动类

package prakash.work.trackmobile;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.widget.TextView;

public class getCords extends Activity {

TextView longi;
TextView lat;
TextView tv=(TextView) findViewById(R.id.tv);
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.get_cords);

    longi=(TextView) findViewById(R.id.longi);
    lat=(TextView) findViewById(R.id.lat);

    LocationManager lm=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
    LocationListener ll=new myLocationListener();
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);


}
class myLocationListener implements LocationListener{

    public void onLocationChanged(Location arg0) {
        if(arg0 != null)
        {
            tv.setText("Working");
            double plongi=arg0.getLongitude();
            double plat=arg0.getLatitude();

            longi.setText(Double.toString(plongi));
            lat.setText(Double.toString(plat));
        }

    }

    public void onProviderDisabled(String arg0) {
        // TODO Auto-generated method stub

    }

    public void onProviderEnabled(String arg0) {
        // TODO Auto-generated method stub

    }

    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        // TODO Auto-generated method stub

    }

}
}

##XML布局文件##

<LinearLayout 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:orientation="vertical">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Latitude"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/lat"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Longitude"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/longi"
    android:text=""
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

##显现##

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="prakash.work.trackmobile"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission
 android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission
android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission
android:name="android.permission.CONTROL_LOCATION_UPDATES" />
<uses-permission android:name="android.permission.INTERNET" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".getCords"
        android:label="@string/title_activity_get_cords" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>


</manifest>

或者还有什么我需要研究如何与听众合作

4

2 回答 2

1

TextView tv=(TextView) findViewById(R.id.tv);

你需要把它放在你的public void onCreate(Bundle savedInstanceState) {方法中setContentView(R.layout.get_cords);

TextView longi;
TextView lat;
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.get_cords);
    tv=(TextView) findViewById(R.id.tv);
    longi=(TextView) findViewById(R.id.longi);
    lat=(TextView) findViewById(R.id.lat);
于 2012-06-29T04:48:30.720 回答
-1

您通常会在 log cat 的前几行(这是第 5 行)中查看错误无法启动 getcords。尝试重新启动模拟器,因为这也是一个已知错误,如果错误再次出现,请在 android 清单中的 .getCords 前面添加包。(prakash.work.trackwork

于 2012-06-29T04:39:05.610 回答