2011-01-20 11 views
1

私はかなりアンドロイドに慣れていて、物理的な問題を解決するアプリを書いています。これらのフォーラムの助けを借りて数学はうまくいっていますが、リストから自分のアクティビティを開始しようとすると、新しいアクティビティのonCreateメソッドでnullpointerexceptionが発生します。しかし、これはあまり意味がないようです。なぜなら、すべてのthatsには、いくつかのEditTextビューから数学を実行する送信ボタンがあるからです。ここに私のコードです。onCreate()のAndroidプログラムでNullPointerExceptionが発生しました

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> 
+3

大きなコードブロックがある場合は、コードブロックをハイライトし、 '{} 'ボタンを押してフォーマットすることができます。バッククォートを使用するのはインラインコードIIRCのみです。 – eldarerathis

+0

StackTraceをポストし、それが投げる行を特定します。 –

答えて

2

を割り当てるのを忘れ

+0

ありがとうございます。それがまさに問題でした。 –

0

projectile_motion_layoutは、Androidマニフェストではないかもしれません。私はまずそれをチェックするだろう。

アプリケーションノードは、マニフェストの[アプリケーション]タブにあります。あなたが投稿したコードに基づいて、あなたのsubmitButtonfindViewById付き)

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

申し訳ありませんが、私はまだまだこれで新しくなっています。マニフェストにレイアウトを追加するにはどうすればいいですか?私は既にアクティビティを追加しました: –

+0

マニフェストの[アプリケーション]タブの[アプリケーションノード]セクションの下にアクティビティが表示されている場合は、それを実行してください。私は、基本を除いてすべてをコメントアウトして、デバッグを開始し、そのことを確認し、そこから作業します。また、行番号をダブルクリックし、マニフェストでデバッグ可能がtrueに設定されていることを確認して、ブレークポイントを設定することもできます。 – wajiw

+0

ありがとうございますが、明らかにマニフェストに正しく登録されています。しかし、助けてくれてありがとう。 –

4
super.onCreate(savedInstanceState); 
    setContentView(R.layout.projectile_motion_layout); 

    submitButton.setOnClickListener(new OnClickListener() { 

、あなたが submitButton.setOnClickListener(新OnClickListener(){ 前に呼び出すことが表示されますsubmitButtonを値でインスタンス化するとNullPointerが発生する

関連する問題