简体   繁体   中英

android:onclick method doesn't work

xml code:

 <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 code:

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();



    }


}

Error:

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.per formClick(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.ja va: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)

How can I fix it?

update your oncreate method

 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);


    }

Xml Layout:

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

Add following code to your activity:

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

        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(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(CreazioneSondaggio.this, "ok", Toast.LENGTH_LONG).show();
                // code
            }
        });

    }

Do like this...

super.onCreate(savedInstanceState);
        setContentView(R.layout.your_layout);
    InviaButton = (TextView) findViewById(R.id.invia);

     InviaButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    //your_code
      }
    }

you need to find each view of activity once layout is inflated to activity as follows

    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);

and since you have used attribute onClick no need to initiate Button reference you can do directly

 public void onClick1(View v) {

           if(v.getId() == R.id.invia)
           {

           }


        }

Read the full stack here it is saying java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' you have not initialized EditText but you are using in your onClick method. Try to figure it out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM