按钮单击时不会打开新活动

时间:2017-03-24 15:45:08

标签: java android android-activity

我无法理解为什么我的应用程序没有在按钮点击上打开新活动。我的布局文件中有一个按钮(id是showButton),当我点击该按钮时我需要打开另一个活动。但是它不起作用,我的应用程序将崩溃。因此我添加了一个try catch块并获得如下的android监视器结果。

  

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"/>

这是Place picker类,我已将onClick listner包含在按钮中以打开新活动。

  

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监视器结果   Android Monitor result

     

已编辑:这是崩溃日志   Crash log

任何人都可以帮我这个吗?

2 个答案:

答案 0 :(得分:1)

在意图中写出类名PlacePickerActivity,您的方法应该是

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();
        }
    }
});

答案 1 :(得分:0)

我找到了问题的答案。 This 链接帮助我找到问题的答案。在PlacePickerActivity中,我刚刚声明了Context ctx;,它不包含任何原始值。但是我用它作为参数来打开一个新的活动,它会给我一个NullPointerException.So而不是

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

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

        }
    });

我用过

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

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

        }
    });

这解决了我的问题。感谢每一个人。