我尝试实施我所遵循的修复程序,但没有结果,我很确定它是一些小的,我只是没有看到。这是logcat的错误
致命的例外:主要 处理:com.example.ramos.nester,PID:11297 java.lang.IllegalStateException:找不到父或祖先的方法检索(MainActivity)(View)上下文中的android:onClick属性在视图类android.support.v7.widget.AppCompatButton上定义,id为#guffutt&#39 ; 在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) at 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(本地方法)
代码
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>
答案 0 :(得分:0)
我是新来的,我对Android的体验有限。但是,我认为如果不发布你的代码就更难分辨导致崩溃的原因(至少对于像我这样的人来说)。添加代码可能会激励其他人帮助您。
答案 1 :(得分:0)
从我上面发布的代码中可以看出,由于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代码会更好。