0

代码运行良好.. 这是一个简单的计算应用程序。

不幸的是,在我改变了一些东西后它停止了工作..问题是我不知道这个东西..

这是我的代码...

package com.bogaza.barazileyyecalc;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

//declarations..

final TextView        lbl1 =  (TextView)       findViewById(R.id.textView1); 
final TextView        lbl2 =  (TextView)       findViewById(R.id.textView2);
final EditText        txt1 =  (EditText)       findViewById(R.id.editText1); 
final EditText        txt2 =  (EditText)       findViewById(R.id.editText2);
final Button          btn1 =  (Button)         findViewById(R.id.button1); 
final TextView        lbl3 =  (TextView)       findViewById(R.id.textView3);

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

// click button to get numbers from edittexts and add them to textviews ..      

    btn1.setOnClickListener(new OnClickListener() {   


        public void onClick(View v) {  
            if(v.getId() == R.id.button1){

            int value1 = (int) ((txt1.getText().toString()   
                    .equals("")) ? 0 : Double.valueOf(txt1.getText().toString()));   

            int value2 = (int) ((txt2.getText().toString()   
                    .equals("")) ? 0 : Double.valueOf(txt2.getText().toString()));   

            lbl3.setText(String.valueOf(value1 + value2));
            }
          }   

    });   ;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}

}

它工作正常..我试图改变布局并做出更好的设计..之后问题发生了..现在当我点击运行时..它出现在设备上..并直接崩溃..

4

4 回答 4

1

在完成 setContextView 之前,您无法初始化视图。

final TextView        lbl1 =  (TextView)       findViewById(R.id.textView1); 
final TextView        lbl2 =  (TextView)       findViewById(R.id.textView2);
.
.

Oncreate在调用 setContentView 之后将 findViewById 代码移动到。

于 2012-11-18T09:28:32.297 回答
0

把这段代码:

//declarations..

final TextView        lbl1 =  (TextView)       findViewById(R.id.textView1); 
final TextView        lbl2 =  (TextView)       findViewById(R.id.textView2);
final EditText        txt1 =  (EditText)       findViewById(R.id.editText1); 
final EditText        txt2 =  (EditText)       findViewById(R.id.editText2);
final Button          btn1 =  (Button)         findViewById(R.id.button1); 
final TextView        lbl3 =  (TextView)       findViewById(R.id.textView3);

进入你的onCreate()方法。

我希望这有帮助。

于 2012-11-18T09:35:33.453 回答
0

在设置任何布局之前,您已经初始化了视图。

如果您按如下方式修改代码,它应该可以工作:

public class MainActivity extends Activity implements OnClickListener 
{
//declarations..

final TextView lbl1;
final TextView lbl2;
final EditText txt1;
final EditText txt2;
final Button btn1;
final TextView lbl3;

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    lbl1 = (TextView)findViewById(R.id.textView1);
    lbl2 = (TextView)findViewById(R.id.textView2);
    txt1 = (EditText)findViewById(R.id.editText1); 
    txt2 = (EditText)findViewById(R.id.editText2);
    btn1 = (Button)findViewById(R.id.button1); 
    lbl3 = (TextView)findViewById(R.id.textView3);

    // click button to get numbers from edittexts and add them to textviews ..      

    btn1.setOnClickListener(new OnClickListener()
    {
        public void onClick(View v)
        {
            ...
        }
于 2012-11-18T09:39:15.943 回答
0

在 OnCreate() 方法之前找不到 ViewByID() !所以尝试将它们放在 Oncreate() 方法中,如下所示:

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button btn1= (Button)findviewById(R.id.*);
于 2012-11-18T09:59:49.840 回答