java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.view.View android.app.Activity.findViewById(int)”

新手上路,请多包涵

我的 Android 项目总是出错。

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.view.View android.app.Activity.findViewById(int)”

在我的 MainActivity 中,我调用了 FragmentStart 类。

FragmentStart.class

 import android.app.Activity;
import android.content.Intent;
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.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class FragmentStart extends Fragment {

    //Define Listview
    private ListView startList;
    private String[] stringList;
    Activity activity = getActivity();

    public FragmentStart() {
        //
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_start, container, false);

        // Create ListView start
        startList = (ListView) rootView.findViewById(R.id.startList);
        stringList = getResources().getStringArray(R.array.startList);

        if (activity != null) {
            ArrayAdapter<String> objAdapter = new ArrayAdapter<String>(activity, android.R.layout.simple_list_item_1, stringList);
            startList.setAdapter(objAdapter);
            startList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    switch (startList.getPositionForView(view)) {

                        case 0:
                            // ...
                            break;

                    }
                }
            });
        }
        // Create ListView end

        //New Entry
        Button add = (Button) activity.findViewById(R.id.add);
        add.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), addItem.class);
                startActivityForResult(myIntent, 0);
            }

        });

        return rootView;
    }
}

原文由 user4747848 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 561
2 个回答

除了@Selvin 指出的内容之外,它看起来像这样:

 Button add = (Button) activity.findViewById(R.id.add);

……应该改成这样:

 Button add = (Button) rootView.findViewById(R.id.add);

原文由 Daniel Nugent 发布,翻译遵循 CC BY-SA 3.0 许可协议

使用以下代码:

  listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

       //     selectedPosition = position;

            View newTypeView=listView1.getChildAt(position-listView1.getFirstVisiblePosition()).findViewById(R.id.RelativeLayout);

            if (newTypeView.getClass().getName().toString().contains("RelativeLayout")) {

                if (lastTouchedVw != null)
                    lastTouchedVw.setBackgroundColor(Color.parseColor("#ffffff"));
                 // mainImageView.setImageURI(Uri.parse(al.get(position).toString()));
                //bigImageRelativeLayoutVw.setBackgroundColor(Color.parseColor("#ffb000"));
                    newTypeView.setBackgroundResource(R.drawable.cellbg);

                    lastTouchedVw = newTypeView;
            }

原文由 Aman Goel 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题