Android应用程序:更改为DrawerLayout后,calc函数不起作用

时间:2017-06-06 17:59:54

标签: java android

我作为DrawerLayout的孩子,将我的应用中的布局从RelativLayout更改为RelativLayout。一切正常,但onClick()MainActivity.java内的所有内容除外。按下按钮button1后,计算结果为NaN,之前正在运行。它必须对布局的变化做些什么。我不知道导致这个问题的原因。 也许有人可以帮助我? GitHub:https://github.com/ephlox/GradesCalculator2

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 个答案:

答案 0 :(得分:1)

解决了它。我必须将ViewGroup group = (ViewGroup)findViewById(R.id.activity_main);更改为ViewGroup group = (ViewGroup)findViewById(R.id.activitylayout);