为什么我在意图双值时得到null

时间:2014-02-17 05:54:43

标签: java android sqlite android-intent double

我需要使用double值来插入SQLite,但是当我打印输出时它显示NULL值。

这是第一个Activity中的代码意图

    Intent intent = new Intent(Process.this,AddStudent.class);
    intent.putExtra("Intent", result);
    startActivity(intent);

这是代码在另一个活动中的意图

    String concentrate = getIntent().getStringExtra("Intent");

这是完整的代码。 FirstActivity;

public class Process extends Activity {

public static double a,b,r,std_err = 0.0;
public static double e;
public static int N;




@SuppressWarnings("static-access")
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_process);

    double[] x = { 10,30,50,60,90,55 };
    double[] y = { 1, 10, 20, 30, 40, 50 };

    Process model = new Process();
    model.Regression(x, y);
    double result = Math.pow(2.71828182845904,x[0]*b);  // e^bx 

    Intent intent = new Intent(Process.this,AddStudent.class);
    intent.putExtra("Intent", result);
    startActivity(intent);


}

//-----------------------------------------------------------------------------------//

public static void Regression (double[] oat, double[] energy) {

    N = oat.length;

    // constant e:
    Double e = Math.E;

    Double sumX = 0.00;
    Double sumX2 = 0.00;
    Double sumY = 0.00;
    Double sumYlin = 0.00;
    Double sumY2 = 0.00;
    Double sumY2lin = 0.00;
    Double sumXY = 0.00;
    Double sumXYlin = 0.00;

    for(int i=0;i<N;i++)
    {
            sumX = sumX + oat[i];
            sumX2 = sumX2 + Math.pow(oat[i], 2);

            // exponential
            sumY = sumY + Math.log(energy[i]);
            sumY2 = sumY2 + Math.pow(Math.log(energy[i]), 2);
            sumXY = sumXY + (oat[i]*(Math.log(energy[i])));

    }

    b = ((N*sumXY) - (sumX*sumY))/(N*sumX2 - (sumX*sumX));
    a = Math.pow(e, (sumY - (b*sumX))/N);


    Double c = 0.00;        // numerator
    Double d = 0.00;        // denominator

    c = (b)*(sumXY - sumX*sumY/N);
    d = sumY2 - (sumY*sumY)/N;
    r = c/d;

    Double p = 0.00;
    if(r > 0){
            p = Math.sqrt(r);
    } else {
            p = 0.00;
    }


    std_err = Math.sqrt((d-c)/(N-2));


    }
}

SecondActivity;在SQLite中添加数据的活动

public class AddStudent extends Activity {
DatabaseStudent mHelper;
SQLiteDatabase mDb;


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add);

    mHelper = new DatabaseStudent(this);
    mDb = mHelper.getWritableDatabase();

    final EditText editName = (EditText)findViewById(R.id.editName);
    final EditText editLastName = (EditText)findViewById(R.id.editLastName);
    final EditText editSchool = (EditText)findViewById(R.id.editSchool);

    Button buttonAdd = (Button)findViewById(R.id.buttonAdd);
    buttonAdd.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            String name = editName.getText().toString();
            String lastname = editLastName.getText().toString();
            String school = editSchool.getText().toString();
            //concentrate
            //String concentrate = getIntent().getStringExtra("Intent");
            String concentrate = getIntent().getStringExtra("Intent");

            //Date&Time
            java.util.Date dt = new java.util.Date();
            java.text.SimpleDateFormat sdf = 
                 new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String currentTime = sdf.format(dt);

            if(name.length() != 0 && lastname.length() != 0 
                    && school.length() != 0 ) {

                Cursor mCursor = mDb.rawQuery("SELECT * FROM " 
                        + DatabaseStudent.TABLE_NAME + " WHERE " 
                        + DatabaseStudent.COL_NAME + "='" + name + "'" 
                        + " AND " + DatabaseStudent.COL_LASTNAME + "='" 
                        + concentrate + "'" + " AND " 
                        + DatabaseStudent.COL_SCHOOL + "='" + currentTime //add COL_SCHOOL = currentTime
                        + "'", null);

                if(mCursor.getCount() == 0) {
                    mDb.execSQL("INSERT INTO " + DatabaseStudent.TABLE_NAME 
                            + " (" + DatabaseStudent.COL_NAME 
                            + ", " + DatabaseStudent.COL_LASTNAME  
                            + ", " + DatabaseStudent.COL_SCHOOL 
                            + ") VALUES ('" + name + "', '" + concentrate //result ไม่มา
                            + "', '" + currentTime + "');"); 



                    editName.setText("");
                    editLastName.setText("");
                    editSchool.setText("");

        }
    });
}

public void onStop() {
    super.onStop();
    mHelper.close();
    mDb.close();
}

}

5 个答案:

答案 0 :(得分:2)

你有

   double result = Math.pow(2.71828182845904,x[0]*b); 

   intent.putExtra("Intent", result);

resultdouble

但是当你找回时有

   String concentrate = getIntent().getStringExtra("Intent"); 

相反,你应该使用

   double concentrate = getIntent().getDoubleExtra("Intent", 0);

参考:

http://developer.android.com/reference/android/content/Intent.html#getDoubleExtra(java.lang.String,double)

答案 1 :(得分:1)

您实际上并未将双重值放入Intent。您需要使用putDoublegetDouble方法。

Intent intent = new Intent(Process.this,AddStudent.class);
Bundle b = new Bundle();
b.putDouble("Intent", result);
intent.putExtras(b);
startActivity(intent);

然后,在下一个活动中获取它:

Bundle b = getIntent().getExtras();
double result = b.getDouble("Intent");

答案 2 :(得分:1)

String concentrate = getIntent().getStringExtra("Intent"); 

应该是这样的:

double concentrate = getIntent().getExtras().getDouble("Intent");

将传递的包直接包装成double。

答案 3 :(得分:0)

您正在传递Double值并尝试获取String值,因此您将获得空值

替换

getIntent().getStringExtra("Intent");

getIntent().getDoubleExtra("Intent", 0);

答案 4 :(得分:0)

尝试将你的双重放入一个包中,然后像这样传递

Intent intent = new Intent(Process.this,AddStudent.class);
Bundle extras = new Bundle();
intent.putExtras(b);
startActivity(intent);

然后,在您的新活动中

 Bundle extras = Intent.getExtras();
 Double result = extras.getDouble("Intent");