2016-06-13 191 views
-4
Hello fellow programmers! 
I have a very disturbing situation here...I'm developing an Android App with several activities.In them I have buttons.The app itself is for measuring calories and other sports releated stuff. 
I have a calorie calculator which I have built in the app,spread across 2 activities.In activity 1,the calculator measures the Basal Metabolic Rate and In the second activity,the app calculates the calories needed,when clicking a button for the desired activity level.My app crashes in the second activity,when clicking "Sedentary" button and I have no clue why,because these buttons are built just like the buttons in my first activity.I'll post photos and my code so you can get orientation. 
Btw,the app crashes also when clicking "Null Data" in the first activity with the BMR...The button doesn't have any coding on it and does nothing.I'm not calling calculate() method in the second activity,instead I tried doing the button click in the main method,but it still didn't work. 

您好同事! 我有一個非常令人不安的情況在這裏...我正在開發一個Android應用程序與幾個活動。在他們我有按鈕。該應用程序本身是測量卡路里和其他運動相關的東西。 我有一個卡路里計算器,我已經在應用程序中構建,分佈在2個活動中。在活動1中,計算器測量基礎代謝率,在第二個活動中,應用程序計算需要的卡路里,單擊所需的按鈕活動水平。我的應用程序在第二項活動中崩潰,點擊「久坐」按鈕時,我不知道爲什麼,因爲這些按鈕的內容與我第一次活動中的按鈕一樣。我會發布照片和代碼,以便您可以獲得方向。 順便說一句,應用程序也崩潰時,第一個活動與BMR中單擊「空數據」...該按鈕沒有任何編碼,並且什麼都不做。我沒有在第二個活動中調用calculate()方法,而是我嘗試在主方法中按下按鈕,但它仍然無法工作。 First ActivitySecond Activity單擊按鈕時,應用程序崩潰,Android Studio(Java)

`package com.petartonkov.foodandsportsinfo; 

import android.content.Intent; 
import android.os.Bundle; 
import android.os.Parcelable; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.EditText; 
import android.widget.TextView; 

public class CalorieCalculator extends AppCompatActivity { 

    public Button nextstepbtn; 
    public TextView BMR; 
    public EditText height; 
    public EditText weight; 
    public EditText age; 

    public void calculate() { 
     final Button calculateBMR = (Button) findViewById(R.id.CalcBtn); 
     final TextView BMR = (TextView) findViewById(R.id.textView_BMR); 
     final EditText weight = (EditText) findViewById(R.id.editText_weight); 
     final EditText height = (EditText) findViewById(R.id.editText_height); 
     final EditText age = (EditText) findViewById(R.id.editText_age); 
     assert calculateBMR != null; 
     calculateBMR.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       assert weight != null; 
       float weight1 = Float.parseFloat(weight.getText().toString()); 
       assert height != null; 
       float height1 = Float.parseFloat(height.getText().toString()); 
       assert age != null; 
       float age1 = Float.parseFloat(age.getText().toString()); 
       float BMR1 = (13.75f * weight1 + 5.003f * height1 - 6.755f * age1) + 66.75f; 
       assert BMR != null; 
       BMR.setText(Float.toString(BMR1)); 
      } 
     }); 
    } 
    public void calculate1() { 
     final Button calculateBMR = (Button) findViewById(R.id.CalcBtnFemale); 
     final TextView BMR = (TextView) findViewById(R.id.textView_BMR); 
     final EditText weight = (EditText) findViewById(R.id.editText_weight); 
     final EditText height = (EditText) findViewById(R.id.editText_height); 
     final EditText age = (EditText) findViewById(R.id.editText_age); 
     assert calculateBMR != null; 
     calculateBMR.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       assert weight != null; 
       float weight1 = Float.parseFloat(weight.getText().toString()); 
       assert height != null; 
       float height1 = Float.parseFloat(height.getText().toString()); 
       assert age != null; 
       float age1 = Float.parseFloat(age.getText().toString()); 
       float BMR1 = (9.563f * weight1 + 1.85f * height1 - 4.676f * age1) + 655.1f; 
       assert BMR != null; 
       BMR.setText(Float.toString(BMR1)); 
      } 
     }); 
    } 
    public void init4(){ 
     nextstepbtn = (Button)findViewById(R.id.NextStepBtn); 
     nextstepbtn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent intent1 = new Intent(getBaseContext(), CalorieCalculatorSecondActivity.class); 
       intent1.putExtra("BMR", (Parcelable) BMR); 
       startActivity(intent1); 
      } 
     }); 


    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_calorie_calculator); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
      init4(); 
calculate(); 
     calculate1(); 
    } 

} 
` 

     ` package com.petartonkov.foodandsportsinfo; 

    import android.os.Bundle; 
    import android.support.design.widget.FloatingActionButton; 
    import android.support.design.widget.Snackbar; 
    import android.support.v7.app.AppCompatActivity; 
    import android.support.v7.widget.Toolbar; 
    import android.view.View; 
    import android.widget.Button; 
    import android.widget.TextView; 

    public class CalorieCalculatorSecondActivity extends CalorieCalculator { 

    public void totalcalories() { 
     final Button calculateBtn = (Button) findViewById(R.id.button_sedentary); 
     final TextView totalcalories = (TextView) findViewById(R.id.textView_totalcalories); 
     final TextView BMR = (TextView) findViewById(R.id.textView_BMR); 
     assert calculateBtn != null; 
     calculateBtn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       assert BMR != null; 
       float BMR2 = Float.parseFloat(BMR.getText().toString()); 
       float totalcalories1 = BMR2 * 1.2f; 
       assert totalcalories != null; 
       totalcalories.setText(Float.toString(totalcalories1)); 
      } 
     }); 
    } 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_calorie_calculator_second); 
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
      setSupportActionBar(toolbar); 
      Bundle extras = getIntent().getExtras(); 
    final Button calcBtn = (Button)findViewById(R.id.button_sedentary); 
      final TextView totalcalories2 = (TextView)findViewById(R.id.textView_totalcalories); 
      final TextView BMR1 = (TextView) findViewById(R.id.textView_BMR); 
      assert calcBtn != null; 
      calcBtn.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        assert BMR1 != null; 
        float BMR3 = Float.parseFloat(BMR1.getText().toString()); 
        float totalcalories4 = BMR3 * 1.2f; 
        assert totalcalories2 != null; 
        totalcalories2.setText(Float.toString(totalcalories4)); 
       } 
      }); 
     } 
    } 
` 

`

<TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Enter your data below to calculate your BMR(Basal Metabolic Rate) and then enter your level of physical activity to determine your daily calorie needs.BMR-this number is the calories needed from the body to maintain basic living functions." 
     android:id="@+id/textView2" 
     android:textColor="#040404" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginTop="30dp" /> 

    <Button 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="go to next step" 
     android:id="@+id/NextStepBtn" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="BMR:" 
     android:id="@+id/textView11" 
     android:textColor="#070707" 
     android:layout_below="@+id/CalcBtn" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginTop="26dp" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="CAL" 
     android:id="@+id/textView13" 
     android:layout_alignTop="@+id/textView_BMR" 
     android:layout_centerHorizontal="true" 
     android:textColor="#980606" /> 

    <Button 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="NULL DATA" 
     android:id="@+id/btn_nulldata" 
     android:layout_alignBaseline="@+id/NextStepBtn" 
     android:layout_alignBottom="@+id/NextStepBtn" 
     android:layout_alignRight="@+id/textView2" 
     android:layout_alignEnd="@+id/textView2" 
     android:onClick="onCheckboxClicked"/> 


    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="numberDecimal" 
     android:ems="10" 
     android:id="@+id/editText_age" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginTop="27dp" 
     android:hint="Enter age" 
     android:layout_below="@+id/textView2" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="numberDecimal" 
     android:ems="10" 
     android:id="@+id/editText_weight" 
     android:layout_below="@+id/editText_age" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:hint="Enter weight(In KG)" 
     android:layout_alignRight="@+id/NextStepBtn" 
     android:layout_alignEnd="@+id/NextStepBtn" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="numberDecimal" 
     android:ems="10" 
     android:id="@+id/editText_height" 
     android:layout_below="@+id/editText_weight" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:hint="Enter height(In CM)" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:id="@+id/textView_BMR" 
     android:layout_alignTop="@+id/textView11" 
     android:layout_toLeftOf="@+id/NextStepBtn" 
     android:layout_toStartOf="@+id/NextStepBtn" 
     android:textColor="#980606" /> 

    <Button 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="CALCULATE BMR-MALE" 
     android:id="@+id/CalcBtn" 
     android:background="?attr/colorPrimary" 
     android:textColor="#ffffff" 
     android:layout_marginTop="31dp" 
     android:layout_below="@+id/editText_height" 
     android:layout_toLeftOf="@+id/textView13" 
     android:layout_toStartOf="@+id/textView13" /> 

    <Button 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="CALCULATE BMR-FEMALE" 
     android:id="@+id/CalcBtnFemale" 
     android:background="?attr/colorPrimary" 
     android:textColor="#ffffff" 
     android:layout_alignBottom="@+id/CalcBtn" 
     android:layout_toRightOf="@+id/textView13" 
     android:layout_toEndOf="@+id/textView13" /> 


</RelativeLayout>` 

`

<TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="SELECT YOUR LEVEL OF PHYSICAL ACTIVITY:" 
     android:id="@+id/textView10" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

    <Button 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="sedentary" 
     android:id="@+id/button_sedentary" 
     android:layout_below="@+id/textView10" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:clickable="true" 
     android:enabled="true" 
     android:onClick="setContentView" /> 

    <Button 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="moderately active" 
     android:id="@+id/button_moderatelyactive" 
     android:layout_alignTop="@+id/button_sedentary" 
     android:layout_alignRight="@+id/textView14" 
     android:layout_alignEnd="@+id/textView14" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Very active" 
     android:id="@+id/button_veryactive" 
     android:layout_below="@+id/button_sedentary" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

    <Button 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Extremely active" 
     android:id="@+id/button_extremelyactive" 
     android:layout_alignBottom="@+id/button_veryactive" 
     android:layout_alignLeft="@+id/button_moderatelyactive" 
     android:layout_alignStart="@+id/button_moderatelyactive" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:text="Sedentary - Little or no exercise,little walking" 
     android:id="@+id/textView14" 
     android:layout_below="@+id/button_veryactive" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:textColor="#e10e0e" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:text="Lighty active - light exercise 1-3 times/week + walking" 
     android:id="@+id/textView15" 
     android:layout_below="@+id/textView14" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:textColor="#e10e0e" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:text="Moderately active - exercise 3-5 times/week + walking" 
     android:id="@+id/textView16" 
     android:layout_below="@+id/textView15" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:textColor="#e10e0e" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:text="Very active - hard exercise 5-7 times/week + walking" 
     android:id="@+id/textView17" 
     android:textColor="#e10e0e" 
     android:layout_below="@+id/textView16" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="TOTAL CALORIES NEEDED:" 
     android:id="@+id/textView18" 
     android:layout_centerVertical="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:textColor="#080808" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:id="@+id/textView_totalcalories" 
     android:layout_alignTop="@+id/textView18" 
     android:layout_toRightOf="@+id/textView18" 
     android:layout_toEndOf="@+id/textView18" 
     android:editable="false" 
     android:enabled="false" 
     android:text="0" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="CAL" 
     android:id="@+id/textView20" 
     android:layout_alignBottom="@+id/textView_totalcalories" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:textColor="#090909" /> 

    <Button 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="NULL DATA" 
     android:id="@+id/btn_nulldata2" 
     android:layout_marginBottom="48dp" 
     android:layout_alignParentBottom="true" 
     android:layout_alignRight="@+id/button_sedentary" 
     android:layout_alignEnd="@+id/button_sedentary" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:text="Extra active - hard exercise 5-7 times/week + physical job + walking" 
     android:id="@+id/textView3" 
     android:textColor="#e10e0e" 
     android:layout_below="@+id/textView17" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

    <Button 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Lightly active" 
     android:id="@+id/button" 
     android:layout_alignBottom="@+id/button_extremelyactive" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:layout_toRightOf="@+id/button_extremelyactive" 
     android:layout_toEndOf="@+id/button_extremelyactive" /> 

</RelativeLayout>` 

`

                    --------- beginning of crash 
06-14 06:10:35.167 2481-2481/com.petartonkov.foodandsportsinfo E/AndroidRuntime: FATAL EXCEPTION: main 
                       Process: com.petartonkov.foodandsportsinfo, PID: 2481 
                       java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference 
                        at com.petartonkov.foodandsportsinfo.CalorieCalculatorSecondActivity$2.onClick(CalorieCalculatorSecondActivity.java:44) 
                        at android.view.View.performClick(View.java:5198) 
                        at android.view.View$PerformClick.run(View.java:21147) 
                        at android.os.Handler.handleCallback(Handler.java:739) 
                        at android.os.Handler.dispatchMessage(Handler.java:95) 
                        at android.os.Looper.loop(Looper.java:148) 
                        at android.app.ActivityThread.main(ActivityThread.java:5417) 
                        at java.lang.reflect.Method.invoke(Native Method) 
                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

`

+1

發佈你的logcat,併爲將來的問題閱讀[this](http://stackoverflow.com/help/formatting)和[this](http://stackoverflow.com/help/how-to-ask) –

+0

我會記住它的.logcat是在崩潰時發佈的。 –

+0

哪個班正在使用1st佈局和2nd佈局?哪一個是activity_calorie_calculator_second – NehaK

回答

0

這梅託德拋出

 calcBtn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       assert BMR1 != null; 
       float BMR3 = Float.parseFloat(BMR1.getText().toString()); 
       float totalcalories4 = BMR3 * 1.2f; 
       assert totalcalories2 != null; 
       totalcalories2.setText(Float.toString(totalcalories4)); 
      } 
     }); 

你沒有的TextView(BMR1)與textView_BMR的ID在你secondActivity佈局

+0

錯誤是,當我在第二個activity中調用我的對象時,它是null。它告訴我它是null。但是在第一個activity中它不是null,而是它顯示你的BMR的Textview。如何在第二個活動中獲得它,所以它不是null? –

+0

您是否想要從第二項活動中訪問第一項活動中的視圖? –

+0

Yes.But Android Studio允許我這樣做,例如。訪問第二個活動中的所有文字瀏覽。 –

0

從一個活動傳遞值到另一個可以使用意圖額外

意圖額外是您想要從一個活動傳遞到另一個活動的一系列附加信息。您可以創建額外這樣

//First Activity 
Intent i = new Intent(FirstActiviy.this, SecondActivity.class); 
String value = "some string you want to put in the extra"; 
int value2 = 14 

//put extra as key-value pairs 
i.putExtra("KEY", value); 
i.putExtra("KEY2", value2); 

startActivity(i); 


//Second activity 

//Get the values from previous Activity by keys you set 
String stringValueFromPreviousActivity = getIntent().getStringExtra("key"); 

//Note: when you getting integer as the second parameter pass the default value if there is no passed integer 
int intValueFromPreviousActiviy = getIntent().getIntExtra("key",4); 
0

我試圖在一個活動設置計算器......我rearanged按鈕和猜測什麼......?我從浮TextView的BMR爲NULL,g雖然我在BMR字符串TextView.Why是否發生這種情況?我在第一個活動中用另一種方法編寫了總卡路里的代碼。

public void calculatetotalcalories() 
    { 
     final Button calcbtn_sedentary = (Button)findViewById(R.id.button_sedentary); 
     final TextView totalcalories = (TextView)findViewById(R.id.textView_totalcalories); 
     calcbtn_sedentary.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       float BMRconverted= Float.parseFloat(String.valueOf(BMR)); 
       float totalcalories1 =BMRconverted*1.2f; 
       totalcalories.setText(Float.toString(totalcalories1)); 
      } 
     }); 
0

我解決了這個問題。它在活動開始時是一個空對象引用。現在所有的東西都可以工作!

相關問題