4

当我只有一个片段一(风景)的布局时,我已经有了这个工作,但现在有了附加的纵向版本,我的应用程序崩溃并在行中出现 IllegalStateExceptionswitch (v.getId())

继承人的代码:

    public void onTextViewClick(View v)
        {
            DialogFragment newFragment2 = new existingLessonDialog();
            DialogFragment newFragment = new newLessonDialog();

            if (v.getTag().equals("stunde")) newFragment2.show(getSupportFragmentManager(), "missiles");

            switch (v.getId())
            {
                case R.id.uhrzeit1:
                    index = 0;
                    newFragment.show(getSupportFragmentManager(), "missiles");
                    break;

                case R.id.uhrzeit2:
                    index = 1;
                    newFragment.show(getSupportFragmentManager(), "missiles");
                    break;

                case R.id.uhrzeit3:
                    index = 2;
                    newFragment.show(getSupportFragmentManager(), "missiles");
                    break;

                case R.id.uhrzeit4:
                    index = 3;
                    newFragment.show(getSupportFragmentManager(), "missiles");
                    break;

                case R.id.uhrzeit5:
                    index = 4;
                    newFragment.show(getSupportFragmentManager(), "missiles");
                    break;

                case R.id.uhrzeit6:
                    index = 5;
                    newFragment.show(getSupportFragmentManager(), "missiles");
                    break;

                case R.id.uhrzeit7:
                    index = 6;
                    newFragment.show(getSupportFragmentManager(), "missiles");
                    break;

                case R.id.uhrzeit8:
                    index = 7;
                    newFragment.show(getSupportFragmentManager(), "missiles");
                    break;

                case R.id.uhrzeit9:
                    index = 8;
                    newFragment.show(getSupportFragmentManager(), "missiles");
                    break;

                case R.id.uhrzeit10:
                    index = 9;
                    newFragment.show(getSupportFragmentManager(), "missiles");
                    break;

                case R.id.uhrzeit11:
                    index = 10;
                    newFragment.show(getSupportFragmentManager(), "missiles");
                    break;
            }
        }

    class newLessonDialog extends DialogFragment {
            @Override
            public Dialog onCreateDialog(Bundle savedInstanceState) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                // Get the layout inflater
                LayoutInflater inflater = getActivity().getLayoutInflater();

                // Inflate and set the layout for the dialog
                // Pass null as the parent view because its going in the dialog layout
                builder.setView(inflater.inflate(R.layout.new_lesson_dialog, null))
                        // Add action buttons,
                        .setPositiveButton("Speichern", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int id)
                            {
                                EditText eF = (EditText)getDialog().findViewById(R.id.editFach);
                                fach = eF.getText().toString();
                                EditText eR = (EditText)getDialog().findViewById(R.id.editRaum);
                                raum = eR.getText().toString();
                                EditText eL = (EditText)getDialog().findViewById(R.id.editLehrer);
                                lehrer = eL.getText().toString();

                                if (tag != null)
                                {
                                    save(fach, raum, lehrer, tag, index);
                                }
                                else
                                {
                                    Toast.makeText(getAppContext(), "Zuerst Wochentag auswählen!", Toast.LENGTH_SHORT);
                                }
                            }
                        })
                        .setNegativeButton("Abbrechen", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                newLessonDialog.this.getDialog().cancel();
                            }
                        });
                return builder.create();
            }

            public void onStart()
            {
                super.onStart();

                // This will get the radiogroup
                RadioGroup rGroup = (RadioGroup)getDialog().findViewById(R.id.radioGroup);
                // This will get the radiobutton in the radiogroup that is checked
                RadioButton checkedRadioButton = (RadioButton)rGroup.findViewById(rGroup.getCheckedRadioButtonId());

                rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
                {
                    public void onCheckedChanged(RadioGroup rGroup, int checkedId)
                    {
                        // This will get the radiobutton that has changed in its check state
                        RadioButton checkedRadioButton = (RadioButton)rGroup.findViewById(checkedId);
                        // This puts the value (true/false) into the variable
                        boolean isChecked = checkedRadioButton.isChecked();
                        // If the radiobutton that has changed in check state is now checked...
                        if (isChecked)
                        {
                            tag = checkedRadioButton.getText().toString();
                        }
                    }
                });
            }
        }

        class existingLessonDialog extends DialogFragment {
            @Override
            public Dialog onCreateDialog(Bundle savedInstanceState) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                // Get the layout inflater
                LayoutInflater inflater = getActivity().getLayoutInflater();

                // Inflate and set the layout for the dialog
                // Pass null as the parent view because its going in the dialog layout
                builder.setView(inflater.inflate(R.layout.existing_lesson_dialog, null))
                        // Add action buttons,
                        .setPositiveButton("Neue HA", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int id) {

                            }
                        })
                        .setNeutralButton("Neue KA", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int id) {

                            }
                        })
                        .setNegativeButton("Abbrechen", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                existingLessonDialog.this.getDialog().cancel();
                            }
                        });
                return builder.create();
            }
        }

和景观布局:

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity$fragmentTab1">

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

    <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/scrollView"
            android:layout_marginTop="25dp">

        <TableLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

            <TableRow
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:id="@+id/tableRow2">

                <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="07:45 - 08:30"
                        android:id="@+id/uhrzeit1"
                        android:layout_margin="5dp"
                        android:layout_column="0"
                        android:lines="2"
                        android:textSize="25sp"
                        android:clickable="true"
                        android:onClick="onTextViewClick"/>

                <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Deutsch \n in 404"
                        android:id="@+id/montag1"
                        android:layout_margin="5dp"
                        android:layout_column="1"
                        android:lines="2"
                        android:layout_weight="20"
                        android:textSize="25sp"
                        android:clickable="true"
                        android:onClick="onTextViewClick"
                        android:tag="stunde"/>

                <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="New Text"
                        android:id="@+id/dienstag1"
                        android:layout_margin="5dp"
                        android:layout_column="2"
                        android:lines="2"
                        android:layout_weight="20"
                        android:textSize="25sp"
                        android:clickable="true"
                        android:onClick="onTextViewClick"
                        android:tag="stunde"/>

                <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="New Text"
                        android:id="@+id/mittwoch1"
                        android:layout_margin="5dp"
                        android:layout_column="3"
                        android:lines="2"
                        android:layout_weight="20"
                        android:textSize="25sp"
                        android:clickable="true"
                        android:onClick="onTextViewClick"
                        android:tag="stunde"/>

                <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="New Text"
                        android:id="@+id/donnerstag1"
                        android:layout_margin="5dp"
                        android:layout_column="4"
                        android:lines="2"
                        android:layout_weight="20"
                        android:textSize="25sp"
                        android:clickable="true"
                        android:onClick="onTextViewClick"
                        android:tag="stunde"/>

                <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="New Text"
                        android:id="@+id/freitag1"
                        android:layout_margin="5dp"
                        android:layout_column="5"
                        android:lines="2"
                        android:layout_weight="20"
                        android:textSize="25sp"
                        android:clickable="true"
                        android:onClick="onTextViewClick"
                        android:tag="stunde"/>
            </TableRow>

还有 10 个这样的表行

和纵向布局:

    <RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context=".MainActivity$fragmentTab1">

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

        <ScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/scrollView">

            <TableLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent">

                <TableRow
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent">

                    <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="07:45 -\n08:30"
                            android:id="@+id/uhrzeit1p"
                            android:layout_column="0"
                            android:textIsSelectable="false"
                            android:textSize="25sp"
                            android:layout_margin="5dp"
                            android:lines="2"/>

                    <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="New Text"
                            android:id="@+id/actualLesson1"
                            android:layout_column="1"
                            android:textIsSelectable="false"
                            android:textSize="25sp"
                            android:layout_margin="5dp"
                            android:lines="2"
                            android:layout_weight="50"/>

                    <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="New Text"
                            android:id="@+id/actualNote1"
                            android:layout_column="2"
                            android:textIsSelectable="false"
                            android:textSize="25sp"
                            android:layout_margin="5dp"
                            android:lines="2"
                            android:layout_weight="50"/>
                </TableRow>

这里还有 10 个像这样的表行

最后是logcat:

    08-15 13:02:45.938      438-453/? I/ActivityManager: Displayed de.nathan.android.droidschool/.MainActivity: +6s548ms
    08-15 13:04:10.378  27691-27691/de.nathan.android.droidschool D/AndroidRuntime: Shutting down VM
    08-15 13:04:10.388  27691-27691/de.nathan.android.droidschool W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41760700)
    08-15 13:04:10.408  27691-27691/de.nathan.android.droidschool E/AndroidRuntime: FATAL EXCEPTION: main
            java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:3633)
            at android.view.View.performClick(View.java:4240)
            at android.view.View$PerformClick.run(View.java:17721)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
            Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at android.view.View$1.onClick(View.java:3628)
            ... 11 more
            Caused by: java.lang.NullPointerException
            at de.nathan.android.droidschool.MainActivity.onTextViewClick(MainActivity.java:299)
            ... 14 more
    08-15 13:04:10.428      438-665/? W/ActivityManager: Force finishing activity de.nathan.android.droidschool/.MainActivity
    08-15 13:04:10.948      438-454/? W/ActivityManager: Activity pause timeout for ActivityRecord{41ef1e68 u0 de.nathan.android.droidschool/.MainActivity}
    08-15 13:04:21.548      438-454/? W/ActivityManager: Activity destroy timeout for ActivityRecord{41ef1e68 u0 de.nathan.android.droidschool/.MainActivity}
    08-15 13:09:10.448  27691-27691/de.nathan.android.droidschool I/Process: Sending signal. PID: 27691 SIG: 9
4

3 回答 3

2
if (v.getTag().equals("stunde")) 
   newFragment2.show(getSupportFragmentManager(), "missiles")

这会在没有 commit() 的情况下更改/显示片段吗???

于 2013-08-15T12:18:48.393 回答
1

这是可能导致 NPE 的可能行: if (v.getTag().equals("stunde")) newFragment2.show(getSupportFragmentManager(), "missiles");

第一个 TextView 没有标签,而其他所有的都有。因此,要么添加标签,要么颠倒 if 的顺序:

if("stunde".equals(v.get())) {
    newFragment2.show(getSupportFragmentManager(), "missiles");
}
于 2013-08-15T12:14:08.393 回答
0

这是由于在屏幕方向更改时重新创建 xml 视图的 oncreate 方法的重复调用。您需要在方向更改时停止对 oncreate 的调用。您可以通过在清单的活动标签中添加 android:configChanges="keyboard|orientation" 来做到这一点。祝你好运

于 2013-08-15T11:44:50.320 回答