2014-01-19 55 views
0

我正在做一個涉及計算的Android應用程序。 現在,我只是做第一部分,試圖登錄。 我創建了一個數據庫,爲用戶註冊存儲信息,以便當用戶註冊時,信息將存儲在數據庫中,這將被用於登錄。當我試圖去另一個頁面日食時強制關閉錯誤

我登錄後,會顯示另一個頁面,它是activity_main.xml 但是,當我嘗試登錄時,出現強制關閉錯誤。

以前,當我嘗試過,沒有錯誤,我可以成功登錄。 但是,在MainActivity.java中添加代碼並嘗試登錄後,發生錯誤的起始日期爲 。

有人可以幫我一把。 我是處理日食的新手。

這是我的logcat錯誤

01-20 05:02:44.773: E/AndroidRuntime(1159): FATAL EXCEPTION: main 
01-20 05:02:44.773: E/AndroidRuntime(1159): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fuellog/com.example.fuellog.MainActivity}: java.lang.NumberFormatException: Invalid float: "floatvalue" 
01-20 05:02:44.773: E/AndroidRuntime(1159):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
01-20 05:02:44.773: E/AndroidRuntime(1159):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
01-20 05:02:44.773: E/AndroidRuntime(1159):  at android.app.ActivityThread.access$600(ActivityThread.java:141) 
01-20 05:02:44.773: E/AndroidRuntime(1159):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
01-20 05:02:44.773: E/AndroidRuntime(1159):  at android.os.Handler.dispatchMessage(Handler.java:99) 
01-20 05:02:44.773: E/AndroidRuntime(1159):  at android.os.Looper.loop(Looper.java:137) 
01-20 05:02:44.773: E/AndroidRuntime(1159):  at android.app.ActivityThread.main(ActivityThread.java:5041) 
01-20 05:02:44.773: E/AndroidRuntime(1159):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-20 05:02:44.773: E/AndroidRuntime(1159):  at java.lang.reflect.Method.invoke(Method.java:511) 
01-20 05:02:44.773: E/AndroidRuntime(1159):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
01-20 05:02:44.773: E/AndroidRuntime(1159):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
01-20 05:02:44.773: E/AndroidRuntime(1159):  at dalvik.system.NativeStart.main(Native Method) 
01-20 05:02:44.773: E/AndroidRuntime(1159): Caused by: java.lang.NumberFormatException: Invalid float: "floatvalue" 
01-20 05:02:44.773: E/AndroidRuntime(1159):  at java.lang.StringToReal.invalidReal(StringToReal.java:63) 
01-20 05:02:44.773: E/AndroidRuntime(1159):  at java.lang.StringToReal.initialParse(StringToReal.java:94) 
01-20 05:02:44.773: E/AndroidRuntime(1159):  at java.lang.StringToReal.parseFloat(StringToReal.java:304) 
01-20 05:02:44.773: E/AndroidRuntime(1159):  at java.lang.Float.parseFloat(Float.java:300) 
01-20 05:02:44.773: E/AndroidRuntime(1159):  at com.example.fuellog.MainActivity.onCreate(MainActivity.java:39) 
01-20 05:02:44.773: E/AndroidRuntime(1159):  at android.app.Activity.performCreate(Activity.java:5104) 
01-20 05:02:44.773: E/AndroidRuntime(1159):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
01-20 05:02:44.773: E/AndroidRuntime(1159):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
01-20 05:02:44.773: E/AndroidRuntime(1159):  ... 11 more 

這是我homeactivity.java

public class homeActivity extends Activity{ 
    Button btnSignIn,btnSignUp; 
    Button btnAbout; 
    LoginDataBaseAdapter loginDataBaseAdapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.homescreen); 
     // create a instance of SQLite Database 
     loginDataBaseAdapter=new LoginDataBaseAdapter(this); 
     loginDataBaseAdapter=loginDataBaseAdapter.open(); 

     // Get The Reference Of Buttons 
     btnSignIn=(Button)findViewById(R.id.buttonSignIN); 
     btnSignUp=(Button)findViewById(R.id.buttonSignUP); 
     btnAbout=(Button)findViewById(R.id.aboutBTN); 

     // Set OnClick Listener on SignUp button 
      btnSignUp.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

       /// Create Intent for SignUpActivity abd Start The Activity 
       Intent intentSignUP=new Intent(getApplicationContext(),SignUpActivity.class); 
       startActivity(intentSignUP); 
       } 
      }); 



      // set OnClick Listener on About button 
      btnAbout.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 

        /// Create Intent for About and start activity 
        Intent intentAbout=new Intent(getApplicationContext(),about.class); 
        startActivity(intentAbout); 
        } 
       }); 


     } 
    // Methods to handleClick Event of Sign In Button 
     public void signIn(View V) 
      { 
       final Dialog dialog = new Dialog(homeActivity.this); 
       dialog.setContentView(R.layout.login); 
       dialog.setTitle("Login"); 

       // get the Refferences of views 
       final EditText editTextUserName=(EditText)dialog.findViewById(R.id.editTextUserNameToLogin); 
       final EditText editTextPassword=(EditText)dialog.findViewById(R.id.editTextPasswordToLogin); 

       Button btnSignIn=(Button)dialog.findViewById(R.id.buttonSignIn); 

       // Set On ClickListener 
       btnSignIn.setOnClickListener(new View.OnClickListener() { 

        public void onClick(View v) { 
         // get The User name and Password 
         String userName=editTextUserName.getText().toString(); 
         String password=editTextPassword.getText().toString(); 

         // fetch the Password form database for respective user name 
         String storedPassword=loginDataBaseAdapter.getSinlgeEntry(userName); 

         // check if the Stored password matches with Password entered by user 
         if(password.equals(storedPassword)) 
         { 
          Toast.makeText(homeActivity.this, "Welcome," + userName, Toast.LENGTH_LONG).show(); 
          dialog.dismiss(); 
          Intent mainact=new Intent(getApplicationContext(),MainActivity.class); 
          mainact.putExtra("priceEdit", "floatvalue"); 
          mainact.putExtra("pumpEdit", "floatvalue"); 
          startActivity(mainact); 


         } 
         else 
         { 
          Toast.makeText(homeActivity.this, "User Name or Password does not match", Toast.LENGTH_LONG).show(); 
         } 
        } 
       }); 

       dialog.show(); 
     } 

     @Override 
     protected void onDestroy() { 
      super.onDestroy(); 
      // Close The Database 
      loginDataBaseAdapter.close(); 
     } 

} 

代碼這是我MainActivity.java

public class MainActivity extends Activity { 

    Button saveButton = null; 
    EditText dateEdit; 
    EditText priceEdit; 
    EditText pumpEdit; 
    TextView costView; 
    EditText odometerEdit; 
    TextView fconView; 




    public boolean isNumeric(String str) 
    { 
     return str.matches("-?\\d+(\\.\\d+)?"); 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 




     setContentView(R.layout.activity_main); 
     Bundle bundle = getIntent().getExtras(); 
     float price = Float.parseFloat(bundle.getString("priceEdit")); 
     float pump = Float.parseFloat(bundle.getString("pumpEdit")); 
     final float costView = (price * pump); 
     dateEdit = (EditText)findViewById(R.id.date); 
     priceEdit = (EditText)findViewById(R.id.fuelprice); 
     pumpEdit = (EditText)findViewById(R.id.fuelpump); 

     saveButton = (Button) findViewById(R.id.saveBTN); 
     saveButton.setOnClickListener(new OnClickListener() 
     { 
      public void onClick(View v) 
      { 
       DBAdapter dbAdaptor = new DBAdapter(getApplicationContext()); 
       try 
       { 
        dbAdaptor.open(); 
        String date = dateEdit.getText().toString(); 
        String price = priceEdit.getText().toString(); 
        String pump = pumpEdit.getText().toString(); 
        String cost = Float.toString(costView); 
        String odometer = odometerEdit.getText().toString(); 
        String fcon = fconView.getText().toString(); 
        dbAdaptor.insertLog(date, price, pump, cost, odometer, fcon); 

       } 
       catch(Exception e){ 
        Log.d("Fuel Log", e.getMessage()); 
       } 
       finally 
       { 
        if(dbAdaptor != null) 
         dbAdaptor.close(); 
       } 
      } 
     }); 

    }//oncreate 


}//main 

代碼這是我的代碼for homescreen.xml

<LinearLayout 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:orientation="vertical" 

    android:gravity="center_vertical" 
    > 


    <Button 
     android:id="@+id/buttonSignIN" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Sign In" 
     android:onClick="signIn"/> 

    <Button 
     android:id="@+id/buttonSignUP" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Sign Up" /> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/home" /> 
    <Button 
     android:id="@+id/aboutBTN" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="About" /> 

</LinearLayout> 

這是我activity_main.xml中的代碼

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:orientation="vertical" 
    android:layout_height="fill_parent" 
    tools:context=".MainActivity" > 


    <TableLayout 
     android:id="@+id/tableLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:stretchColumns="1"> 

     <TableRow 
      android:id="@+id/tableRow1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 
      <TextView 
       android:id="@+id/datetxtview" 
       android:text="@string/date" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
      </TextView> 
       <EditText 
       android:id="@+id/date" 
       android:text="" 
       android:inputType="date" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
      </EditText> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow2" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" > 
      <TextView 
       android:id="@+id/fuelpricetxtview" 
       android:text="@string/fuelprice" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
      </TextView> 
      <EditText 
       android:id="@+id/fuelprice" 
       android:text="" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
      </EditText> 
     </TableRow> 
     <TableRow 
      android:id="@+id/tableRow3" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" > 
      <TextView 
       android:id="@+id/fuelpumptxtview" 
       android:text="@string/fuelpump" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
      </TextView> 
      <EditText 
       android:id="@+id/fuelpump" 
       android:text="" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
      </EditText> 
     </TableRow> 


      <TableRow 
      android:id="@+id/tableRow4" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" > 
      <TextView 
       android:id="@+id/totalcosttxtview" 
       android:text="@string/totalcost" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
      </TextView> 

      <TextView 
       android:id="@+id/tcost" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="" /> 

     </TableRow> 



     <TableRow 
      android:id="@+id/tableRow5" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" > 
      <TextView 
       android:id="@+id/odometertxtview" 
       android:text="@string/odometer" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
      </TextView> 

      <EditText 
       android:id="@+id/odometer" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:ems="10" > 

       <requestFocus /> 
      </EditText> 

     </TableRow> 
     <TableRow 
      android:id="@+id/tableRow6" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" > 
      <TextView 
       android:id="@+id/fctxtview" 
       android:text="@string/fc" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
      </TextView> 

      <TextView 
       android:id="@+id/fcon" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="" /> 

     </TableRow>  
     </TableLayout> 


    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 

     <Button 
      android:id="@+id/saveBTN" 
      android:text="@string/save" 
      android:layout_width="wrap_content" 
      android:layout_height="60px" > 
     </Button> 

     <Button 
      android:id="@+id/cancelBTN" 
      android:text="@string/cancel" 
      android:layout_width="wrap_content" 
      android:layout_height="60px" > 
     </Button> 
    </LinearLayout> 



</LinearLayout> 
+0

'MainActivity.java'的第35行是什麼? –

+0

您在哪裏發送'priceEdit'和'pumpEdit'到MainActivity? –

+0

@ZouZou float price = Float.parseFloat(bundle.getString(「priceEdit」));這是行 – Chloe

回答

3

你有

Intent mainact=new Intent(getApplicationContext(),MainActivity.class); 
startActivity(mainact); 

您正在使用意圖

變化不傳遞任何值MainActivity到

Intent mainact=new Intent(HomeActivity.this,MainActivity.class); 
mainact.putExtra("priceEdit","floatvalue"); 
mainact.putExtra("pumpEdit","floatvalue"); 
startActivity(mainact); 

您尚未初始化dateEdit,priceEdit,pumpEdit

setContentView(R.layout.activity_main); 
Bundle bundle = getIntent().getExtras(); 
float price = Float.parseFloat(bundle.getString("priceEdit")); 
float pump = Float.parseFloat(bundle.getString("pumpEdit")); 
final float costView = (price * pump); 
dateEdit= (EditText)findViewById(R.id.date); 
priceEdit =(EditText)findViewById(R.id.fuelprice); 
pumpEdit =(EditText)findViewById(R.id.fuelpump); 
+0

還有錯誤。 在logcat中的無效浮點數值 – Chloe

+0

@ user3210263您應該發送一個有效的浮點值,如23.4 – Raghunandan

+0

我的意思是仍然有強制關閉錯誤,我嘗試登錄後。 – Chloe

0

在mainactivity.java中更改代碼,如下所示。所有初始化必須發生在'setContentView(R.layout.activity_main);'

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Bundle bundle = getIntent().getExtras(); 
     float price = Float.parseFloat(bundle.getString("priceEdit")); 
     float pump = Float.parseFloat(bundle.getString("pumpEdit")); 
     final float costView = (price * pump); 
相關問題