Android代码NPE错误:java.lang.RuntimeException:无法启动活动ComponentInf



我们正在为学期项目做一个营养学应用。

当尝试在模拟器上运行程序时,apk文件运行并立即关闭,并出现错误。

java code:

package com.example.nutripro;
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.os.Build;
 public class BMI extends ActionBarActivity {
EditText hgtf;
EditText wgtf;
EditText bmi;
Button calc;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    hgtf=(EditText)findViewById(R.id.editText1);
    wgtf=(EditText)findViewById(R.id.editText2);
    bmi=(EditText)findViewById(R.id.editText3);
    calc=(Button)findViewById(R.id.button1);
    calc.setOnClickListener(new ClickButton ());        
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }
}
private class ClickButton implements Button.OnClickListener{
    @Override
    public void onClick(View v) {
        float hg,wg,sh,res; 
        hg=Float.parseFloat(hgtf.getText().toString());
        wg=Float.parseFloat(wgtf.getText().toString());
        sh=hg*hg;
        res=sh/wg;
        bmi.setText(" "+"Your calculated body mass index is:"+res);
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.bmi, menu);
    return true;
}
@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.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    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,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_bmi, container,
                false);
        return rootView;
    }
  }
  }

片段:

<RelativeLayout 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.example.nutripro.BMI$PlaceholderFragment" >
<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button1"
    android:layout_alignLeft="@+id/editText3"
    android:layout_marginBottom="37dp"
    android:ems="10"
    android:inputType="numberDecimal" />
<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/editText2"
    android:layout_alignLeft="@+id/editText2"
    android:layout_marginBottom="17dp"
    android:ems="10"
    android:inputType="numberDecimal" />
<EditText
    android:id="@+id/editText3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="83dp"
    android:ems="10" >
    <requestFocus />
</EditText>
<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/editText3"
    android:layout_alignLeft="@+id/editText3"
    android:layout_marginBottom="30dp"
    android:layout_marginLeft="37dp"
    android:text="Button" />
<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="25dp"
    android:layout_marginTop="24dp"
    android:text="BMI Calculator"
    android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="18dp"
    android:text="Calculate your Body Mass Index"
    android:textAppearance="?android:attr/textAppearanceSmall" />

manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.nutripro"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.nutripro.BMI"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
</manifest>

logcat:

05-07 03:14:31.380: E/AndroidRuntime(862): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nutripro/com.example.nutripro.BMI}: java.lang.NullPointerException
谁能建议我们解决这个问题?

findViewById()setOnClickListener()从活动onCreate()移动到片段onCreateView(),因为您操作的视图在片段布局中而不是活动布局中:

View rootView = inflater.inflate(R.layout.fragment_bmi, container, false);
hgtf=(EditText)rootView.findViewById(R.id.editText1);
wgtf=(EditText)rootView.findViewById(R.id.editText2);
bmi=(EditText)rootView.findViewById(R.id.editText3);
calc=(Button)rootView.findViewById(R.id.button1);
calc.setOnClickListener(new ClickButton ());        

最新更新