1

I'm working on an app and I have the following code:

package com.S.A.Productions.android.first;

import com.S.A.Productions.android.first.R;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class FirstActivity extends Fragment implements OnClickListener {

    int counter;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

                View v = inflater.inflate(R.layout.lin, container, false);

        TextView temp = (TextView) v.findViewById(R.id.textView2);

        //Set the buttons
        Button button2 = (Button) v.findViewById(R.id.button2);


        //+++ BUTTON
        button2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub//Get content of TextView
                TextView temp = (TextView) v.findViewById(R.id.textView2);
                //Convert the string to an integer
                counter = Integer.parseInt(temp.getText().toString());
                counter++;
                temp.setText("" + counter);
                String stringData = temp.getText().toString();
                SharedPreferences.Editor editor = someData.edit();
                editor.putString("sharedString", stringData);
                editor.commit();
            }

        });
        //END OF +++ BUTTON

        return v;
    }


}

But when I run the app and I click on that button the app crashes. I'm using the "v.findViewById" And at the end I return the v. So I don't know what exactly is wrong. Any ideas?

1
  • Can you post the trace and your layout lin xml?
    – Niko
    Feb 15, 2014 at 7:17

2 Answers 2

0

The conflict comes because the View v inside onclick and inflated view v are different. This code will work:

public class FirstActivity extends Fragment implements OnClickListener {

int counter;
View v ;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

            v = inflater.inflate(R.layout.lin, container, false);

    TextView temp = (TextView) v.findViewById(R.id.textView2);

    //Set the buttons
    Button button2 = (Button) v.findViewById(R.id.button2);


    //+++ BUTTON
    button2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub//Get content of TextView
            TextView temp = (TextView) this.v.findViewById(R.id.textView2);
            //Convert the string to an integer
            counter = Integer.parseInt(temp.getText().toString());
            counter++;
            temp.setText("" + counter);
            String stringData = temp.getText().toString();
            SharedPreferences.Editor editor = someData.edit();
            editor.putString("sharedString", stringData);
            editor.commit();
        }

    });
    //END OF +++ BUTTON

    return v;
  }
}
3
  • I understand. I just changed the "v" to "myView" now everything works! Thank you so much, I've been trying to get this for many hours now.
    – Rorrim
    Feb 15, 2014 at 8:05
  • I don't have enough rep :(
    – Rorrim
    Feb 15, 2014 at 8:26
  • You can "upvote" and "tick" the answers in your questions without reps.
    – amalBit
    Feb 15, 2014 at 8:31
0

The argument v of the onClick(View v) is the element that was clicked, in your case, a button.

The error is that You are using argument V (which is a button) as a view group by trying to find your text view by Id from the button itself. The textView will be null. The setText() will cause the crash.

You need to use the findviewbyid() on any of the containers of the textView in the view hierarchy.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.