2017-06-06 45 views
-1

我將我的應用程序中的佈局從RelativLayout更改爲RelativLayout作爲DrawerLayout的子項。一切工作正常,除onClick()MainActivity.java內的所有內容。按下按鈕button1後,計算結果爲NaN,此前工作正常。它必須對佈局的改變做些事情。我不知道是什麼原因導致此問題。 也許有人可以幫助我? GitHub的:https://github.com/ephlox/GradesCalculator2Android應用程序:更改爲DrawerLayout後,calc函數不起作用

MainActivity.java

package com.lob.gradescalculator; 

import android.content.SharedPreferences; 
import android.content.res.Configuration; 
import android.preference.PreferenceManager; 
import android.support.annotation.Nullable; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import java.text.DecimalFormat; 
import java.text.NumberFormat; 
import android.view.MenuInflater; 
import android.view.MenuItem; 


public class MainActivity extends AppCompatActivity { 

    Toolbar toolbar; 
    DrawerLayout drawerLayoutgesamt; 
    ActionBarDrawerToggle drawerToggle; 

    int sum = 0; 
    int ects = 0; 
    float mark = 0; 
    NumberFormat numberFormat = new DecimalFormat("0.00"); 
    Button button1; 
    TextView textView1; 
    EditText editText1; 
    EditText editText2; 
    EditText editText3; 

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

     toolbar = (Toolbar) findViewById(R.id.toolbar1); 
     setSupportActionBar(toolbar); 

     drawerLayoutgesamt = (DrawerLayout) findViewById(R.id.drawerlayoutgesamt); 
     drawerToggle = new ActionBarDrawerToggle(MainActivity.this, drawerLayoutgesamt, R.string.auf, R.string.zu); 
     drawerLayoutgesamt.setDrawerListener(drawerToggle); 

     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     drawerToggle.syncState(); 

     button1 = (Button)findViewById(R.id.button1); 
     textView1 = (TextView)findViewById(R.id.textView1); 
     editText1 = (EditText)findViewById(R.id.editTextGDI); 
     editText2 = (EditText)findViewById(R.id.editTextGDW); 
     editText3 = (EditText)findViewById(R.id.editTextProgrammieren1); 
     loadSavedPreferences(); 

     button1.setOnClickListener(
       new Button.OnClickListener() { 
        public void onClick(View v){ 

         ViewGroup group = (ViewGroup)findViewById(R.id.activity_main); 
         for (int i = 0, count = group.getChildCount(); i < count; ++i) { 
          View view = group.getChildAt(i); 
          if (view instanceof EditText) { 
           if(view.equals(editText1) && !(editText1.getText().toString().matches(""))){ //GDI 
            System.out.println("edittext1"); 
            ects += 5; 
            try{ sum += Integer.parseInt(editText1.getText().toString()) *5; } catch(NumberFormatException n){} 
           } 
           else if(view.equals(editText2)&& !(editText2.getText().toString().matches(""))){ //GDW 
            System.out.println("edittext2"); 
            ects += 5; 
            try{ sum += Integer.parseInt(editText2.getText().toString()) *5; } catch(NumberFormatException n){} 
           } 
           else if(view.equals(editText3)&& !(editText3.getText().toString().matches(""))){ 
            System.out.println("edittext3"); 
            ects += 7; 
            try{ sum += Integer.parseInt(editText3.getText().toString()) *7; } catch(NumberFormatException n){} 
           } 
          } 
         } 

         mark = (float)sum/(float)ects; 
         textView1.setText(String.valueOf(numberFormat.format(mark))); 
         savePreferences("editText1", editText1.getText().toString()); 
         savePreferences("editText2", editText2.getText().toString()); 
         savePreferences("editText3", editText3.getText().toString()); 

         sum = 0; 
         ects = 0; 
         mark = 0; 
        } 
       } 
     ); 
    } 

    private void loadSavedPreferences() { 
     SharedPreferences sharedPreferences = PreferenceManager 
       .getDefaultSharedPreferences(this); 
     editText1.setText(sharedPreferences.getString("editText1", "")); 
     editText2.setText(sharedPreferences.getString("editText2", "")); 
     editText3.setText(sharedPreferences.getString("editText3", "")); 
    } 

    private void savePreferences(String key, String value) { 
     SharedPreferences sharedPreferences = PreferenceManager 
       .getDefaultSharedPreferences(this); 
     SharedPreferences.Editor editor = sharedPreferences.edit(); 
     editor.putString(key, value); 
     editor.commit(); 
    } 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.menu, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item){ 
     int id = item.getItemId(); 

     if(id == R.id.action_settings){ 
      return true; 
     } 
     if(drawerToggle.onOptionsItemSelected(item)){ 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    @Override 
    protected void onPostCreate(@Nullable Bundle savedInstanceState) { 
     super.onPostCreate(savedInstanceState); 
     drawerToggle.syncState(); 
    } 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     drawerToggle.onConfigurationChanged(new Configuration()); 
    } 
} 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.lob.gradescalculator.MainActivity"> 


    <android.support.v4.widget.DrawerLayout 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent" 
     android:id="@+id/drawerlayoutgesamt" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentStart="true"> 

     <!-- Activity Layout für Hauptbereich --> 
     <RelativeLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:id="@+id/activitylayout" 
      > 

      <android.support.v7.widget.Toolbar 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/toolbar1" 
       android:background="#c6d9ff" 
       android:fitsSystemWindows="true" 
       > 
      </android.support.v7.widget.Toolbar> 

      <!-- TextViews --> 
      <TextView 
       android:id="@+id/WIF_Title" 
       android:text="Wirtschaftsinformatik B.Sc." 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="25dp" 
       android:layout_x="23dp" 
       android:layout_y="200dp" 
       android:layout_below="@+id/toolbar1" 
       android:layout_centerHorizontal="true" 
       android:layout_marginTop="10dp"/> 

      <TextView 
       android:id="@+id/textViewGDW" 
       android:text="Grundlagen der Wirtschaftsinformatik" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textAppearance="@style/TextAppearance.AppCompat.Body1" 
       android:textStyle="normal|bold" 
       android:textAlignment="textStart" 
       android:layout_below="@+id/textViewGDI" 
       android:layout_marginTop="30dp" 
       android:layout_x="45dp" 
       android:layout_y="344dp" 
       android:layout_alignStart="@+id/textViewGDI" /> 
      <TextView 
       android:id="@+id/TextViewProgrammieren1" 
       android:text="Programmieren 1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textAppearance="@style/TextAppearance.AppCompat.Body1" 
       android:textStyle="normal|bold" 
       android:textAlignment="textStart" 
       android:layout_below="@+id/textViewGDW" 
       android:layout_marginTop="32dp" 
       android:layout_x="113dp" 
       android:layout_y="238dp" 
       android:layout_alignStart="@+id/textViewGDW" /> 



      <!-- EditTexts --> 


      <!-- Buttons --> 

      <EditText 
       android:id="@+id/editTextGDI" 
       android:layout_width="50dp" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/WIF_Title" 
       android:layout_alignParentEnd="true" 
       android:layout_marginTop="10dp" 
       android:layout_marginEnd="10dp" 
       android:inputType="number" 
       android:textColorHint="@drawable/selector" 
       android:background="@drawable/roundedborders" 
       android:digits="12345" 
       android:maxLength="1" 



       /> 

      <TextView 
       android:id="@+id/textViewGDI" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 

       android:text="Grundlagen der Informatik" 
       android:textAppearance="@style/TextAppearance.AppCompat.Body1" 
       android:textStyle="normal|bold" 
       android:textAlignment="center" 
       android:layout_x="125dp" 
       android:layout_y="184dp" 
       android:layout_below="@+id/WIF_Title" 
       android:layout_alignParentStart="true" 
       android:layout_marginStart="3dp" 
       android:layout_marginTop="15dp"/> 

      <EditText 
       android:id="@+id/editTextGDW" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:inputType="number" 
       android:textColorHint="@drawable/selector" 
       android:background="@drawable/roundedborders" 
       android:ems="10" 
       android:layout_x="69dp" 
       android:layout_y="91dp" 
       android:layout_alignBaseline="@+id/textViewGDW" 
       android:layout_alignBottom="@+id/textViewGDW" 
       android:layout_alignStart="@+id/editTextGDI" 
       android:layout_alignEnd="@+id/editTextGDI" /> 

      <EditText 
       android:id="@+id/editTextProgrammieren1" 
       android:layout_height="wrap_content" 
       android:inputType="number" 
       android:textColorHint="@drawable/selector" 
       android:background="@drawable/roundedborders" 
       android:ems="10" 
       android:layout_x="78dp" 
       android:layout_y="264dp" 
       android:layout_width="50dp" 
       android:layout_alignBaseline="@+id/TextViewProgrammieren1" 
       android:layout_alignBottom="@+id/TextViewProgrammieren1" 
       android:layout_alignStart="@+id/editTextGDW" 
       android:layout_alignEnd="@+id/editTextGDW" /> 

      <TextView 
       android:id="@+id/textView1" 
       android:text="..." 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="71dp" 
       android:layout_x="23dp" 
       android:layout_y="200dp" 
       android:layout_above="@+id/button1" 
       android:layout_centerHorizontal="true" /> 

      <Button 
       android:id="@+id/button1" 
       android:text="Berechne" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="75dp" 
       android:layout_x="128dp" 
       android:layout_y="442dp" 
       android:layout_alignParentBottom="true" 
       android:layout_alignEnd="@+id/textViewGDW" /> 


     </RelativeLayout> 

     <!-- Drawerlayout für links --> 
     <RelativeLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:id="@+id/drayerlayoutsingle" 
      android:layout_gravity="start" 
      android:background="#fff"> 

     </RelativeLayout> 


    </android.support.v4.widget.DrawerLayout> 


</RelativeLayout> 
+1

你能發佈logcat的實際錯誤或日誌嗎?你的代碼看起來不錯。如果你給我具體的錯誤,那麼也許我會幫助你。 –

+0

我會如果是一些。 0錯誤,0警告,沒有什麼特別的logcat。這使我最困惑...... – lob

+0

嘗試通過調試或打印每個塊中的結果來查看所有條件,以查看問題出在哪裏。我今天忙於一些重要的工作。但是我會在隊列中添加您的問題,嘗試解決問題,否則我會在幾天內使用改進的代碼回覆您。 –

回答

1

解決它。我必須將ViewGroup group = (ViewGroup)findViewById(R.id.activity_main);更改爲ViewGroup group = (ViewGroup)findViewById(R.id.activitylayout);

相關問題