加载时总是app崩溃..

时间:2012-11-18 09:25:55

标签: java android

代码工作正常..这是一个简单的计算应用程序。

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

这是我的代码......

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 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分: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()方法。

我希望这会有所帮助。

答案 2 :(得分: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)
        {
            ...
        }

答案 3 :(得分:0)

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

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

Button btn1= (Button)findviewById(R.id.*);