2016-01-31 2 views
0

Перед публикацией этого вопроса я много искал, чтобы узнать эту специфическую проблему.
Что я делаю, что я устанавливаю текст в TextView внутри метода обратного вызова.
Этот код отлично работает при первом запуске приложения, но в следующий раз он не обновляет TextView.
У меня есть только одна активность и внутри onBackPressed() Я закончил.
Ниже приведен код, который я написал для обновления значений объекта местоположения в четырех разных TextView.Метод setText TextView не устанавливает текст при первом запуске Activity

public class Home extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, LocationApi.LocationResult{ 

private static final String TAG = Home.class.getSimpleName(); 
private DrawerLayout drawer; 
private Toolbar toolbar; 
private NavigationView navigationView; 
private TextView txtLatitude; 
private TextView txtLongitude; 
private TextView txtAccuracy; 
private TextView txtProvider; 
private Context mContext; 
//private MyLocationApi locationApi; 
private LocationApi locationApi; 

private StringBuilder logcat; 
private int count =0; 
private MyLocationApi.MyLocationDataListener myLocationListener; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_home); 
    mContext = this; 
    toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); 
     } 
    }); 

    drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
      this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
    drawer.setDrawerListener(toggle); 
    toggle.syncState(); 

    navigationView = (NavigationView) findViewById(R.id.nav_view); 
    navigationView.setNavigationItemSelectedListener(this); 

    txtLatitude = (TextView)findViewById(R.id.val_lat); 
    txtLongitude = (TextView)findViewById(R.id.val_lng); 
    txtAccuracy = (TextView) findViewById(R.id.val_accuracy); 
    txtProvider = (TextView)findViewById(R.id.val_provider); 

    //Code to get location using fused api (GoogleClientApi) 
    locationApi = LocationApi.getInstance(mContext); 
    locationApi.connect(); 
    Log.d(TAG, "OnCreate is called"); 

} 
@Override 
public void onStart(){ 
    super.onStart(); 

    Log.d(TAG, "On Start is called "); 
} 

@Override 
public void onBackPressed() { 
    Log.d(TAG, "On Backpressed is called "); 

    if (drawer.isDrawerOpen(GravityCompat.START)) { 
     drawer.closeDrawer(GravityCompat.START); 
    } else { 
     if(locationApi != null){ 
      locationApi.disconnect(); 
     } 
     finish(); 
     //super.onBackPressed(); 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.home, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

@SuppressWarnings("StatementWithEmptyBody") 
@Override 
public boolean onNavigationItemSelected(MenuItem item) { 
    // Handle navigation view item clicks here. 
    int id = item.getItemId(); 

    if (id == R.id.nav_camera) { 
     // Handle the camera action 
    } else if (id == R.id.nav_gallery) { 

    } else if (id == R.id.nav_slideshow) { 

    } else if (id == R.id.nav_manage) { 

    } else if (id == R.id.nav_share) { 

    } else if (id == R.id.nav_send) { 

    } 

    //DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    drawer.closeDrawer(GravityCompat.START); 


    return true; 
} 

@Override 
public void gotLocation(Location _location) { 
    Log.d(TAG,"Got Location is called"); 

    Log.d(TAG,"Lat "+Double.toString(_location.getLatitude())); 
    Log.d(TAG,"Lng "+Double.toString(_location.getLongitude())); 
    Log.d(TAG,"Acc " + Double.toString(_location.getAccuracy())); 
    Log.d(TAG, "Prov " + _location.getProvider().toString()); 

    txtLatitude.setText(Double.toString(_location.getLatitude())); 
    txtLongitude.setText(Double.toString(_location.getLongitude())); 
    txtAccuracy.setText(Double.toString(_location.getAccuracy())); 
    txtProvider.setText(_location.getProvider().toString()); 
    //locationApi.disconnect(); 
} 

@Override 
public void locationNotAvailable() { 
    Log.d(TAG,"Location not available is called"); 
} 
} 

Logcat:

02-01 12:41:16.721 13865-13865/? D/dalvikvm: Late-enabling CheckJNI 
02-01 12:41:17.071 13865-13865/com.binarysoft.nearme I/dalvikvm: Could not find method android.app.Notification$Builder.setLocalOnly, referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zza 
02-01 12:41:17.071 13865-13865/com.binarysoft.nearme W/dalvikvm: VFY: unable to resolve virtual method 254: Landroid/app/Notification$Builder;.setLocalOnly (Z)Landroid/app/Notification$Builder; 
02-01 12:41:17.071 13865-13865/com.binarysoft.nearme D/dalvikvm: VFY: replacing opcode 0x6e at 0x00c8 
02-01 12:41:17.071 13865-13865/com.binarysoft.nearme I/dalvikvm: DexOpt: access denied from Lcom/google/android/gms/common/GooglePlayServicesUtil; to field Landroid/app/Notification;.extras 
02-01 12:41:17.071 13865-13865/com.binarysoft.nearme W/dalvikvm: VFY: unable to resolve instance field 18 
02-01 12:41:17.071 13865-13865/com.binarysoft.nearme D/dalvikvm: VFY: replacing opcode 0x54 at 0x00e7 
02-01 12:41:17.081 13865-13865/com.binarysoft.nearme I/dalvikvm: Could not find method android.os.UserManager.getApplicationRestrictions, referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zzah 
02-01 12:41:17.081 13865-13865/com.binarysoft.nearme W/dalvikvm: VFY: unable to resolve virtual method 1595: Landroid/os/UserManager;.getApplicationRestrictions (Ljava/lang/String;)Landroid/os/Bundle; 
02-01 12:41:17.081 13865-13865/com.binarysoft.nearme D/dalvikvm: VFY: replacing opcode 0x6e at 0x0012 
02-01 12:41:17.081 13865-13865/com.binarysoft.nearme E/dalvikvm: Could not find class 'android.app.AppOpsManager', referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zzb 
02-01 12:41:17.081 13865-13865/com.binarysoft.nearme W/dalvikvm: VFY: unable to resolve check-cast 27 (Landroid/app/AppOpsManager;) in Lcom/google/android/gms/common/GooglePlayServicesUtil; 
02-01 12:41:17.081 13865-13865/com.binarysoft.nearme D/dalvikvm: VFY: replacing opcode 0x1f at 0x000e 
02-01 12:41:17.081 13865-13865/com.binarysoft.nearme I/dalvikvm: Could not find method android.content.pm.PackageManager.getPackageInstaller, referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zzj 
02-01 12:41:17.081 13865-13865/com.binarysoft.nearme W/dalvikvm: VFY: unable to resolve virtual method 549: Landroid/content/pm/PackageManager;.getPackageInstaller()Landroid/content/pm/PackageInstaller; 
02-01 12:41:17.081 13865-13865/com.binarysoft.nearme D/dalvikvm: VFY: replacing opcode 0x6e at 0x000b 
02-01 12:41:17.121 13865-13888/com.binarysoft.nearme I/GMPM: App measurement is starting up 
02-01 12:41:17.131 13865-13888/com.binarysoft.nearme E/GMPM: getGoogleAppId failed with status: 10 
02-01 12:41:17.141 13865-13888/com.binarysoft.nearme E/GMPM: Uploading is not possible. App measurement disabled 
02-01 12:41:17.291 13865-13865/com.binarysoft.nearme W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;) 
02-01 12:41:17.291 13865-13865/com.binarysoft.nearme I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested 
02-01 12:41:17.291 13865-13865/com.binarysoft.nearme W/dalvikvm: VFY: unable to resolve interface method 19485: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z 
02-01 12:41:17.291 13865-13865/com.binarysoft.nearme D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002 
02-01 12:41:17.291 13865-13865/com.binarysoft.nearme I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode 
02-01 12:41:17.291 13865-13865/com.binarysoft.nearme W/dalvikvm: VFY: unable to resolve interface method 19489: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode; 
02-01 12:41:17.291 13865-13865/com.binarysoft.nearme D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002 
02-01 12:41:17.672 13865-13865/com.binarysoft.nearme I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations 
02-01 12:41:17.672 13865-13865/com.binarysoft.nearme W/dalvikvm: VFY: unable to resolve virtual method 627: Landroid/content/res/TypedArray;.getChangingConfigurations()I 
02-01 12:41:17.672 13865-13865/com.binarysoft.nearme D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002 
02-01 12:41:17.672 13865-13865/com.binarysoft.nearme I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType 
02-01 12:41:17.672 13865-13865/com.binarysoft.nearme W/dalvikvm: VFY: unable to resolve virtual method 649: Landroid/content/res/TypedArray;.getType (I)I 
02-01 12:41:17.672 13865-13865/com.binarysoft.nearme D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002 
02-01 12:41:17.682 13865-13865/com.binarysoft.nearme I/dalvikvm: Could not find method android.widget.LinearLayout$LayoutParams.<init>, referenced from method android.support.design.widget.AppBarLayout$LayoutParams.<init> 
02-01 12:41:17.682 13865-13865/com.binarysoft.nearme W/dalvikvm: VFY: unable to resolve direct method 20100: Landroid/widget/LinearLayout$LayoutParams;.<init> (Landroid/widget/LinearLayout$LayoutParams;)V 
02-01 12:41:17.682 13865-13865/com.binarysoft.nearme D/dalvikvm: VFY: replacing opcode 0x70 at 0x0000 
02-01 12:41:17.682 13865-13865/com.binarysoft.nearme I/dalvikvm: Could not find method android.widget.LinearLayout$LayoutParams.<init>, referenced from method android.support.design.widget.AppBarLayout$LayoutParams.<init> 
02-01 12:41:17.682 13865-13865/com.binarysoft.nearme W/dalvikvm: VFY: unable to resolve direct method 20100: Landroid/widget/LinearLayout$LayoutParams;.<init> (Landroid/widget/LinearLayout$LayoutParams;)V 
02-01 12:41:17.682 13865-13865/com.binarysoft.nearme D/dalvikvm: VFY: replacing opcode 0x70 at 0x0000 
02-01 12:41:18.042 13865-13865/com.binarysoft.nearme D/Home: OnCreate is called 
02-01 12:41:18.042 13865-13865/com.binarysoft.nearme D/Home: On Start is called 
02-01 12:41:18.062 13865-13869/com.binarysoft.nearme D/dalvikvm: GC_CONCURRENT freed 514K, 14% free 8945K/10352K, paused 2ms+2ms, total 31ms 
02-01 12:41:18.713 13865-13865/com.binarysoft.nearme D/libEGL: loaded /system/lib/egl/libGLES_hawaii.so 
02-01 12:41:18.733 13865-13865/com.binarysoft.nearme D/ION: config: version(0x10000) secure(0xf000) 256M(0x22d) fast(0x608) hwwr(0x608) 
02-01 12:41:18.733 13865-13865/com.binarysoft.nearme D/MM_DEVICE: Waiting for mm thread to come up 
02-01 12:41:18.733 13865-13916/com.binarysoft.nearme D/MM_DEVICE: mm_device_thread starting 
02-01 12:41:18.743 13865-13865/com.binarysoft.nearme D/HAWAII_EGL: eglCreateContext() config: 18 context: 0x51a12fc0, VC context 1, Thread 13865 
02-01 12:41:18.743 13865-13865/com.binarysoft.nearme D/HAWAII_EGL: Set SWAP INTERVAL 0 
02-01 12:41:18.753 13865-13865/com.binarysoft.nearme D/HAWAII_EGL: eglCreateWindowSurface() surface: 0x4fd89720, VC surface: 1, Thread: 13865 
02-01 12:41:18.753 13865-13865/com.binarysoft.nearme D/HAWAII_EGL: eglMakeCurrent(0x51a12fc0, 0x4fd89720, 0x4fd89720) Thread: 13865 
02-01 12:41:18.763 13865-13865/com.binarysoft.nearme D/OpenGLRenderer: Enabling debug mode 0 
02-01 12:41:18.773 13865-13865/com.binarysoft.nearme I/dalvikvm: Could not find method android.support.v7.widget.LinearLayoutCompat.drawableHotspotChanged, referenced from method android.support.design.internal.ForegroundLinearLayout.drawableHotspotChanged 
02-01 12:41:18.773 13865-13865/com.binarysoft.nearme W/dalvikvm: VFY: unable to resolve virtual method 16146: Landroid/support/v7/widget/LinearLayoutCompat;.drawableHotspotChanged (FF)V 
02-01 12:41:18.773 13865-13865/com.binarysoft.nearme D/dalvikvm: VFY: replacing opcode 0x6f at 0x0000 
02-01 12:41:19.153 13865-13865/com.binarysoft.nearme D/WritingBuddyImpl: getCurrentWritingBuddyView() 
02-01 12:41:19.233 13865-13865/com.binarysoft.nearme W/IInputConnectionWrapper: showStatusIcon on inactive InputConnection 
02-01 12:41:19.243 13865-13865/com.binarysoft.nearme I/LocationApi: OnConnected :: 
02-01 12:41:19.313 13865-13865/com.binarysoft.nearme I/LocationApi: OnConnected :: Last Known Location: 77.0630103 , 28.4594539 
02-01 12:41:19.313 13865-13865/com.binarysoft.nearme D/Home: Connected is called 
02-01 12:41:19.614 13865-13865/com.binarysoft.nearme I/LocationApi: OnLocationChanged Called 1 timesNew Lat: 28.4594519 New Long: 77.06301 
02-01 12:41:20.575 13865-13865/com.binarysoft.nearme D/Home: On Start is called 
02-01 12:41:20.925 13865-13865/com.binarysoft.nearme I/LocationApi: OnLocationChanged Called 2 timesNew Lat: 28.4594525 New Long: 77.0630112 
02-01 12:41:24.318 13865-13865/com.binarysoft.nearme D/Home: Got Location is called 
02-01 12:41:24.318 13865-13865/com.binarysoft.nearme D/Home: Lat 28.4594525 
02-01 12:41:24.318 13865-13865/com.binarysoft.nearme D/Home: Lng 77.0630112 
02-01 12:41:24.318 13865-13865/com.binarysoft.nearme D/Home: Acc 24.0 
02-01 12:41:24.318 13865-13865/com.binarysoft.nearme D/Home: Prov fused 
02-01 12:41:24.498 13865-13865/com.binarysoft.nearme D/LocationApi: Update Timeout 
02-01 12:41:59.482 13865-13865/com.binarysoft.nearme D/Home: On Start is called 
02-01 12:41:59.683 13865-13865/com.binarysoft.nearme D/WritingBuddyImpl: getCurrentWritingBuddyView() 
02-01 12:42:01.134 13865-13865/com.binarysoft.nearme D/Home: On Backpressed is called 
02-01 12:42:01.454 13865-13865/com.binarysoft.nearme D/HAWAII_EGL: eglMakeCurrent(NULL) Thread: 13865 
02-01 12:42:01.454 13865-13865/com.binarysoft.nearme D/HAWAII_EGL: eglDestroySurface() surface: 0x4fd89720, android window 0x4fd813e0, Thread: 13865 
02-01 12:42:01.865 13865-13865/com.binarysoft.nearme D/HAWAII_EGL: eglMakeCurrent(0x51a12fc0, 0x53fa3158, 0x53fa3158) Thread: 13865 
02-01 12:42:01.865 13865-13865/com.binarysoft.nearme D/HAWAII_EGL: eglMakeCurrent(NULL) Thread: 13865 
02-01 12:42:01.885 13865-13865/com.binarysoft.nearme D/HAWAII_EGL: eglMakeCurrent(0x51a12fc0, 0x53fa3158, 0x53fa3158) Thread: 13865 
02-01 12:42:01.885 13865-13865/com.binarysoft.nearme D/HAWAII_EGL: eglMakeCurrent(NULL) Thread: 13865 
02-01 12:42:13.286 13865-13869/com.binarysoft.nearme D/dalvikvm: GC_CONCURRENT freed 256K, 12% free 9188K/10352K, paused 24ms+6ms, total 160ms 
02-01 12:42:13.296 13865-13865/com.binarysoft.nearme D/Home: OnCreate is called 
02-01 12:42:13.296 13865-13865/com.binarysoft.nearme D/Home: On Start is called 
02-01 12:42:13.396 13865-13865/com.binarysoft.nearme D/HAWAII_EGL: Set SWAP INTERVAL 0 
02-01 12:42:13.396 13865-13865/com.binarysoft.nearme D/HAWAII_EGL: eglCreateWindowSurface() surface: 0x53f77070, VC surface: 3, Thread: 13865 
02-01 12:42:13.396 13865-13865/com.binarysoft.nearme D/HAWAII_EGL: eglMakeCurrent(0x51a12fc0, 0x53f77070, 0x53f77070) Thread: 13865 
02-01 12:42:13.516 13865-13865/com.binarysoft.nearme D/WritingBuddyImpl: getCurrentWritingBuddyView() 
02-01 12:42:13.616 13865-13865/com.binarysoft.nearme I/LocationApi: OnConnected :: 
02-01 12:42:13.656 13865-13865/com.binarysoft.nearme I/LocationApi: OnConnected :: Last Known Location: 77.0630112 , 28.4594525 
02-01 12:42:13.656 13865-13865/com.binarysoft.nearme D/Home: Connected is called 
02-01 12:42:13.726 13865-13865/com.binarysoft.nearme I/LocationApi: OnLocationChanged Called 1 timesNew Lat: 28.4594525 New Long: 77.0630112 
02-01 12:42:14.817 13865-13865/com.binarysoft.nearme I/LocationApi: OnLocationChanged Called 2 timesNew Lat: 28.4594525 New Long: 77.0630112 
02-01 12:42:18.651 13865-13865/com.binarysoft.nearme D/Home: Got Location is called 
02-01 12:42:18.651 13865-13865/com.binarysoft.nearme D/Home: Lat 28.4594525 
02-01 12:42:18.651 13865-13865/com.binarysoft.nearme D/Home: Lng 77.0630112 
02-01 12:42:18.651 13865-13865/com.binarysoft.nearme D/Home: Acc 24.0 
02-01 12:42:18.651 13865-13865/com.binarysoft.nearme D/Home: Prov fused 
02-01 12:42:18.661 13865-13865/com.binarysoft.nearme D/LocationApi: Update Timeout 
02-01 12:42:44.617 13865-13865/com.binarysoft.nearme D/Home: On Start is called 

============= gotLocation() является методом, где я получаю значение от объекта определения местоположения, но это делает не отображаются в TextView.

Любая помощь будет высоко оценена.

+1

вы действительно думаете, этот метод ('gotLocation') увольняют? – Vyacheslav

+0

Да, уволен? Вы видите кошку? Да? то это нулевое значение или имеет какое-то значение? – Ritesh

+1

В вашем посте нет никакого ловушка. –

ответ

0

editText метод не в методе oncreate.

@Override public void gotLocation(Location _location) { Log.d(TAG,"Got Location is called"); Log.d(TAG,"Lat "+Double.toString(_location.getLatitude())); Log.d(TAG,"Lng "+Double.toString(_location.getLongitude())); Log.d(TAG,"Acc " + Double.toString(_location.getAccuracy())); Log.d(TAG, "Prov " + _location.getProvider().toString()); txtLatitude.setText(Double.toString(_location.getLatitude())); txtLongitude.setText(Double.toString(_location.getLongitude())); txtAccuracy.setText(Double.toString(_location.getAccuracy())); txtProvider.setText(_location.getProvider().toString()); //locationApi.disconnect(); } 

Вы можете использовать метод воссоздавать, нажав Ctrl + O

+0

Извините, я не понял вашу точку зрения. Существует не editText. Я объявил TextView на уровне модуля. Инициализировали их на «OnCreate». и использовать их внутри метода обратного вызова. – Ashish

+0

@Ashish Извините, это моя ошибка –

Смежные вопросы