-1

this is my layout file

                        <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): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 07-20 05:48:24.134: E/AndroidRuntime(1538): at android.app.ActivityThread.access$600(ActivityThread.java:141) 07-20 05:48:24.134: E/AndroidRuntime(1538): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 07-20 05:48:24.134: E/AndroidRuntime(1538): at android.os.Handler.dispatchMessage(Handler.java:99) 07-20 05:48:24.134: E/AndroidRuntime(1538): at android.os.Looper.loop(Looper.java:137) 07-20 05:48:24.134: E/AndroidRuntime(1538): at 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): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 07-20 05:48:24.134: E/AndroidRuntime(1538): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 07-20 05:48:24.134: E/AndroidRuntime(1538): at dalvik.system.NativeStart.main(Native Method) 07-20 05:48:24.134: E/AndroidRuntime(1538): Caused by: java.lang.NullPointerException 07-20 05:48:24.134: E/AndroidRuntime(1538): at android.app.Activity.findViewById(Activity.java:1853) 07-20 05:48:24.134: E/AndroidRuntime(1538): at com.example.gsrtc_driver.MainActivity.(MainActivity.java:24) 07-20 05:48:24.134: E/AndroidRuntime(1538): at java.lang.Class.newInstanceImpl(Native Method) 07-20 05:48:24.134: E/AndroidRuntime(1538): at java.lang.Class.newInstance(Class.java:1130) 07-20 05:48:24.134: E/AndroidRuntime(1538): at android.app.Instrumentation.newActivity(Instrumentation.java:1061) 07-20 05:48:24.134: E/AndroidRuntime(1538): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128) 07-20 05:48:24.134: E/AndroidRuntime(1538): ... 11 more

androidmanifest.xml

this is my 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>
  • Did you add your activity in the AndroidManifest.xml ? – spacebiker Jul 20 '14 at 10:16
  • Please post some more explanation and relevant parts of your code. – Aleksander Lidtke Jul 20 '14 at 10:17
  • yes i added in manifest.xml – Jainam Mehta Jul 20 '14 at 10:17
  • can you post your manifest.xml and the full stacktrace? – spacebiker Jul 20 '14 at 10:17
  • @AleksanderLidtke here it is in MainActivity.java i add on click listener on button and put extra values in it [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); ] – Jainam Mehta Jul 20 '14 at 10:19
  • by the info you provide i just can tell you that findviewbyid returns null, probably because you are looking for a view that does not belong to the current activity's view hierarchy. – spacebiker Jul 20 '14 at 10:27
  • @Xabier ther is only one activity that has component and second one is blank xml – Jainam Mehta Jul 20 '14 at 10:40

2 Answers2

0

Seems that there is a problem in findViewById in your Activity.

Can you please post your Activity code and activity layout code?

Edit:

public class ExampleActivity extends Activity {

    private EditText mSampleEdit;

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

        mSampleEdit = (EditText) findViewById(R.id.sampleEdit);
    }
}
AlexBalo
  • 1,258
  • 1
  • 11
  • 16
  • In your manifest can you try writing packageNames lowercased? I see also other strange things in term of Java convention. I would rename Start_Tracking to StartTracking for example. http://en.wikipedia.org/wiki/Naming_convention_(programming)#Java This is a good starting point for improving your code as well. – AlexBalo Jul 20 '14 at 10:42
  • ya but it means i have to make whole project from start and this kinda errors comes often with lower case package name also... can you provide exact solution ? i am stuck in this from last 10 days – Jainam Mehta Jul 20 '14 at 10:44
  • Move your findViewById in the onCreate method. You cannot define them like you did. Define your widget and in the onCreate method assign them the corresponding view using the id – AlexBalo Jul 20 '14 at 10:46
  • i don't know about findviewbyid method that it can't be define outside and also when i want to use value of edittext in MyLocationListener class it show error so i did it globally. – Jainam Mehta Jul 20 '14 at 10:54
  • But it's wrong logically. In the onCreate the activity knows with layout to use. If you do like you did the activity doesn't know where to get your editexts from. I would suggest you to change your code as I did in my edited answer. Keep the edittext as an instance variable but initialize it in onCreate – AlexBalo Jul 20 '14 at 10:57
0

I see.. you are trying to assign the widgets when the view is not created yet, try the following code:

Change:

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();

to:

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

String RTO_NO;
String FROM_STRING;
String TO_STRING;

then, in the 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);


}
spacebiker
  • 3,777
  • 4
  • 30
  • 49