创建按钮时出现NullPointerException

时间:2015-02-09 04:22:41

标签: android nullpointerexception

我正在尝试为android程序创建一堆按钮,但是当我尝试将我在XML文件中创建的按钮“连接”到我在Java文件中创建的按钮时,我一直得到NullPointerException。我一直想弄清楚这个错误,想知道你们中是否有人能够摆脱一些光明

public class AddActivity extends ActionBarActivity {
    private ExerciseLoader loader = new ExerciseLoader();
    private ArrayList<Button> listofButton = new ArrayList<Button>();
    private Button button=new Button(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_add);
        Intent intent = getIntent();
        createButtons();
        textButtons();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.second, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void createButtons() {
        for (int i = 0; i < loader.getList().size(); i++) {
            String buttonID = "btn" + i;
            int resID = getResources().getIdentifier(buttonID, "id",
                    getPackageName());
            listofButton.add(((Button) findViewById(resID)));
            // listofButton.get(i).setText("This is a test");
        }
    }
}

这是错误代码

java.lang.RuntimeException: Unable to instantiate activity
    ComponentInfo{com.example.alert/com.***.alert.AddActivity}:
    java.lang.NullPointerException: Attempt to invoke virtual method
    'android.content.res.Resources android.content.Context.getResources()'
    on a null object reference

2 个答案:

答案 0 :(得分:0)

for (int i = 0; i < loader.getList().size(); i++) {
        String buttonID = "btn" + i;
        int resID = AddActivity.this.getResources().getIdentifier(buttonID, "id", getPackageName());
        listofButton.add(((Button) findViewById(resID)));
        // listofButton.get(i).setText("This is a test");
    }

将此代码写入onCreate并尝试再次运行。我希望它能奏效。

答案 1 :(得分:0)

尝试在onCreate()

中实例化您的按钮

喜欢这个;

    public class AddActivity extends ActionBarActivity 
{
    private ExerciseLoader loader = new ExerciseLoader();
    private ArrayList<Button> listofButton = new ArrayList<Button>();
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_add);

         button=new Button(this);

        Intent intent = getIntent();
        createButtons();
        textButtons();

    }

   // Remaining coding stuff....

你可能不会得到例外。