java - 单击按钮时无法打开新 Activity

标签 java android android-activity

我不明白为什么我的应用程序在单击按钮时不打开新 Activity 。我的布局文件中有一个按钮(id 是 showButton),当我单击该按钮时,我需要打开另一个 Activity 。但是它不起作用,我的应用程序将崩溃。因此我添加了一个 try catch block 并获取 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"/>

这是地点选择器类,我已将 onClick 监听器包含到按钮中以打开新 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

谁能帮我解决这个问题吗?

最佳答案

在intent中写入类名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();
        }
    }
});

关于java - 单击按钮时无法打开新 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43003786/

相关文章:

java - 单例模式可以被内部类破坏吗?

java - Android:清除 Activity 堆栈

java - 通过 JAX-WS 使用 Web 服务

java - 如何使用 disruptor 实现自定义 actor 邮箱?

android - "Refresh Property Sheet"遇到了

java - Android CardView 动画不正确

android - 在videoview中播放youtube视频

android - 如何在包中存储稀疏数组

java - Android - 创建一个类的实例并从 String 调用

java - 如何使用 Spring MVC 从 POST 请求返回 XML 响应?