我是java的新手并且正在做一些练习。我的问题是代码中的java.lang.NullPointerException。我读了很多问题,但我无法找到答案。所有字符串都添加了strings.xml
logcat错误:
06-13 19:08:07.690: D/AndroidRuntime(865): Shutting down VM
06-13 19:08:07.690: W/dalvikvm(865): threadid=1: thread exiting with uncaught exception group=0xb2ab1ba8)
06-13 19:08:07.720: E/AndroidRuntime(865): FATAL EXCEPTION: main
06-13 19:08:07.720: E/AndroidRuntime(865): Process: com.example.fpscalculator, PID: 865
06-13 19:08:07.720: E/AndroidRuntime(865): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fpscalculator/com.example.fpscalculator.MainActivity}: java.lang.NullPointerException
我的xml代码:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="499dp" >
<EditText
android:id="@+id/bbweight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="34dp"
android:ems="10"
android:inputType="number"
android:labelFor="@id/bbweight"
android:text="@string/bbweight" />
<EditText
android:id="@+id/bbfps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/bbweight"
android:layout_below="@+id/bbweight"
android:layout_marginTop="34dp"
android:ems="10"
android:inputType="number"
android:text="@string/bbfps"
android:labelFor="@id/bbfps"
/>
<Button
android:id="@+id/calc"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/bbfps"
android:layout_alignParentRight="true"
android:layout_marginBottom="9dp"
android:layout_marginRight="18dp"
android:text="@string/calc" />
<TextView
android:id="@+id/tenergy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/bbfps"
android:layout_below="@+id/bbfps"
android:layout_marginTop="25dp"
android:text="@string/tenergy" />
<TextView
android:id="@+id/fps20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tenergy"
android:layout_below="@+id/tenergy"
android:layout_marginTop="30dp"
android:text="@string/fps20"
/>
<TextView
android:id="@+id/fps25"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/fps20"
android:layout_below="@+id/fps20"
android:layout_marginTop="30dp"
android:text="@string/fps25" />
<TextView
android:id="@+id/fps28"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/fps25"
android:layout_below="@+id/fps25"
android:layout_marginTop="30dp"
android:text="@string/fps28" />
<TextView
android:id="@+id/fps30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/fps28"
android:layout_below="@+id/fps28"
android:layout_marginTop="30dp"
android:text="@string/fps30" />
<TextView
android:id="@+id/fps40"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/fps30"
android:layout_below="@+id/fps30"
android:layout_marginTop="30dp"
android:text="@string/fps40" />
</RelativeLayout>
java代码:
package com.example.fpscalculator;
import android.app.Activity;
import android.app.ActionBar;
import android.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;
import java.lang.Math;
public class MainActivity extends Activity {
double fps,energy;
double bb20=0.20,bb25=0.25,bb28=0.28,bb30=0.30,bb40=0.40;
EditText bbweight;
EditText bbfps;
Button calc;
TextView displayenergy, fps20, fps25, fps28, fps30, fps40;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fps=0;
bbweight = (EditText) findViewById(R.id.bbweight);
bbfps = (EditText) findViewById(R.id.bbfps);
displayenergy = (TextView) findViewById(R.id.tenergy);
fps20 = (TextView) findViewById(R.id.fps20);
fps25 = (TextView) findViewById(R.id.fps25);
fps28 = (TextView) findViewById(R.id.fps28);
fps30 = (TextView) findViewById(R.id.fps30);
fps40 = (TextView) findViewById(R.id.fps40);
calc = (Button) findViewById(R.id.calc);
calc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
double BBweight =Double.valueOf(bbweight.getText().toString());
double BBfps =Double.valueOf(bbfps.getText().toString());
fps = 0;
energy = 0;
bb20=0.20;bb25=0.25;bb28=0.28;bb30=0.30;bb40=0.40;
energy = 0.5 * (0.001 * BBweight) * (Math.pow((0.3048 * BBfps),2));
displayenergy.setText("BB energy = " + energy + " joule");
fps = BBfps * (Math.sqrt(BBweight/bb20));
fps20.setText("0.20g BB fps = " + fps);
fps = BBfps * (Math.sqrt(BBweight/bb25));
fps25.setText("0.25g BB fps = " + fps);
fps = BBfps * (Math.sqrt(BBweight/bb28));
fps28.setText("0.28g BB fps = " + fps);
fps = BBfps * (Math.sqrt(BBweight/bb30));
fps30.setText("0.30g BB fps = " + fps);
fps = BBfps * (Math.sqrt(BBweight/bb40));
fps40.setText("0.40g BB fps = " + fps);
}
});
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, 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_main, container,
false);
return rootView;
}
}
}
答案 0 :(得分:0)
您已将代码放置在托管片段的Activity
内,因为您需要在onCreateView()
PlaceholderFragment
片段中编写该代码。我假设您发布的xml属于PlaceholderFragment
片段而非Activity
类。从MainActivity
类中删除代码段并替换如下,
public class MainActivity extends Activity {
double fps,energy;
double bb20=0.20,bb25=0.25,bb28=0.28,bb30=0.30,bb40=0.40;
EditText bbweight;
EditText bbfps;
Button calc;
TextView displayenergy, fps20, fps25, fps28, fps30, fps40;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
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_main, container,
false);
fps=0;
bbweight = (EditText)rootView.findViewById(R.id.bbweight);
bbfps = (EditText)rootView.findViewById(R.id.bbfps);
displayenergy = (TextView)rootView.findViewById(R.id.tenergy);
fps20 = (TextView)rootView.findViewById(R.id.fps20);
fps25 = (TextView)rootView.findViewById(R.id.fps25);
fps28 = (TextView)rootView.findViewById(R.id.fps28);
fps30 = (TextView)rootView.findViewById(R.id.fps30);
fps40 = (TextView)rootView.findViewById(R.id.fps40);
calc = (Button)rootView.findViewById(R.id.calc);
calc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
double BBweight =Double.valueOf(bbweight.getText().toString());
double BBfps =Double.valueOf(bbfps.getText().toString());
fps = 0;
energy = 0;
bb20=0.20;bb25=0.25;bb28=0.28;bb30=0.30;bb40=0.40;
energy = 0.5 * (0.001 * BBweight) * (Math.pow((0.3048 * BBfps),2));
displayenergy.setText("BB energy = " + energy + " joule");
fps = BBfps * (Math.sqrt(BBweight/bb20));
fps20.setText("0.20g BB fps = " + fps);
fps = BBfps * (Math.sqrt(BBweight/bb25));
fps25.setText("0.25g BB fps = " + fps);
fps = BBfps * (Math.sqrt(BBweight/bb28));
fps28.setText("0.28g BB fps = " + fps);
fps = BBfps * (Math.sqrt(BBweight/bb30));
fps30.setText("0.30g BB fps = " + fps);
fps = BBfps * (Math.sqrt(BBweight/bb40));
fps40.setText("0.40g BB fps = " + fps);
}
});
return rootView;
}
}
}
答案 1 :(得分:0)
请检查您是否初始化了代码中使用的每个控件和变量。因为它是空指针异常的主要原因。