-5

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

这是我的活动代码

    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);
    }}

这是错误日志:

12-26 02:50:45.606: E/AndroidRuntime(1776): FATAL EXCEPTION: main
12-26 02:50:45.606: E/AndroidRuntime(1776): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bmicaldg/com.example.bmicaldg.MainActivity}: java.lang.NullPointerException
12-26 02:50:45.606: E/AndroidRuntime(1776):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-26 02:50:45.606: E/AndroidRuntime(1776):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-26 02:50:45.606: E/AndroidRuntime(1776):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-26 02:50:45.606: E/AndroidRuntime(1776):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-26 02:50:45.606: E/AndroidRuntime(1776):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-26 02:50:45.606: E/AndroidRuntime(1776):     at android.os.Looper.loop(Looper.java:137)
12-26 02:50:45.606: E/AndroidRuntime(1776):     at android.app.ActivityThread.main(ActivityThread.java:5039)
12-26 02:50:45.606: E/AndroidRuntime(1776):     at java.lang.reflect.Method.invokeNative(Native Method)
12-26 02:50:45.606: E/AndroidRuntime(1776):     at java.lang.reflect.Method.invoke(Method.java:511)
12-26 02:50:45.606: E/AndroidRuntime(1776):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-26 02:50:45.606: E/AndroidRuntime(1776):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-26 02:50:45.606: E/AndroidRuntime(1776):     at dalvik.system.NativeStart.main(Native Method)
12-26 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 02:50:45.606: E/AndroidRuntime(1776):     at android.app.Activity.performCreate(Activity.java:5104)
12-26 02:50:45.606: E/AndroidRuntime(1776):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-26 02:50:45.606: E/AndroidRuntime(1776):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-26 02:50:45.606: E/AndroidRuntime(1776):     ... 11 more

这是调试日志:

 12-26 03:04:06.238: I/Process(1854): Sending signal. PID: 1854 SIG: 9
    12-26 03:04:12.779: W/Trace(1876): Unexpected value from nativeGetEnabledTags: 0
    12-26 03:04:12.837: W/Trace(1876): Unexpected value from nativeGetEnabledTags: 0
    12-26 03:04:14.866: D/dalvikvm(1876): GC_CONCURRENT freed 76K, 7% free 2723K/2916K, paused 32ms+32ms, total 270ms
    12-26 03:04:15.427: D/AndroidRuntime(1876): Shutting down VM
    12-26 03:04:15.456: W/dalvikvm(1876): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
    12-26 03:04:15.546: E/AndroidRuntime(1876): FATAL EXCEPTION: main
    12-26 03:04:15.546: E/AndroidRuntime(1876): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bmicaldg/com.example.bmicaldg.MainActivity}: java.lang.NullPointerException
    12-26 03:04:15.546: E/AndroidRuntime(1876):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
    12-26 03:04:15.546: E/AndroidRuntime(1876):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    12-26 03:04:15.546: E/AndroidRuntime(1876):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
    12-26 03:04:15.546: E/AndroidRuntime(1876):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    12-26 03:04:15.546: E/AndroidRuntime(1876):     at android.os.Handler.dispatchMessage(Handler.java:99)
    12-26 03:04:15.546: E/AndroidRuntime(1876):     at android.os.Looper.loop(Looper.java:137)
    12-26 03:04:15.546: E/AndroidRuntime(1876):     at android.app.ActivityThread.main(ActivityThread.java:5039)
    12-26 03:04:15.546: E/AndroidRuntime(1876):     at java.lang.reflect.Method.invokeNative(Native Method)
    12-26 03:04:15.546: E/AndroidRuntime(1876):     at java.lang.reflect.Method.invoke(Method.java:511)
    12-26 03:04:15.546: E/AndroidRuntime(1876):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    12-26 03:04:15.546: E/AndroidRuntime(1876):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    12-26 03:04:15.546: E/AndroidRuntime(1876):     at dalvik.system.NativeStart.main(Native Method)
    12-26 03:04:15.546: E/AndroidRuntime(1876): Caused by: java.lang.NullPointerException
    12-26 03:04:15.546: E/AndroidRuntime(1876):     at com.example.bmicaldg.MainActivity.onCreate(MainActivity.java:25)
    12-26 03:04:15.546: E/AndroidRuntime(1876):     at android.app.Activity.performCreate(Activity.java:5104)
    12-26 03:04:15.546: E/AndroidRuntime(1876):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    12-26 03:04:15.546: E/AndroidRuntime(1876):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
    12-26 03:04:15.546: E/AndroidRuntime(1876):     ... 11 more
    12-26 03:04:22.547: I/Process(1876): Sending signal. PID: 1876 SIG: 9

任何帮助表示赞赏

4

4 回答 4

4
  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 throwing 中的第 25 行NullPointerException。第 25 行的代码是一些如何导致 null 的代码,您正在尝试对null导致NullPointerException.

于 2012-12-26T03:11:48.273 回答
3

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

于 2012-12-26T03:11:38.300 回答
2
Caused by: java.lang.NullPointerException 12-26 02:50:45.606: E/AndroidRuntime(1776): at com.example.bmicaldg.MainActivity.onCreate(MainActivity.java:25)

这是你的错误所在,你MainActivity班级的第 25 行。无论您引用的是null.

于 2012-12-26T03:13:55.880 回答
1

MainActivity将您的onCreate 代码更改为:

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...
于 2012-12-26T03:41:56.953 回答