gpt4 book ai didi

java - android:onclick 方法不起作用

转载 作者:行者123 更新时间:2023-12-02 03:41:01 25 4
gpt4 key购买 nike

xml代码:

 <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Invia"
android:clickable="true"
android:onClick="onClick1"
android:id="@+id/invia" />

java代码:

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class CreazioneSondaggio extends AppCompatActivity {

String URL;
private EditText titolo;
private EditText sceltaA;
private EditText sceltaB;
private EditText sceltaC;
private Button invio;
private EditText server;
@Override
protected void onCreate(Bundle savedInstanceState) {
titolo = (EditText) findViewById(R.id.titolo);
sceltaA = (EditText) findViewById(R.id.A);
sceltaB = (EditText) findViewById(R.id.B);
sceltaC = (EditText) findViewById(R.id.C);
server= (EditText) findViewById(R.id.server);
invio = (Button) findViewById(R.id.invia);

// invio.setOnClickListener(this);

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_creazione_sondaggio);
}

private void inviodati() {
......
}

public void onClick1(View v) {
Toast.makeText(CreazioneSondaggio.this, "ok", Toast.LENGTH_LONG).show();

inviodati();



}


}

错误:

Process: com.example.rober.registrazione, PID: 21261 java.lang.IllegalStateException: Could not execute method for android:onClick at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:275) at android.view.View.performClick(View.java:5210) at android.view.View$PerformClick.run(View.java:21288) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5527) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270) at android.view.View.performClick(View.java:5210)  at android.view.View$PerformClick.run(View.java:21288)  at android.os.Handler.handleCallback(Handler.java:739)  at android.os.Handler.dispatchMessage(Handler.java:95)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5527)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference at com.example.rober.registrazione.CreazioneSondaggio.inviodati(CreazioneSondaggio.java:48) at com.example.rober.registrazione.CreazioneSondaggio.onClick1(CreazioneSondaggio.java:87) at java.lang.reflect.Method.invoke(Native Method)  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)  at android.view.View.performClick(View.java:5210)  at android.view.View$PerformClick.run(View.java:21288)  at android.os.Handler.handleCallback(Handler.java:739)  at android.os.Handler.dispatchMessage(Handler.java:95)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5527)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)

如何修复它?

最佳答案

更新你的 oncreate 方法

 protected void onCreate(Bundle savedInstanceState)  {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_creazione_sondaggio);
titolo = (EditText) findViewById(R.id.titolo);
sceltaA = (EditText) findViewById(R.id.A);
sceltaB = (EditText) findViewById(R.id.B);
sceltaC = (EditText) findViewById(R.id.C);
server= (EditText) findViewById(R.id.server);
invio = (Button) findViewById(R.id.invia);

// invio.setOnClickListener(this);


}

关于java - android:onclick 方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36837054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com