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

时间:2015-04-03 22:17:39

标签: java android

我的Android项目总是出现错误。

  

java.lang.NullPointerException:尝试调用虚方法   'android.view.View android.app.Activity.findViewById(int)'上的null   对象参考

在我的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;
    }
}

2 个答案:

答案 0 :(得分:12)

除了@Selvin所指出的,它看起来像这样:

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

......应该改为:

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

答案 1 :(得分:0)

  

这很简单

public class MainActivity extends AppCompatActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn1 = (Button) findViewById(R.id.button1);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              Intent intent = new Intent(MainActivity.this, YourNextClass.class);
                startActivity(intent);
            }
        });

    }
}