首页 文章

Button btnGet无法正常工作,我该怎么办[复制]

提问于
浏览
-3

这个问题在这里已有答案:

ProfileActivity.java;

    import android.Manifest;
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.Build;
    import android.os.Bundle;
    import android.provider.Settings;
    import android.support.v4.app.ActivityCompat;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class ProfileActivity extends AppCompatActivity implements View.OnClickListener {

//firebase auth object
  private FirebaseAuth firebaseAuth;

//view objects
    public TextView textViewUserEmail;
    private Button buttonLogout,btnGet;
    public EditText edtxtLati, edtxtLongi;
    private LocationManager locationManager;
    private LocationListener locationListener;
    private Double lati, longi;


@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);


        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                lati = location.getLatitude();
                edtxtLati.append("" + lati);
                longi = location.getLongitude();
                edtxtLongi.append("" + longi);
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {

            }

            @Override
            public void onProviderEnabled(String provider) {

            }

            @Override
            public void onProviderDisabled(String provider) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);
            }
        };
        //VERSION AND PERMISSION CHECKING
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
            {

                requestPermissions(new String[]{
                        Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.INTERNET}, 10);
                return;
            }
        } else {
            configureButton();
        }

        //initializing firebase authentication object
        firebaseAuth = FirebaseAuth.getInstance();

        //if the user is not logged in
        //that means current user will return null
        if (firebaseAuth.getCurrentUser() == null) {
            //closing this activity
            finish();
            //starting login activity
            startActivity(new Intent(this, LoginActivity.class));
        }

        //getting current user
        FirebaseUser user = firebaseAuth.getCurrentUser();

        //initializing views
        textViewUserEmail = (TextView) findViewById(R.id.textViewUserEmail);
        buttonLogout = (Button) findViewById(R.id.buttonLogout);
        edtxtLati = (EditText)findViewById(R.id.edtxtLati);
        edtxtLongi = (EditText)findViewById(R.id.edtxtLongi);
        btnGet = (Button)findViewById(R.id.btnGet);

        //displaying logged in user name
        textViewUserEmail.setText("Welcome "+user.getEmail());

        //adding listener to button
        buttonLogout.setOnClickListener(this);
    }

    public void onRequestPermissionsResult(int requestCode, String[] permission, int[] grantResults) {
        switch(requestCode)
        {
            case 10:
                if (grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED )
                    configureButton();
        }

    }

    private void configureButton() {
        btnGet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                locationManager.requestLocationUpdates("gps", 5000, 0, locationListener);
            }

        });

    }


    @Override
    public void onClick(View view) {
        //if logout is pressed
        if (view == buttonLogout) {
            //logging out the user
            firebaseAuth.signOut();
            //closing activity
            finish();
            //starting login activity
            startActivity(new Intent(this, LoginActivity.class));
        }
    }
}

/ AndroidRuntime:FATAL EXCEPTION:main进程:com.praful.lochub,PID:3742 java.lang.RuntimeException:传递结果失败ResultInfo {who = @android:requestPermissions:,request = 10,result = -1,data = Intent {act = android.content.pm.action.REQUEST_PERMISSIONS(has extras)}} to activity {com.praful.lochub / com.praful.lochub.ProfileActivity}:java.lang.NullPointerException:尝试调用虚方法'void android .widget.Button.setOnClickListener(android.view.View $ OnClickListener)'在android.app.ActivityThread.deleverResults(ActivityThread.java:4053)上的空对象引用,位于android.app.ActivityThread.handleSendResult(ActivityThread.java:4096 )在Android.A.运行时android.app.Handler.dispatchMessage(Handler.java:102)的android.app.A活动线程中的android.app.ActivityThread.-wrap20(ActivityThread.java)处于android.app.A活动线程中的.H.handleMessage(ActivityThread.java:1516) . os.Looper.loop(Looper.java:154)在android.app.ActivityThread.main(ActivityThread.java:6077)的java.lang.reflect.Method.invoke(Native方法)com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)引起:java.lang.NullPointerException :尝试在com的com.praful.lochub.ProfileActivity.configureButton(ProfileActivity.java:121)上的空对象引用上调用虚拟方法'void android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)' . Android.app.ActivityThread上的android.app.Activity.dispatchAerseResult(Activity.java:6921)上的android.app.Activity.dispatchRequestPermissionsResult(Activity.java:7069)中的praful.lochub.ProfileActivity.onRequestPermissionsResult(ProfileActivity.java:115) .deliverResults(ActivityThread.java:4049)在android.app.ActivityThread.handleSendResult(ActivityThread.java:4096)android.app.ActivityThread.-wrap20(ActivityThread.java)android.app.ActivityThread $ H.handleMessage(ActivityThread) .java:1516)在android.os.L上的android.os.Handler.dispatchMessage(Handler.java:102)位于android.app.ActivityThread.main(ActivityThread.java:6077)的ooper.loop(Looper.java:154)位于com.android.internal.os.ZygoteInit $的java.lang.reflect.Method.invoke(Native Method)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)上的MethodAndArgsCaller.run(ZygoteInit.java:865)应用程序终止 .

activity_profile.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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.praful.lochub.ProfileActivity"
    android:background="#ffffff">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/textViewUserEmail"
        android:layout_gravity="center_horizontal"
        android:fontFamily="casual"
        android:textColor="@color/colorAccent"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Logout"
        android:id="@+id/buttonLogout"
        android:layout_gravity="center_horizontal"
        android:fontFamily="casual"
        android:textColor="@color/common_google_signin_btn_text_dark_default"
        android:textStyle="normal|bold"
        android:textAllCaps="false"
        android:textSize="18sp"
        android:background="@color/colorAccent"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_alignBottom="@+id/textViewUserEmail" />


    <Button
        android:text="Get Location"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnGet"
        style="@style/Widget.AppCompat.Button.Colored"
        android:fontFamily="casual"
        android:textStyle="normal|bold"
        android:textAllCaps="false"
        android:textSize="16sp"
        android:layout_below="@+id/edtxtLongi"
        android:layout_centerHorizontal="true" />

    <ImageButton
        android:background="@drawable/showonmap"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:layout_below="@+id/edtxtLongi"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="12dp"
        android:id="@+id/imgbtnMap" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/edtxtLati"
        android:hint="Latitude"
        android:textAlignment="center"
        android:fontFamily="casual"
        android:layout_marginTop="15dp"
        android:layout_below="@+id/textViewUserEmail"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/edtxtLongi"
        android:fontFamily="casual"
        android:hint="Longitude"
        android:textAlignment="center"
        android:layout_below="@+id/edtxtLati"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/loc"
        android:id="@+id/imgbtnLocate"
        android:background="@null"
        android:layout_marginBottom="22dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

1 回答

  • 3

    放置初始化:

    //initializing views
    textViewUserEmail = (TextView) findViewById(R.id.textViewUserEmail);
    buttonLogout = (Button) findViewById(R.id.buttonLogout);
    edtxtLati = (EditText)findViewById(R.id.edtxtLati);
    edtxtLongi = (EditText)findViewById(R.id.edtxtLongi);
    btnGet = (Button)findViewById(R.id.btnGet);
    

    紧随onCreate方法的顶部

    setContentView(R.layout.activity_profile);
    

    您的configureButton()方法是问题,因为它在初始化之前使用btnGet . 您可以从日志的这一行看到:

    reference at com.praful.lochub.ProfileActivity.configureButton(ProfileActivity.java:121)
    

相关问题