Estou tomando uma porrada aqui do Java e do Android Studio.
Eu já criei uma Activity + DialogFragment, as informações de location não passam da tela Activity para o DialogFragment!
Agora estou tentando fazer um AlertDialog dentro da Activity para ver se eu consigo utilizar a latitude e longitude do gps do usuário. Porém ao clicar no botão ele da erro e fecha o aplicativo.
Como faço para popular essas informações dentro deste alert dialog?
AboutScreen.java
/*===============================================================================
Copyright (c) 2016 PTC Inc. All Rights Reserved.
Copyright (c) 2012-2014 Qualcomm Connected Experiences, Inc. All Rights Reserved.
Vuforia is a trademark of PTC Inc., registered in the United States and other
countries.
===============================================================================*/
package com.vuforia.samples.Books.ui.ActivityList;
import com.vuforia.samples.Books.R;
import com.vuforia.samples.Books.app.Books.Books;
import com.vuforia.samples.Books.app.Neoris.Shopping;
import android.Manifest;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class AboutScreen extends FragmentActivity {
private Button mStartButton;
private String mClassToLaunch;
private String mClassToLaunchPackage;
private TextView txtShoppingName;
private TextView txtShoppingCity;
private TextView txtShoppingLat;
private TextView txtShoppingLong;
private Location location;
private LocationManager locationManager;
private Address endereco;
private double latitude;
private double longitude;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.about_screen);
Bundle extras = getIntent().getExtras();
mClassToLaunchPackage = getPackageName();
mClassToLaunch = mClassToLaunchPackage + "." + extras.getString("ACTIVITY_TO_LAUNCH");
/**
* VERIFICAÇÃO DA PERMISSÃO DO USUÁRIO AO USO DO GPS
*/
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
} else {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
if (location !=null){
latitude = location.getLatitude();
longitude = location.getLongitude();
}
txtShoppingName = (TextView) findViewById(R.id.txtShoppingName);
txtShoppingCity = (TextView) findViewById(R.id.txtShoppingCity);
txtShoppingLat = (TextView) findViewById(R.id.txtShoppingLat);
txtShoppingLong = (TextView) findViewById(R.id.txtShoppingLong);
mStartButton = (Button) findViewById(R.id.button_start);
mStartButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Cria um Layout para o DialogFragment.
AlertDialog.Builder mBuilder = new AlertDialog.Builder(AboutScreen.this);
AboutScreen abs = new AboutScreen();
View mView = getLayoutInflater().inflate(R.layout.fragment_dialogo, null);
ImageView mImageTitle = (ImageView) mView.findViewById(R.id.imageTitle);
TextView mShoppingName = (TextView) mView.findViewById(R.id.shoppingName);
TextView mShoppingCity = (TextView) mView.findViewById(R.id.shoppingCity);
Button mComecarButton = (Button) mView.findViewById(R.id.btn_comecar);
//Insere as Informações ao Dialogo.
txtShoppingLat.setText(" " + latitude);
txtShoppingLong.setText(" " + longitude);
//Abre a Activity Books.
mComecarButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(AboutScreen.this, Books.class);
startActivity(i);
}
});
mBuilder.setView(mView);
AlertDialog dialog = mBuilder.create();
dialog.show();
}
});
}
}
AlertFragmentLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:gravity="center"
android:padding="10dp"
android:orientation="vertical">
<ImageView
android:id="@+id/backgroundImageTitle"
android:layout_width="match_parent"
android:background="@color/blue_aliansce"
android:layout_height="100dp"
android:gravity="center_horizontal|center_vertical" />
<ImageView
android:id="@+id/imageTitle"
android:layout_width="50dp"
android:layout_height="100dp"
android:src="@drawable/world_location"
android:gravity="center_horizontal|center_vertical"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/shoppingName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:text="SHOPPING:"
android:textSize="20dp"
android:layout_marginTop="14dp"
android:layout_below="@+id/backgroundImageTitle"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp" />
<TextView
android:id="@+id/shoppingCity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:text="CIDADE:"
android:textSize="20dp"
android:layout_marginTop="8dp"
android:layout_below="@+id/shoppingName"
android:layout_alignLeft="@+id/shoppingName"
android:layout_alignStart="@+id/shoppingName" />
<TextView
android:id="@+id/shoppingLat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:text="LATITUDE:"
android:textSize="20dp"
android:layout_marginTop="10dp"
android:layout_below="@+id/shoppingCity"
android:layout_alignLeft="@+id/shoppingCity"
android:layout_alignStart="@+id/shoppingCity" />
<TextView
android:id="@+id/shoppingLong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:text="LONGITUDE:"
android:textSize="20dp"
android:layout_marginTop="10dp"
android:layout_below="@+id/shoppingLat"
android:layout_alignLeft="@+id/shoppingLat"
android:layout_alignStart="@+id/shoppingLat" />
<Button
android:id="@+id/btn_comecar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/green_aliansce"
android:gravity="center_horizontal|center_vertical"
android:textSize="@dimen/menu_title"
android:textColor="@color/white"
android:fontFamily="sans-serif"
android:text="@string/btn_comecar"
android:layout_below="@+id/shoppingLong"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="27dp" />
<TextView
android:id="@+id/txtShoppingName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/shoppingName"
android:layout_alignLeft="@+id/imageTitle"
android:layout_alignStart="@+id/imageTitle"
android:text="TextView"
android:layout_alignTop="@+id/shoppingName" />
<TextView
android:id="@+id/txtShoppingCity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/shoppingLat"
android:layout_alignLeft="@+id/txtShoppingName"
android:layout_alignStart="@+id/txtShoppingName"
android:text="TextView"
android:layout_alignTop="@+id/shoppingCity" />
<TextView
android:id="@+id/txtShoppingLat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/shoppingLong"
android:layout_alignLeft="@+id/txtShoppingCity"
android:layout_alignStart="@+id/txtShoppingCity"
android:text="TextView"
android:layout_alignTop="@+id/shoppingLat" />
<TextView
android:id="@+id/txtShoppingLong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txtShoppingLat"
android:layout_alignStart="@+id/txtShoppingLat"
android:layout_alignTop="@+id/shoppingLong"
android:text="TextView"
android:layout_above="@+id/btn_comecar" />
</RelativeLayout>
ERRO
06-14 15:12:17.818 11356-11356/com.vuforia.samples.Books E/AndroidRuntime: FATAL EXCEPTION: main Process: com.vuforia.samples.Books, PID: 11356 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.vuforia.samples.Books.ui.ActivityList.AboutScreen$1.onClick(AboutScreen.java:104) at android.view.View.performClick(View.java:5340) at android.view.View$PerformClick.run(View.java:21610) at android.os.Handler.handleCallback(Handler.java:815) at android.os.Handler.dispatchMessage(Handler.java:104) at android.os.Looper.loop(Looper.java:207) at android.app.ActivityThread.main(ActivityThread.java:5776) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)