0

I am extremely new when it comes to Android Studio but have managed to put together 2 activities which work fine under separate apps, but when joined, it crashed when the MainActivity calls AddressActivity usingIntent and startActivity.

There is another problem which maybe related or may not be. When installing the app, there are NO permissions requested.

Here is my code:

AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COURSE_LOCATION" />
 <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".AddressActivity"
        android:label="login">
    </activity>
</application>

MainActivity.class

package crproductionsptyltd.login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import static crproductionsptyltd.login.R.id.driverid;
import static crproductionsptyltd.login.R.id.login;

public class MainActivity extends Activity {
    EditText driverid;
    Button btnOk;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        // find View-elements
        btnOk = (Button) findViewById(login);
     //   btnOk.setOnClickListener(this)
        // create click listener
        View.OnClickListener oclBtnOk = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // change text of the TextView (tvOut)
                btnOk.setText("Logging IN.....");

                Intent i = new Intent(MainActivity.this, AddressActivity.class);
                i.putExtra("driver_id", driverid.getText().toString());
                startActivity(i);
            };

        };
        // assign click listener to the OK button (btnOK)
        btnOk.setOnClickListener(oclBtnOk);
        //   startActivity(new Intent(MainActivity.this, AddressActivity.class));
       // Intent i = new Intent(MainActivity.this, NewActivity.class);//
        // startActivity(i);
    };
}

AddressActivity.class

package crproductionsptyltd.login;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Locale;
import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.content.Intent;

public class AddressActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    String lat = "", lon = "";
    TextView tvView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gps);
        tvView = (TextView) findViewById(R.id.tvView);
        Intent intent = getIntent();
        String driverid = intent.getStringExtra("driver_id");
        tvView.setText("Your Driver ID is: " + driverid);

        Button btnLocation = (Button) findViewById(R.id.btnLocation);
        btnLocation.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                // Acquire a reference to the system Location Manager
                LocationManager locationManager = (LocationManager) AddressActivity.this.getSystemService(Context.LOCATION_SERVICE);
                // Define a listener that responds to location updates
                LocationListener locationListener = new LocationListener() {
                    public void onLocationChanged(Location location) {
                        // Called when a new location is found by the network location provider.
                        lat = Double.toString(location.getLatitude());
                        lon = Double.toString(location.getLongitude());
                        TextView tv = (TextView) findViewById(R.id.txtLoc);
                        tv.setText("Your Location is:" + lat + "--" + lon);
                    }

                    public void onStatusChanged(String provider, int status, Bundle extras) {
                    }

                    public void onProviderEnabled(String provider) {
                    }

                    public void onProviderDisabled(String provider) {
                    }
                };
                // Register the listener with the Location Manager to receive location updates

                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
            }
        });

        Button btnSend = (Button) findViewById(R.id.btnSend);
        btnSend.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                postData(lat, lon);
            }
        });

        Button btnAdd = (Button) findViewById(R.id.btnAddress);
        btnAdd.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                TextView tv = (TextView) findViewById(R.id.txtAddress);
                tv.setText(GetAddress(lat, lon));
            }
        });
    }


    public void postData(String la, String lo) {
        //URL url = new URL("https://www.autoflora.net/driver/gps.php?Driver_ID=877&latlong=" + lat + "*" + lon);
        int TIMEOUT_VALUE = 15000;
            try{
                URL myUrl = new URL("https://www.autoflora.net/driver/gps.php?Driver_ID=877&latlong=" + lat + "*" + lon);
                URLConnection connection = myUrl.openConnection();
                connection.setConnectTimeout(TIMEOUT_VALUE);
                connection.connect();
            } catch (Exception e) {
            }
        }

    public String GetAddress(String lat, String lon)
    {
        Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);
        String ret = "";
        try {
            List<Address> addresses = geocoder.getFromLocation(Double.parseDouble(lat), Double.parseDouble(lon), 1);
            if(addresses != null) {
                Address returnedAddress = addresses.get(0);
                StringBuilder strReturnedAddress = new StringBuilder("Address:\n");
                for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
                    strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
                }
                ret = strReturnedAddress.toString();
            }
            else{
                ret = "No Address returned!";
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            ret = "Can't get Address!";
        }
        return ret;
    }
}

activity_login.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:gravity="center_horizontal"
    android:orientation="vertical"
    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="crproductionsptyltd.login.LoginActivity">

    <!-- Login progress -->
    <ProgressBar
        android:id="@+id/login_progress"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:visibility="gone" />

    <ScrollView
        android:id="@+id/login_form"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/email_login_form"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <AutoCompleteTextView
                    android:id="@+id/driverid"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Driver ID"
                    android:inputType="textEmailAddress"
                    android:maxLines="1"
                    android:singleLine="true" />

            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            </android.support.design.widget.TextInputLayout>

            <Button
                android:id="@+id/login"
                style="?android:textAppearanceSmall"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:text="Login"
                android:textStyle="bold"
                android:onClick="checkDriver" />

        </LinearLayout>
    </ScrollView>
</LinearLayout>

activity_gps.class

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

    <TextView
        android:id="@+id/txtLoc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:id="@+id/tvView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:textSize="20sp">
    </TextView>


    <Button
        android:id="@+id/btnLocation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Get my Location" />

    <Button
        android:id="@+id/btnSend"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send Location" />

    <Button
        android:id="@+id/btnAddress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Try Get Street Address" />

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

</LinearLayout>
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Ben
  • 59
  • 2
  • 7
  • If the app accesses to the user's current location, you may need allow permission for the app to access to user's location. Try doing that and see how it goes. – WQYeo Jan 17 '17 at 07:45
  • Hi Wen, Thanks for your comment. I believe ` is for that in my AndoidManifest.xml. Is that not right? – Ben Jan 17 '17 at 07:50
  • It should be a yes, but you can also read more about it here : https://developer.android.com/training/permissions/requesting.html – WQYeo Jan 17 '17 at 07:52
  • I suggest you attach the crash log – Fer Jan 17 '17 at 08:21

2 Answers2

0

I think, to use android.support.design.widget.TextInputLayout you should use these dependencies

compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:design:22.2.0'

& also please correct

btnOk = (Button) findViewById(R.id.login);            //wrongly added
driverid = (EditText) findViewById(R.id.driverid);    // not added
Ashish John
  • 1,867
  • 2
  • 23
  • 38
0

Based on your code, the app will crash here: i.putExtra("driver_id", driverid.getText().toString()), resulting in a NullPointerException. It crashes because you don't have an initialisation for EditText driverid; in onCreate and thus, it will throw an exception when used uninitialised.

To fix the problem you have to update the field in onCreate:

driverid = (EditText) findViewById(driverid);
Community
  • 1
  • 1
Iulian Popescu
  • 2,595
  • 4
  • 23
  • 31