Cannot fix Java null pointer error

前端 未结 4 696
悲哀的现实
悲哀的现实 2021-01-29 06:19

I have some code that when I run it returns a null pointer error. This is the code and error from the logcat:

import android.support.v7.app.AppCompatActivity;
im         


        
相关标签:
4条回答
  • 2021-01-29 06:42

    Try to change the theme and check if it works.

     android:theme="@android:style/Theme.Holo.Light"
    
    0 讨论(0)
  • The problem is this line:

    !textViewString.equals()
    

    Because you've not given the value to the textViewString anywhere before using it. Assign it to an empty string at least, or simply delete that line since at the moment it's not doing you much good, I don't really get it's point. Since you're nowhere changing it's value, none of your buttons will work (even without the exception).

    0 讨论(0)
  • 2021-01-29 07:01

    You are not setting the onClickListener for your red button, you are reassigning the listener to the blue button.

    Button red = (Button) findViewById(R.id.red_button);
    blue.setOnClickListener(new View.OnClickListener() {
    

    Have you also defined the onClick property in the xml layout file? If so then you need to ensure you have a method matching the name you have specified. For example if you have entered

    onClick="buttonClicked"
    

    You need to define a method called

    public void buttonClicked() { /* OnClick functionality here */ }
    

    See: @https://stackoverflow.com/a/38547338/1540698 from @vucko for your second issue.

    0 讨论(0)
  • 2021-01-29 07:01

    In your Layout, you say:

    android:onClick="onClick"
    

    It appears that you did not include all of the code for the Activity but I'd bet that it does not define a method named onClick

    All you need to do is define the method.

    0 讨论(0)
提交回复
热议问题