簡體   English   中英

Android代碼NPE錯誤:java.lang.RuntimeException:無法啟動活動ComponentInfo

[英]Android Code NPE Error : java.lang.RuntimeException: Unable to start activity ComponentInfo

我們正在努力為該學期項目申請營養。

嘗試在模擬器上運行程序時,apk文件將運行並立即關閉並出現錯誤。

Java代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM