-1

这是我的布局文件

                        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:orientation="vertical" >

                        <EditText
                            android:id="@+id/rto"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:ems="10"
                            android:hint="RTO Number" >

                            <requestFocus />
                        </EditText>

                        <EditText
                            android:id="@+id/from"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:ems="10"
                            android:hint="From" />

                        <EditText
                            android:id="@+id/to"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:ems="10" 
                            android:hint="To"/>

                        <Button
                            android:id="@+id/start_tracking"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="START TRACKING" />

                    </LinearLayout>

ManiActivity.java

                            package com.example.gsrtc_driver;

                        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.content.Intent;
                        import android.view.Menu;
                        import android.view.View;
                        import android.view.View.OnClickListener;
                        import android.widget.Button;
                        import android.widget.EditText;

                        public class MainActivity extends Activity implements OnClickListener {

                            static String Bus_latitude = "BUS_LAT";
                            static String Bus_longitude = "BUS_LON";
                            static String TAG_RTO = "BUS_RTO";
                            static String TAG_FROM = "BUS_FROM";
                            static String  TAG_TO = "BUS_TO";

                            EditText RTO = (EditText) findViewById(R.id.rto);
                            EditText FROM = (EditText) findViewById(R.id.from);
                            EditText TO = (EditText) findViewById(R.id.to);
                            Button Start_Tracking = (Button) findViewById(R.id.start_tracking);

                            String RTO_NO = RTO.getText().toString();
                            String FROM_STRING = FROM.getText().toString();
                            String TO_STRING = TO.getText().toString();


                            // GET LOCATION OF DRIVER


                            LocationManager lm;
                            LocationListener locationListener;


                            @Override
                            public void onCreate(Bundle savedInstanceState) {
                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.activity_main);

                                Start_Tracking.setOnClickListener(this);


                            }


                            @Override
                            public void onClick(View arg0) {
                                // TODO Auto-generated method stub
                                 lm = (LocationManager)
                                         getSystemService(Context.LOCATION_SERVICE);
                                         locationListener = new MyLocationListener();


                                        // ---request location updates---
                                            locationListener = new MyLocationListener();
                                            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                                                1000,1000, locationListener);


                            }

                            public class MyLocationListener implements LocationListener
                            {
                                @Override
                                public void onLocationChanged(Location arg0) {
                                    // TODO Auto-generated method stub
                                    double latitude = arg0.getLatitude();
                                    double longitude =  arg0.getLongitude();

                                     String Bus_lat = Double.toString(latitude);
                                     String Bus_lon = Double.toString(longitude);

                                     Intent starttracking = new Intent (MainActivity.this,Start_Tracking.class);

                                     starttracking.putExtra(Bus_lat, Bus_latitude);
                                     starttracking.putExtra(Bus_lon, Bus_longitude);
                                     starttracking.putExtra(RTO_NO, TAG_RTO);
                                     starttracking.putExtra(FROM_STRING,TAG_FROM );
                                     starttracking.putExtra(TO_STRING,TAG_TO );

                                     startActivity(starttracking);

                                     lm.removeUpdates(locationListener);
                                }



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

                                }

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

                                }

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

                                }

                            }
                        }

07-20 05:48:24.134: E/AndroidRuntime(1538): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 07-20 05:48:24.134: E/AndroidRuntime(1538): 在 android. app.ActivityThread.access$600(ActivityThread.java:141) 07-20 05:48:24.134: E/AndroidRuntime(1538): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 07-20 05 :48:24.134: E/AndroidRuntime(1538): 在 android.os.Handler.dispatchMessage(Handler.java:99) 07-20 05:48:24.134: E/AndroidRuntime(1538): 在 android.os.Looper。循环(Looper.java:137) 07-20 05:48:24.134: E/AndroidRuntime(1538): 在 android.app.ActivityThread.main(ActivityThread.java:5103) 07-20 05:48:24.134: E/ AndroidRuntime(1538): at java.lang.reflect.Method.invokeNative(Native Method) 07-20 05:48:24.134: E/AndroidRuntime(1538): at java.lang.reflect.Method.invoke(Method.java:525) 07-20 05:48:24.134: E/AndroidRuntime(1538): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 07-20 05:48:24.134: E/AndroidRuntime(1538): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 07-20 05:48:24.134: E/AndroidRuntime(1538): 在 dalvik.system.NativeStart.main (本机方法)07-20 05:48:24.134:E/AndroidRuntime(1538):原因:java.lang.NullPointerException 07-20 05:48:24.134:E/AndroidRuntime(1538):在 android.app.Activity .findViewById(Activity.java:1853) 07-20 05:48:24.134: E/AndroidRuntime(1538): 在 com.example.gsrtc_driver.MainActivity.(MainActivity.java:24) 07-20 05:48:24.134: E/AndroidRuntime(1538): 在 java.lang.Class.newInstanceImpl(Native Method) 07-20 05:48:24.134: E/AndroidRuntime(1538): 在 java.lang.Class.newInstance(Class.java:1130) 07-20 05:48:24.134: E/AndroidRuntime(1538): 在 android.app.Instrumentation.newActivity(Instrumentation.java:1061) 07-20 05:48:24.134: E/AndroidRuntime(1538): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread .java:2128) 07-20 05:48:24.134: E/AndroidRuntime(1538): ... 11 更多

androidmanifest.xml

这是我的 manifest.xml

                <?xml version="1.0" encoding="utf-8"?>
            <manifest xmlns:android="http://schemas.android.com/apk/res/android"
                package="com.example.gsrtc_driver"
                android:versionCode="1"
                android:versionName="1.0" >

                <uses-sdk
                    android:minSdkVersion="8"
                    android:targetSdkVersion="17" />

                <uses-permission android:name="android.permission.INTERNET" />
             <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
                <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

                <application
                    android:allowBackup="true"
                    android:icon="@drawable/ic_launcher"
                    android:label="@string/app_name"
                    android:theme="@style/AppTheme" >
                    <activity
                        android:name="in.online.gsrtc.DRIVER.MainActivity"
                        android:label="@string/app_name" >
                        <intent-filter>
                            <action android:name="android.intent.action.MAIN" />

                            <category android:name="android.intent.category.LAUNCHER" />
                        </intent-filter>
                    </activity>
                    <activity
                        android:name="in.online.gsrtc.DRIVER.Start_Tracking"
                        android:label="@string/title_activity_start__tracking" >
                    </activity>
                </application>

            </manifest>
4

2 回答 2

0

似乎您的 Activity 中的 findViewById 存在问题。

您能否发布您的活动代码和活动布局代码?

编辑:

public class ExampleActivity extends Activity {

    private EditText mSampleEdit;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mSampleEdit = (EditText) findViewById(R.id.sampleEdit);
    }
}
于 2014-07-20T10:30:40.610 回答
0

我明白了..您正在尝试在尚未创建视图时分配小部件,请尝试以下代码:

改变:

EditText RTO = (EditText) findViewById(R.id.rto);
EditText FROM = (EditText) findViewById(R.id.from);
EditText TO = (EditText) findViewById(R.id.to);
Button Start_Tracking = (Button) findViewById(R.id.start_tracking);

String RTO_NO = RTO.getText().toString();
String FROM_STRING = FROM.getText().toString();
String TO_STRING = TO.getText().toString();

至:

EditText RTO;
EditText FROM;
EditText TO;
Button Start_Tracking;

String RTO_NO;
String FROM_STRING;
String TO_STRING;

然后,在 onCreate 中:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    RTO = (EditText) findViewById(R.id.rto);
    FROM = (EditText) findViewById(R.id.from);
    TO = (EditText) findViewById(R.id.to);
    Start_Tracking = (Button) findViewById(R.id.start_tracking);

    RTO_NO = RTO.getText().toString();
    FROM_STRING = FROM.getText().toString();
    TO_STRING = TO.getText().toString();


    Start_Tracking.setOnClickListener(this);


}
于 2014-07-20T11:06:50.420 回答