项目意外停止了

时间:2012-06-29 04:32:12

标签: java android eclipse

我正在尝试编写代码以便在手机上获取我的电线。

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

trackmobileclass

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>

或者还有什么我需要研究的 如何使用听众

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:-1)

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