致命异常:主Java.lang.RuntimeException:无法启动活动ComponentInfo

时间:2014-06-23 12:26:18

标签: android

我正在尝试开发基本计算器我收到致命异常无法启动活动组件。

以下是来自MainActivity.java的代码

  package com.madito.basiccalculator;

    import android.support.v7.app.ActionBarActivity;
    import android.support.v7.app.ActionBar;
    import android.support.v4.app.Fragment;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.os.Build;

public class MainActivity extends ActionBarActivity {

    Button calc;
    EditText number;
    TextView display;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);


                 calc =(Button)findViewById(R.id.btnCalculate);

                calc.setOnClickListener(new View.OnClickListener()
                {
                    public void onClick(View v)
                    {
                         number=(EditText)findViewById(R.id.num);
                          display =(TextView)findViewById(R.id.display);

                         double num = Double.parseDouble(number.getText().toString());

                         num=num*5;

                         display.setText(num + "");

                    }
                });
            }





        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            `enter code here`int id = item.getItemId();
            if (id == R.id.action_settings) {
            `enter code here`    return true;
            }
           `enter code here` return super.onOptionsItemSelected(item);
        }

        /**
         * A placeholder fragment containing a simple view.
         */
        public static class PlaceholderFragment extends Fragment {

            public PlaceholderFragment() {
            }

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
             `enter code here`       Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.fragment_main, container, false);
                return rootView;
            }
        }

    }

fragment_main.xml代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.madito.basiccalculator.MainActivity$PlaceholderFragment" >

    <TextView
        android:id="@+id/lblTittle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="58dp"
        android:text="Basic Calculator"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/btnCalculate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="27dp"
        android:text="Calculator" />

    <TextView
        android:id="@+id/display"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/lblTittle"
        android:layout_below="@+id/btnCalculate"
        android:layout_marginTop="66dp"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btnCalculate"
        android:layout_alignLeft="@+id/lblTittle"
        android:layout_marginBottom="24dp"
        android:ems="5"
        android:inputType="numberSigned|numberDecimal" />

</LinearLayout>

以下是我的Logcat中出现的异常

LOGCAT:

FATAL EXCEPTION: main
AndroidRuntime(1116): Process: com.madito.basiccalculator, java.lang.RuntimeException: Unable to start activity ComponentInfocom.madito.basiccalculator/com.madito.basiccalculator.MainActivity java.lang.NullPointerException

1 个答案:

答案 0 :(得分:0)

您的XML是片段并使用占位符进行扩展.. 因此,您无法访问Button中的 onCreate() ,它将返回NullPointerException

您有2个选项

1)

更改

setContentView(R.layout.activity_main);

setContentView(R.layout.fragment_main);

2)

或在 Button

中发布 onCreateView() 的代码
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
           Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        calc =(Button)rootview.findViewById(R.id.btnCalculate);
        // Your Other Code


        return rootView;
    }

此处您夸大您的片段,因此当您想要访问Fragment的子视图时,您必须使用视图对象(rootview)