Android 程序 onCreate() 中出现 NullPointerException

发布于 2024-10-12 18:09:15 字数 7635 浏览 4 评论 0原文

我对 Android 相当陌生,正在编写一个可以解决物理问题的应用程序。由于这些论坛的一些帮助,数学进展顺利,但是当我尝试从列表启动我的活动时,它在新活动的 onCreate 方法中出现了空指针异常。但这似乎没有多大意义,因为只有一个提交按钮可以从某些 EditText 视图执行数学运算。这是我的代码。

package android.physicsengine;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import java.lang.Math;

public class ProjectileMotion extends Activity {

 private EditText acceleration;
 private EditText finalVelocity;
 private EditText initialVelocity;
 private EditText time;
 private EditText deltay;
 private EditText velocity;
 private EditText deltax;
 private Button submitButton;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.projectile_motion_layout);

  submitButton.setOnClickListener(new OnClickListener() { 
   @Override
   public void onClick(View v) {
    acceleration = (EditText)findViewById(R.id.acceleration);
    double a = Doublify(acceleration);

    finalVelocity = (EditText)findViewById(R.id.finalVelocity);
    double vf = Doublify(finalVelocity);

    initialVelocity = (EditText)findViewById(R.id.intitialVelocity);
    double vi = Doublify(initialVelocity);

    time = (EditText)findViewById(R.id.time);
    double t = Doublify(time);

    deltay = (EditText)findViewById(R.id.deltay);
    double y = Doublify(deltay);

    velocity = (EditText)findViewById(R.id.velocity);
    double vx = Doublify(velocity);

    deltax = (EditText)findViewById(R.id.deltax);
    double x = Doublify(deltax);


    //Y Axis
    if(time.getText()==null && deltay.getText()==null){
     time.setText(Double.toString((vf-vi)/a));
     deltay.setText(Double.toString(((vf-vi)/a)+(a*Math.pow(((vf-vi)/a),2))));
    }
    if(acceleration.getText()==null && deltay.getText()==null){
     acceleration.setText(Double.toString((vf-vi)/t));
     deltay.setText(Double.toString((vi*t+.5*((vf-vi)/t))*Math.pow(t,2)));
    }
    if(acceleration.getText()==null && time.getText()==null){
     acceleration.setText(Double.toString(((Math.pow(vf,2)-Math.pow(vi,2)))/2*y));
     time.setText(Double.toString(2*y*(vf-vi)/(Math.pow(vf,2)-vi)));
    }
    if(initialVelocity.getText()==null && deltay.getText()==null){
     initialVelocity.setText(Double.toString(vf-a*t));
     deltay.setText(Double.toString((vf-a*t)*t+.5*a*Math.pow(t,2)));
    }
    if(initialVelocity.getText()==null && time.getText()==null){
     initialVelocity.setText(Double.toString(Math.sqrt(Math.pow(vf,2)-2*a*y)));
     time.setText(Double.toString((vf-Math.sqrt(Math.pow(vf,2)-2*a*y))/2));
    }
    if(initialVelocity.getText()==null && acceleration.getText()==null){
     initialVelocity.setText(Double.toString(vf-2*(vf-y/t)));
     acceleration.setText(Double.toString((2/t)*(vf-y/t)));
    }
    if(finalVelocity.getText()==null && deltay.getText()==null){
     finalVelocity.setText(Double.toString(vi+a*t));
     deltay.setText(Double.toString(vi*t+.5*a*Math.pow(t,2)));
    }
    if(finalVelocity.getText()==null && time.getText()==null){
     finalVelocity.setText(Double.toString(Math.sqrt(Math.pow(vi,2)+2*a*y)));
     time.setText(Double.toString(((Math.sqrt(Math.pow(vi,2)+2*a*y)-vi))/a));
    }
    if(finalVelocity.getText()==null && acceleration.getText()==null){
     acceleration.setText(Double.toString(2*(y-vi*t)/Math.pow(t,2)));
     finalVelocity.setText(Double.toString(vi+(2*(y-vi*t)/t)));
    }
    if(finalVelocity.getText()==null && initialVelocity.getText()==null){
     initialVelocity.setText(Double.toString((y-.5*a*Math.pow(t,2))/t));
     finalVelocity.setText(Double.toString((y-.5*a*Math.pow(t,2))/t)+a*t);
    }

   }   

  });

 }
 private double Doublify(EditText editText){
  if(editText.getText()!= null){
   return Double.parseDouble(editText.getText().toString());
  }
  return 0;
 }

}

和 XML

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical">
   <TableRow>
    <TextView android:text="Projectile Motion Engine"
        android:textSize="25dip"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_span="4"
        android:layout_gravity="center" />    
   </TableRow>
   <TableRow>
    <TextView android:text="X axis"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_span="2"
        android:textStyle="bold"/>  
    <TextView android:text="Y axis"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_span="2"
        android:textStyle="bold"/>  
   </TableRow>
   <TableRow>
    <TextView android:text="accel(m/s^2)="
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <EditText android:id="@+id/acceleration"
        android:text="9.8"
        android:inputType="numberDecimal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <TextView android:text="deltax(m)="
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <EditText android:id="@+id/deltax"
        android:inputType="numberDecimal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
   </TableRow>
   <TableRow>
    <TextView android:text="init v(m/s)="
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <EditText android:id="@+id/intitialVelocity"
        android:inputType="numberDecimal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <TextView android:text="v ="
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <EditText android:id="@+id/velocity"
        android:inputType="numberDecimal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
   </TableRow>
   <TableRow>
    <TextView android:text="final v(m/s)="
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <EditText android:id="@+id/finalVelocity"
        android:inputType="numberDecimal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <TextView android:text="time(s)="
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <EditText android:id="@+id/time"
        android:inputType="numberDecimal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
   </TableRow>
   <TableRow>
    <TextView android:text="deltay(m)="
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <EditText android:id="@+id/deltay"
        android:inputType="numberDecimal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
  <Button android:id="@+id/submitButton"
    android:layout_width="fill_parent"
      android:layout_height="wrap_content" 
      android:layout_span="2"
      android:text="Launch" />
   </TableRow>

</TableLayout>

I am fairly new to android and am writing an app that will solve physics problems. The math is going well thanks to some help from these forums, but when I try to start my activity from a list it comes up with a nullpointerexception in the onCreate Method of the new activity. This doesn't seem to make much sense though, because all thats there is a submit button that executes the math from some EditText views. Here is my code.

package android.physicsengine;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import java.lang.Math;

public class ProjectileMotion extends Activity {

 private EditText acceleration;
 private EditText finalVelocity;
 private EditText initialVelocity;
 private EditText time;
 private EditText deltay;
 private EditText velocity;
 private EditText deltax;
 private Button submitButton;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.projectile_motion_layout);

  submitButton.setOnClickListener(new OnClickListener() { 
   @Override
   public void onClick(View v) {
    acceleration = (EditText)findViewById(R.id.acceleration);
    double a = Doublify(acceleration);

    finalVelocity = (EditText)findViewById(R.id.finalVelocity);
    double vf = Doublify(finalVelocity);

    initialVelocity = (EditText)findViewById(R.id.intitialVelocity);
    double vi = Doublify(initialVelocity);

    time = (EditText)findViewById(R.id.time);
    double t = Doublify(time);

    deltay = (EditText)findViewById(R.id.deltay);
    double y = Doublify(deltay);

    velocity = (EditText)findViewById(R.id.velocity);
    double vx = Doublify(velocity);

    deltax = (EditText)findViewById(R.id.deltax);
    double x = Doublify(deltax);


    //Y Axis
    if(time.getText()==null && deltay.getText()==null){
     time.setText(Double.toString((vf-vi)/a));
     deltay.setText(Double.toString(((vf-vi)/a)+(a*Math.pow(((vf-vi)/a),2))));
    }
    if(acceleration.getText()==null && deltay.getText()==null){
     acceleration.setText(Double.toString((vf-vi)/t));
     deltay.setText(Double.toString((vi*t+.5*((vf-vi)/t))*Math.pow(t,2)));
    }
    if(acceleration.getText()==null && time.getText()==null){
     acceleration.setText(Double.toString(((Math.pow(vf,2)-Math.pow(vi,2)))/2*y));
     time.setText(Double.toString(2*y*(vf-vi)/(Math.pow(vf,2)-vi)));
    }
    if(initialVelocity.getText()==null && deltay.getText()==null){
     initialVelocity.setText(Double.toString(vf-a*t));
     deltay.setText(Double.toString((vf-a*t)*t+.5*a*Math.pow(t,2)));
    }
    if(initialVelocity.getText()==null && time.getText()==null){
     initialVelocity.setText(Double.toString(Math.sqrt(Math.pow(vf,2)-2*a*y)));
     time.setText(Double.toString((vf-Math.sqrt(Math.pow(vf,2)-2*a*y))/2));
    }
    if(initialVelocity.getText()==null && acceleration.getText()==null){
     initialVelocity.setText(Double.toString(vf-2*(vf-y/t)));
     acceleration.setText(Double.toString((2/t)*(vf-y/t)));
    }
    if(finalVelocity.getText()==null && deltay.getText()==null){
     finalVelocity.setText(Double.toString(vi+a*t));
     deltay.setText(Double.toString(vi*t+.5*a*Math.pow(t,2)));
    }
    if(finalVelocity.getText()==null && time.getText()==null){
     finalVelocity.setText(Double.toString(Math.sqrt(Math.pow(vi,2)+2*a*y)));
     time.setText(Double.toString(((Math.sqrt(Math.pow(vi,2)+2*a*y)-vi))/a));
    }
    if(finalVelocity.getText()==null && acceleration.getText()==null){
     acceleration.setText(Double.toString(2*(y-vi*t)/Math.pow(t,2)));
     finalVelocity.setText(Double.toString(vi+(2*(y-vi*t)/t)));
    }
    if(finalVelocity.getText()==null && initialVelocity.getText()==null){
     initialVelocity.setText(Double.toString((y-.5*a*Math.pow(t,2))/t));
     finalVelocity.setText(Double.toString((y-.5*a*Math.pow(t,2))/t)+a*t);
    }

   }   

  });

 }
 private double Doublify(EditText editText){
  if(editText.getText()!= null){
   return Double.parseDouble(editText.getText().toString());
  }
  return 0;
 }

}

and the XML

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical">
   <TableRow>
    <TextView android:text="Projectile Motion Engine"
        android:textSize="25dip"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_span="4"
        android:layout_gravity="center" />    
   </TableRow>
   <TableRow>
    <TextView android:text="X axis"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_span="2"
        android:textStyle="bold"/>  
    <TextView android:text="Y axis"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_span="2"
        android:textStyle="bold"/>  
   </TableRow>
   <TableRow>
    <TextView android:text="accel(m/s^2)="
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <EditText android:id="@+id/acceleration"
        android:text="9.8"
        android:inputType="numberDecimal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <TextView android:text="deltax(m)="
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <EditText android:id="@+id/deltax"
        android:inputType="numberDecimal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
   </TableRow>
   <TableRow>
    <TextView android:text="init v(m/s)="
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <EditText android:id="@+id/intitialVelocity"
        android:inputType="numberDecimal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <TextView android:text="v ="
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <EditText android:id="@+id/velocity"
        android:inputType="numberDecimal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
   </TableRow>
   <TableRow>
    <TextView android:text="final v(m/s)="
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <EditText android:id="@+id/finalVelocity"
        android:inputType="numberDecimal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <TextView android:text="time(s)="
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <EditText android:id="@+id/time"
        android:inputType="numberDecimal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
   </TableRow>
   <TableRow>
    <TextView android:text="deltay(m)="
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <EditText android:id="@+id/deltay"
        android:inputType="numberDecimal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
  <Button android:id="@+id/submitButton"
    android:layout_width="fill_parent"
      android:layout_height="wrap_content" 
      android:layout_span="2"
      android:text="Launch" />
   </TableRow>

</TableLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

翻身的咸鱼 2024-10-19 18:09:15
 super.onCreate(savedInstanceState);
  setContentView(R.layout.projectile_motion_layout);

  submitButton.setOnClickListener(new OnClickListener() { 

您忘记分配您的 submitButton(使用 findViewById

  super.onCreate(savedInstanceState);
  setContentView(R.layout.projectile_motion_layout);
  submitButton = (Button) findViewById((R.id.submitButton); // new line
  submitButton.setOnClickListener(new OnClickListener() { 
 super.onCreate(savedInstanceState);
  setContentView(R.layout.projectile_motion_layout);

  submitButton.setOnClickListener(new OnClickListener() { 

You forgot to assign your submitButton (with findViewById)

  super.onCreate(savedInstanceState);
  setContentView(R.layout.projectile_motion_layout);
  submitButton = (Button) findViewById((R.id.submitButton); // new line
  submitButton.setOnClickListener(new OnClickListener() { 
只等公子 2024-10-19 18:09:15

根据您发布的代码,您似乎调用了
提交按钮.setOnClickListener(new OnClickListener() {
在用值实例化submitButton之前。这会导致空指针

Based on the code you've posted, it appears that you call
submitButton.setOnClickListener(new OnClickListener() {
before instantiating submitButton with a value. This would cause a NullPointer

浪漫人生路 2024-10-19 18:09:15

projectile_motion_layout 可能不在您的 Android 清单中。我会先检查一下。

您可以在清单的“应用程序”选项卡下找到应用程序节点。

projectile_motion_layout may not be in your android manifest. I would check that first.

You can find the Application Nodes under the Application tab in the manifest.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文