首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为特定的布局调用findViewById()?findViewById()上的NullPointerException

为特定的布局调用findViewById()?findViewById()上的NullPointerException
EN

Stack Overflow用户
提问于 2014-02-06 02:53:17
回答 1查看 93关注 0票数 0

我有第二个类,它从主类调用一个方法,这个方法将设置一个带有数字的TextView,但是应用程序会因为findViewById()nullPointerException而崩溃。我认为问题在于,当从另一个类调用findViewById时,它将在调用类的setContentView()中查找视图。

那么,如何指定findViewById()正在查看的布局呢?为什么会有“nullPointerException”?

代码语言:javascript
复制
*************************************************
                  MAIN CLASS
*************************************************

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (!NUMBER.exists()) {
        new File("/data/data/com.app.clicker/files").mkdir();
        writeInt(NUMBER, total = 0);
        writeInt(CLICK, perClick = 1);
        writeInt(CASH, $ = 500);
        setView(R.id.numberView, "", readInt(NUMBER));
        setView(R.id.perClickView, "Per Click ", readInt(CLICK));
        setView(R.id.cash, "Cash ", readInt(CASH));
    } else {
        setView(R.id.perClickView, "Per Click ", readInt(CLICK));
        setView(R.id.cash, "Cash ", readInt(CASH));
        setView(R.id.numberView, "", readInt(NUMBER));
    }
}

   public void setTotal(int total) {
    writeInt(NUMBER, total);
    setView(R.id.numberView, "", this.total);
   }
   public void setView(int view, String writeBefore, int num) {
    TextView textView = (TextView) findViewById(view); >> NullPointerException
    textView.setText(writeBefore + Integer.toString(num));
   }

************************************************
                  Second Class 
************************************************    

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dev);
    Main main = new Main();
    total = main.getTotal();
    perClick = main.getPerClick();
    $ = main.get$();
    devListView();
}

final EditText editTotal = new EditText(Dev.this);
            final EditText editPerClick = new EditText(Dev.this);
            final EditText edit$ = new EditText(Dev.this);
            editTotal.setInputType(InputType.TYPE_CLASS_NUMBER);
            editPerClick.setInputType(InputType.TYPE_CLASS_NUMBER);
            edit$.setInputType(InputType.TYPE_CLASS_NUMBER);
            switch (position) {
            case 0:
                Builder devTotal = new AlertDialog.Builder(Dev.this);
                devTotal.setTitle("Set Total").setView(editTotal).setNeutralButton("Set", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                        main.setTotal(Integer.parseInt(editTotal.getText().toString())); ************ Call to Main to SetTotal
                    }
                });
                devTotal.create().show();
                break;
EN

回答 1

Stack Overflow用户

发布于 2014-02-06 03:01:46

从你的代码中:

代码语言:javascript
复制
Main main = new Main();

看起来你是在通过调用new Main()来创建activity (R.layout.activity_main),你不应该这样做:你用startActivity启动activity,如果你想用Intent extras发送一些值给它们。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21585989

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档