0

I can not understand why my application does not open a new activity on a button click.I have a button in my layout file(id is showButton),I need to open another activity when I click on that button.But it does not work and my application will crash.therefore I added a try catch block and get the android monitor result as following.

activity_place_picker.xml

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            android:paddingBottom="@dimen/activity_vertical_margin"
            tools:context=".PlacePickerActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView"
    android:layout_alignParentTop="true"
    android:layout_marginTop="20dp"
    android:layout_centerHorizontal="true"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView2"
    android:layout_below="@+id/textView"
    android:layout_centerHorizontal="true"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Launch Places API Picker"
    android:id="@+id/pickerButton"
    android:layout_below="@+id/textView2"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="50dp"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Add to Place Holder"
    android:id="@+id/saveButton"
    android:layout_below="@+id/pickerButton"
    android:layout_centerHorizontal="true"
    android:visibility="gone"
    android:layout_marginTop="20dp"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Place Holder"
    android:id="@+id/showButton"
    android:layout_below="@+id/saveButton"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView3"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"/>

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView3"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:id="@+id/poweredBy"
    android:src="@drawable/powered_by_google_light"/>

This is Place picker class where I have included the onClick listner to the button to open a new activity.

PlacePickerActivity

public class PlacePickerActivity extends AppCompatActivity {
    private static final int PLACE_PICKER_REQUEST = 1;
    private TextView mName;
    private TextView mAddress;
    private TextView mAttributions;
    private static final LatLngBounds BOUNDS_MOUNTAIN_VIEW = new LatLngBounds(
            new LatLng(37.398160, -122.180831), new LatLng(37.430610, -121.972090));
    Context ctx;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_place_picker);
    mName = (TextView) findViewById(R.id.textView);
    mAddress = (TextView) findViewById(R.id.textView2);
    mAttributions = (TextView) findViewById(R.id.textView3);
    Button showButton = (Button) findViewById(R.id.showButton);
    Button pickerButton = (Button) findViewById(R.id.pickerButton);

    pickerButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                PlacePicker.IntentBuilder intentBuilder =
                        new PlacePicker.IntentBuilder();
                intentBuilder.setLatLngBounds(BOUNDS_MOUNTAIN_VIEW);
                Intent intent = intentBuilder.build(PlacePickerActivity.this);
                startActivityForResult(intent, PLACE_PICKER_REQUEST);

            } catch (GooglePlayServicesRepairableException
                    | GooglePlayServicesNotAvailableException e) {
                e.printStackTrace();
            }
        }
    });


    showButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            try {
                Intent i = new Intent(ctx, PlacesActivity.class);
                ctx.startActivity(i);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    });

}



@Override
protected void onActivityResult(int requestCode,
                                int resultCode, Intent data) {

    if (requestCode == PLACE_PICKER_REQUEST
            && resultCode == Activity.RESULT_OK) {

        //Extracting place information from the API
        final Place place = PlacePicker.getPlace(this, data);
        final String place_Id = place.getId().toString();
        final String place_Name = place.getName().toString();
        final String place_Address = place.getAddress().toString();
        final String place_PhoneNumber = place.getPhoneNumber().toString();
        final String place_Website = place.getWebsiteUri().toString();
        //Get rating as a float value and converting to string
        final float rating = place.getRating();
        final String place_Rating = String.valueOf(rating);
        final String place_LatLng = place.getLatLng().toString();
        final String function_Save = "save_Place";
        final String function_Show = "show_Place";

        String attributions = (String) place.getAttributions();
        if (attributions == null) {
            attributions = "";
        }


        mName.setText(place_Name);
        mAddress.setText(place_Address);
        mAttributions.setText(Html.fromHtml(attributions));



        //Accessing the save button and show button in the view and making it visible after selecting a place
        final View save = findViewById(R.id.saveButton);
        save.setVisibility(View.VISIBLE);


        //Passing the Extracted place details to the background worker class
        save.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {

                BackgroundWorker backgroundWorker = new BackgroundWorker(PlacePickerActivity.this);
                backgroundWorker.execute(function_Save,place_Id,place_Name,place_Address,place_PhoneNumber,place_Website,place_Rating,place_LatLng);

            }
        });



    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

}

PlacesActivity.java

package com.truiton.placepicker;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class PlacesActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_places);
}

}

activity_places.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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: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="com.truiton.placepicker.PlacesActivity">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello world"
    android:id="@+id/textView" />

</RelativeLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.truiton.placepicker">

<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="AIzaSyBpIGvJfE8eKhoEFCMqV8NrFkWRjTAnNyQ" />

    <activity
        android:name=".PlacePickerActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".PlacesActivity"></activity>
</application>

</manifest>

Android Monitor result when the button clicks Android Monitor result

Edited : This is the crash log Crash log

can anyone help me with this?

2 Answers2

1

Write the class name PlacePickerActivity in the intent, your method should be

showButton.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v) {
        try {
            Intent i = new Intent(PlacePickerActivity.this , PlacesActivity.class);
            ctx.startActivity(i);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
});
AlexGuerra
  • 181
  • 7
0

I found the answer for the question. This link helped me to find the answer for the problem. In PlacePickerActivity I have just declare Context ctx;and it does not contain any primitive value. But I have use the it as an argument to open a new activity which will give me a NullPointerException.So instead of

showButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {

                Intent i = new Intent(ctx, PlacesActivity.class);
                ctx.startActivity(i);

        }
    });

I used

 showButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {

                Intent i = new Intent(PlacePickerActivity.this, PlacesActivity.class);
                PlacePickerActivity.this.startActivity(i);

        }
    });

And this fixed my problem.Thank you for every one.

Community
  • 1
  • 1