문제

I am relatively new to android development and there are a few ideas I am having trouble getting my head around.

What I am trying to achieve is when a button is clicked, a view is inflated to a LinearLayout of R.id.resultsBox. My idea behind what I think I have to do is this:

public class FindClassFragment extends Fragment {

    private Button search_button;
    private EditText course_abbrev_field;
    private EditText course_num_field;
    private Spinner semester_spinner;
    private String course_abbrev;
    private String course_num;
    private String semester;
    private ArrayList<String[]> sectionsList;

    private LinearLayout resultsBox;

    private WebScraper scraper;

    public FindClassFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_findclass, container, false);
        resultsBox = (LinearLayout) rootView.findViewById(R.id.resultsView);
        RelativeLayout f = (RelativeLayout) inflater.inflate(R.layout.fragment_section, null);

        search_button = (Button) rootView.findViewById(R.id.search_button);
        course_abbrev_field = (EditText) rootView.findViewById(R.id.course_abbrev);
        course_num_field = (EditText) rootView.findViewById(R.id.course_num);

        assert search_button != null;
        search_button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                System.out.println("CLICK");

                if(true) {
                    resultsBox.removeAllViews();
                    LinearLayout l = (LinearLayout) view.inflate(getActivity(), R.layout.error, (ViewGroup) view);
                    resultsBox.addView(l);
                } else {
                    resultsBox.removeAllViews();
                }

            }

        });

        return rootView;
    }

}

I figured out how to get the click event working. "CLICK" is printed to the log when I click it. I thought I inflated the view correctly but obviously not because the application crashed.

I'm assuming my error has something to do with inheritance from the FindClassFragment class to the new View.OnClickListener() sub class but I'm just not sure what exactly.

I tried defining everything in the scope of the class but that didn't work... I'm lost.

A helpful hint in the right direction is more than appreciated!

EDIT: Not worried about variable null pointer exceptions, I'm worried about getting the view to inflate correctly.

The error log I am getting is:

java.lang.ClassCastException: android.widget.Button cannot be cast to android.view.ViewGroup
        at github.m_porter.fragments.FindClassFragment$1.onClick(FindClassFragment.java:72)
        at android.view.View.performClick(View.java:4354)
        at android.view.View$PerformClick.run(View.java:17967)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5329)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
        at dalvik.system.NativeStart.main(Native Method)

For those who don't like to count, line 67 is LinearLayout l = (LinearLayout) view.inflate(getActivity(), R.layout.error, (ViewGroup) view);

도움이 되었습니까?

해결책

At line# 67 in onClick() of FindClassFragment class is giving NullPointer exception.

Put break point and check which object/variable is null fix that first and give a try.

Edited: Couldn't find semester_spinner initialized in the code. Initialize it before using it.

Edited:

Use like this:

LayoutInflater layoutInflater = (LayoutInflater)    getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout layout = layoutInflater.inflate(R.layout.error,null);
resultsBox.addView(layout);

다른 팁

At line 67, the view that you are referring to is the search_button. The view that is being clicked.

Obviously it cannot be cast to ViewGroup...

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top