Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么每次在eclipse中运行Android应用程序时,我的logcat上都会出现这些错误?_Java_Android_Runtime Error - Fatal编程技术网

Java 为什么每次在eclipse中运行Android应用程序时,我的logcat上都会出现这些错误?

Java 为什么每次在eclipse中运行Android应用程序时,我的logcat上都会出现这些错误?,java,android,runtime-error,Java,Android,Runtime Error,我正在测试我的android应用程序是否正常工作,它由两个活动屏幕组成。我的代码中没有错误,但我的应用程序无法运行。它总是在模拟器上给我这个错误,不幸的是“应用程序名”已经停止 这是我的活动代码 public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.

我正在测试我的android应用程序是否正常工作,它由两个活动屏幕组成。我的代码中没有错误,但我的应用程序无法运行。它总是在模拟器上给我这个错误,不幸的是“应用程序名”已经停止

这是我的活动代码

    public class MainActivity extends Activity
{

        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            View title = getWindow().findViewById(android.R.id.title);
            View titleBar = (View) title.getParent();
            titleBar.setBackgroundColor(Color.RED);
            Button next=(Button)findViewById(R.id.DGButton);
            next.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    Intent myIntent=new Intent(view.getContext(), 

    Activity2.class);
                    startActivityForResult(myIntent, 0);
                }});}

        public void calculateClickHandler(View view)
        {
            if (view.getId() == R.id.CalculateButton)
            {
                EditText ageText = (EditText)findViewById

    (R.id.AgeField);
                EditText weightText = (EditText)findViewById

    (R.id.WeightField);
                EditText ftText = (EditText)findViewById

    (R.id.HeightField);
                EditText inText = (EditText)findViewById

    (R.id.HeightField2);
                RadioGroup weightRG = (RadioGroup) findViewById

    (R.id.WeightRG);
                RadioGroup sexRG = (RadioGroup) findViewById

    (R.id.SexRG);

                TextView resultText = (TextView)findViewById

    (R.id.ResultLabel);
                TextView normalBMIText = (TextView)findViewById

    (R.id.NormalBMI);
                TextView idealKgText = (TextView)findViewById

    (R.id.IdealKgLabel);
                TextView idealLbText = (TextView)findViewById

    (R.id.IdealLbLabel);
                int age = Integer.parseInt(ageText.getText

    ().toString());
                double weight = Double.parseDouble

    (weightText.getText().toString());
                double ftheight = Double.parseDouble(ftText.getText

    ().toString());
                double inheight = Double.parseDouble(inText.getText

    ().toString());
                int checkedRadioButton1 = 

    weightRG.getCheckedRadioButtonId();
                int checkedRadioButton2 = 

    sexRG.getCheckedRadioButtonId();

                double bmiValue = calculateBMI(weight, ftheight, 

    inheight, checkedRadioButton1);
                String bmiInterpretation1 = interpretBMI1(bmiValue);
                String bmiInterpretation2 = interpretBMI2(age);
                String bmiInterpretation3 = interpretBMI3(ftheight, 

    inheight, checkedRadioButton2);
                String bmiInterpretation4 = interpretBMI4(ftheight, 

    inheight, checkedRadioButton2);

                resultText.setText(bmiValue + " - " + 

    bmiInterpretation1);
                normalBMIText.setText(""+bmiInterpretation2);
                idealKgText.setText(""+bmiInterpretation3);
                idealLbText.setText(""+bmiInterpretation4);

                Intent intent1 = new Intent(MainActivity.this, 

    Activity2.class);
                Bundle b = new Bundle();
                b.putDouble("key", bmiValue);
                intent1.putExtras(b);
                startActivity(intent1);
    }}
这是错误日志:

这是调试日志:

感谢您的帮助这是由NullPointerException引起的。您可能正在访问尚未初始化的空对象。你用你的代码编辑你的问题,这样就有机会编辑我的答案

  02:50:45.606: E/AndroidRuntime(1776): 
  Caused by: java.lang.NullPointerException 12-26 02:50:45.606: E/AndroidRuntime(1776): at
 com.example.bmicaldg.MainActivity.onCreate(MainActivity.java:25) 12-26 
在您的问题中,没有任何代码如此难说如何修复,但基于堆栈跟踪,MainActivity.java中的第25行抛出了NullPointerException。第25行的代码会导致null,您试图调用null引用上的某些操作,这会导致NullPointerException

这就是您的错误所在,MainActivity类的第25行。您引用的任何内容都为空。

在创建代码时将MainActivity更改为:

public class MainActivity extends Activity

{

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        View title = getWindow().findViewById(android.R.id.title);

        if (title != null) { 
             ViewParent titleBar = title.getParent(); 
           if (titleBar != null && (titleBar instanceof View)) { 
                View parentView = (View)titleBar; 
                parentView.setBackgroundColor(Color.RED); 
              } 
         } 

        // Your Code here...

如果您的应用程序在android 3.0+上使用ActionBar默认值,则查找id android.R.id.title将返回null。
  02:50:45.606: E/AndroidRuntime(1776): 
  Caused by: java.lang.NullPointerException 12-26 02:50:45.606: E/AndroidRuntime(1776): at
 com.example.bmicaldg.MainActivity.onCreate(MainActivity.java:25) 12-26 
Caused by: java.lang.NullPointerException 12-26 02:50:45.606: E/AndroidRuntime(1776): at com.example.bmicaldg.MainActivity.onCreate(MainActivity.java:25)
public class MainActivity extends Activity

{

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        View title = getWindow().findViewById(android.R.id.title);

        if (title != null) { 
             ViewParent titleBar = title.getParent(); 
           if (titleBar != null && (titleBar instanceof View)) { 
                View parentView = (View)titleBar; 
                parentView.setBackgroundColor(Color.RED); 
              } 
         } 

        // Your Code here...