2017-01-17 8 views
0

Android Studio와 관련하여 매우 새로운 기능이지만 별도의 앱에서 잘 작동하는 2 가지 활동을 통합했지만 MainActivityIntent을 호출하면 충돌이 발생했습니다. 및 startActivity.모바일에서 테스트 할 때 오류가없는 코드가 충돌합니다.

또 다른 문제가있을 수도 있고 그렇지 않을 수도 있습니다. 앱을 설치할 때 요청 된 권한이 없습니다. 여기

내 코드입니다 :

의 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> 
+0

을, 당신은 사용자의 위치에 액세스하는 응용 프로그램에 대한 권한을 허용해야 할 수 있습니다. 그렇게 해보고 그것이 어떻게 진행되는지보십시오. – WQYeo

+0

안녕하세요, 귀하의 의견에 감사드립니다. 나는 믿는다. 은 AndoidManifest.xml에있다. 그게 옳지 않은가? – Ben

+0

그래야합니다.하지만 여기에 대한 자세한 내용도 있습니다. https://developer.android.com/training/permissions/requesting.html – WQYeo

답변

0

코드에 따라 앱이 여기에 충돌합니다 (i.putExtra("driver_id", driverid.getText().toString())). 그 결과는 NullPointerException이됩니다. EditText driverid;에 대한 초기화가 onCreate에 없으므로 충돌합니다. 따라서 초기화되지 않은 상태로 사용될 때 예외가 발생합니다.

당신이 onCreate의 필드를 업데이트해야 문제를 해결하려면 : 앱이 사용자의 현재 위치에 액세스하는 경우

driverid = (EditText) findViewById(driverid); 
+0

환상적입니다. Lulian 문제 해결에 대단히 감사합니다. – Ben

0

, 당신은 이러한 종속성

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

&

를 사용해야합니다 android.support.design.widget.TextInputLayout 을 사용하면 수정하시기 바랍니다

btnOk = (Button) findViewById(R.id.login);   //wrongly added 
driverid = (EditText) findViewById(R.id.driverid); // not added 
+0

좋습니다. 대단히 감사합니다. Ashish – Ben