当我试图去另一个页面日食时强制关闭错误

时间:2014-01-19 14:53:09

标签: android xml eclipse

我正在做一个涉及计算的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

这是我的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>

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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);
相关问题