【问题标题】:I'm getting an error in my android app where it crashes after I press the button我在我的 android 应用程序中收到一个错误,在我按下按钮后它崩溃了
【发布时间】:2017-05-27 02:44:50
【问题描述】:

我尝试实施我遵循的修复但没有结果,我很确定这是我没有看到的小东西。这是来自 logcat 的错误

致命异常:主要 进程:com.example.ramos.nester,PID:11297 java.lang.IllegalStateException:无法在父级或祖先上下文中找到方法检索(MainActivity)(View)为 android:onClick 属性定义在视图类 android.support.v7.widget.AppCompatButton 与 id 'guibutt' 在 android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327) 在 android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284) 在 android.view.View.performClick(View.java:4492) 在 android.view.View$PerformClick.run(View.java:18568) 在 android.os.Handler.handleCallback(Handler.java:733) 在 android.os.Handler.dispatchMessage(Handler.java:95) 在 android.os.Looper.loop(Looper.java:136) 在 android.app.ActivityThread.main(ActivityThread.java:5021) 在 java.lang.reflect.Method.invokeNative(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:515) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643) 在 dalvik.system.NativeStart.main(Native Method)

代码

    package com.example.ramos.nester;

import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import okhttp3.*;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;



public class MainActivity extends AppCompatActivity {
    public void insert(View view) {
        // Add the code that you want
        // Or do nothing if you want
    }

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

    }

    public void retrieve(View v) {

        Button guibutt = (Button) findViewById(R.id.guibutt);
        TextView software = (TextView) findViewById(R.id.software);
        TextView name = (TextView) findViewById(R.id.name);
        TextView last = (TextView) findViewById(R.id.last);
        TextView device = (TextView) findViewById(R.id.device);
        TextView battery = (TextView) findViewById(R.id.battery);

        final String auth = "Bearer c.I1LDweqMS6Fdc3c2gLPcH8Z1W0B6dDVOXDQ9WYZYdyjoULoUubiB59cGn03mDPmXvaJv1i0Qg2H1mHA3QTmdwKOU9BVePV9MfQgKTcAuNnjddtTgIWR7Ngcz6d7ED7d14whkNuTAE3EC7uS4"; // Update with your token

        OkHttpClient client = new OkHttpClient.Builder()
                .authenticator(new Authenticator() {
                    @Override
                    public Request authenticate(Route route, Response response) throws IOException {
                        return response.request().newBuilder()
                                .header("Authorization", auth)
                                .build();
                    }
                })
                .followRedirects(true)
                .followSslRedirects(true)
                .build();

        Request request = new Request.Builder()
                .url("https://developer-api.nest.com")
                .get()
                .addHeader("content-type", "application/json; charset=UTF-8")
                .addHeader("authorization", auth)
                .build();
        try {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

            StrictMode.setThreadPolicy(policy);
            Response response = client.newCall(request).execute();

            JSONObject json = new JSONObject(response.body().string());
            JSONObject devices = json.getJSONObject("devices");
            JSONObject smoke_co_alarms = devices.getJSONObject("smoke_co_alarms");
            JSONObject alarm = smoke_co_alarms.getJSONObject("pBQ6G56_J-C-VMTeIri1Zl3BtVDjEvJ-");


            String softwaretexterino = alarm.getString("software_version");
            String nametexterino = alarm.getString("name");
            String lasttexterino = alarm.getString("last_connection");
            String devicetexterino = alarm.getString("device_id");
            String betterytexterino = alarm.getString("battery_health");

            software.setText(softwaretexterino);
            name.setText(nametexterino);
            last.setText(lasttexterino);
            device.setText(devicetexterino);
            battery.setText(betterytexterino);

            Thread.sleep(2 * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    View.OnClickListener myOnlyhandler = new View.OnClickListener() {
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.guibutt:
                    break;
            }
        }
    };
}

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:id="@+id/activity_main"
        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="com.example.nestapp.MainActivity">

    <Button
            android:text="Click Me For Information"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="36dp" android:id="@+id/guibutt" android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true" android:onClick="retrieve (MainActivity)"/>
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/software"
            android:layout_marginTop="45dp"
            android:text="Software Version" android:layout_below="@+id/last"
            android:layout_centerHorizontal="true"/>
    <TextView
            android:text="Device ID"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="45dp" android:id="@+id/device" android:layout_above="@+id/battery"
            android:layout_centerHorizontal="true"/>
    <TextView
            android:text="Name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="38dp" android:id="@+id/name"
            android:textAlignment="center"
            android:layout_centerInParent="false" android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"/>
    <TextView
            android:text="Battery Health"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="53dp" android:id="@+id/battery"
            android:layout_above="@+id/last" android:layout_centerHorizontal="true"/>
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/last"
            android:text="Last Time Period of Connection"
            android:layout_centerVertical="true" android:layout_centerHorizontal="true"/>
</RelativeLayout>

【问题讨论】:

  • 我可以查看您的此活动的布局 xml 文件吗?
  • 清单还是主要?
  • 我指的是 R.layout.activity_main。清单不是布局文件。您可以在 res/layout 文件夹中检查所有布局
  • 刚刚编辑过

标签: android intellij-idea


【解决方案1】:

我是新来的,我对 Android 的体验有些有限。但是,我认为如果不发布您的代码,就很难说出导致崩溃的原因(至少对于像我这样的人来说)。添加您的代码可能会激励其他人帮助您。

【讨论】:

    【解决方案2】:

    从我从您上面发布的代码中可以看出,由于 onClick 方法不可用而弹出此错误。我曾经在使用 View 类而不使用 onClick 方法的地方遇到过类似的问题。尝试添加 setOnClickListener 函数代替检索函数。它应该可以解决您的错误。这是利用 View 类和 OnClick 方法检测按钮点击的语法。

    btn = (Button)findViewById(R.id.firstButton);
    btn.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            //Code goes inside this function.
        }
    });
    

    如果这没有帮助,最好也发布活动的 XML 代码。

    【讨论】:

    • 我应该把这个放在哪里?我也再次重新编辑了代码,我发布了错误的代码版本-.-我这样做了 3 次
    • 您是否在 Android Manifest 文件中添加了 Internet 权限?
    • 而且我没有告诉你在检索函数中添加代码。我说过,如果你想通过点击按钮来做某事,你可以将检索函数与上面的函数交换。
    猜你喜欢
    • 2020-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-30
    • 1970-01-01
    • 2018-07-04
    • 2020-05-26
    • 1970-01-01
    相关资源
    最近更新 更多