How popular is Alertdialog in Android Studio?

Asked

Viewed 334 times

3

I’m taking a shit here from Java and Android Studio.

  • I have already created an Activity + Dialogfragment, Location information does not pass from the Activity screen to Dialogfragment!

  • Now I’m trying to do a Alertdialog inside the Activity to see if I can use the latitude and longitude of the user’s gps. But by clicking on the button it error and closes the application.

How do I popular this information within this 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>

ERROR

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)

1 answer

0

Due to the error, some of your Textview was not found correctly by the respective findViewById in Java. You have to debug and see which one is (the one that returns null for the variable). Plus its layout has views with repeated ids, and some views are using alignment referencing other views with "@+id" (it should just be "@id", since + is only used to create a new id in the view hierarchy and assign it to a view) and this can also cause problems.

  • It’s okay to wear it all the time @+id, if the id already exists will not be created another, it is as if you used @id.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.