Solucionado (ver solução)
Solucionado
(ver solução)
7
respostas

Dúvida Aula 5 - Rodando aplicativo no celular modo landscap

Bom, estou com o codigo para mostrar a detalhesProvaFragment de maneira crua, como veio ao mundo e estou testando em um celular com android 5.0.1.

Ao coloca-lo em modo landscape e clicar em baixar provas, o aplicativo crasha e vejo o seguinte erro no stack:

10-18 15:40:54.987 18652-18652/? E/AndroidRuntime: FATAL EXCEPTION: main
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime: Process: caelum.com.br.testederegistrodecoisas, PID: 18652
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getVisibility()' on a null object reference
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.widget.LinearLayout.forceUniformHeight(LinearLayout.java:1379)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1367)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.widget.TableRow.onMeasure(TableRow.java:114)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.view.View.measure(View.java:18596)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1435)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.widget.TableLayout.measureChildBeforeLayout(TableLayout.java:464)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.widget.LinearLayout.measureVertical(LinearLayout.java:721)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.widget.TableLayout.measureVertical(TableLayout.java:476)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.widget.TableLayout.onMeasure(TableLayout.java:439)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.view.View.measure(View.java:18596)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.view.View.measure(View.java:18596)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.view.View.measure(View.java:18596)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1262)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.widget.LinearLayout.onMeasure(LinearLayout.java:614)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.view.View.measure(View.java:18596)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.view.View.measure(View.java:18596)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:447)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.view.View.measure(View.java:18596)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2897)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.view.View.measure(View.java:18596)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2248)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1306)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1548)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1191)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6642)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.view.Choreographer.doCallbacks(Choreographer.java:590)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.view.Choreographer.doFrame(Choreographer.java:560)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:145)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5942)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
10-18 15:40:54.987 18652-18652/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

Qual o problema? Lembrando que os arquivos de layout e de values estão somente com o orientation de qualificador.

7 respostas

Olá Matheus!

Você poderia postar por favor os códigos do DetalhesProvaFragment e do seu respectivo layout XML?

Assim poderemos tentar descobrir o que está acontecendo.

Obrigado!

public class DetalhesProvaFragment extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View layout = inflater.inflate(R.layout.provas_detalhe, container, false);
//
//        Bundle b = getArguments();
//        if(b != null){
//            Toast.makeText(getActivity(), "prova: "+ b.getSerializable("prova").toString(), Toast.LENGTH_SHORT).show();
//        }


//        if(getArguments() != null){
//            Prova prova = (Prova) getArguments().getSerializable("prova");
//            TextView materia = (TextView) layout.findViewById(R.id.detalhe_prova_materia);
//            materia.setText(prova.getMateria());
//            TextView data = (TextView) layout.findViewById(R.id.detalhe_prova_data);
//            data.setText(prova.getData());
//            ListView topicos = (ListView) layout.findViewById(R.id.detalhe_prova_topicos);
//
//            ArrayAdapter arrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, prova.getTopicos());
//            topicos.setAdapter(arrayAdapter);
//
//
//
//        }



        return layout;
    }
}

public class ProvasActivity extends FragmentActivity {

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.provas);

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if(isTablet()) {
            transaction.replace(R.id.provas_lista, new ListaProvasFragment());
            transaction.replace(R.id.provas_detalhe, new DetalhesProvaFragment());
        }else{
            transaction.replace(R.id.provas_view, new ListaProvasFragment());
        }
        transaction.commit();

    }
    private boolean isTablet(){
       return getResources().getBoolean(R.bool.isTablet);
    }


    public void SelecionaProva(Prova prova_selecionada) {
//        Bundle cabide = new Bundle();
//        cabide.putSerializable("prova", prova_selecionada);
//


//        FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
//        if(isTablet()){
//            tx.replace(R.id.provas_detalhe, detalhesProvaFragment);
//        }else{
//            tx.replace(R.id.provas_view, detalhesProvaFragment);
//            tx.addToBackStack(null);
//        }
//
//        tx.commit();

    }
}

Este layout é o em modo landscape.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:baselineAligned="false" >

    <FrameLayout android:id="@+id/provas_lista"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <FrameLayout android:id="@+id/provas_detalhe"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:layout_margin="20dp" />

</LinearLayout>

Olá Matheus!

Pela stack trace parece que tem algo errado com o layout do fragment de detalhes da prova (algo relacionado ao TableLayout). Os arquivos que você postou estão parecendo corretos. Precisamos agora verificar o fragment_detalhes_prova.xml. Você poderia postá-lo aqui por favor?

Abraço!

Mas é claro.

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="DETALHES DA PROVA"/>

    <TableRow >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Materia:"/>

        <TextView
            android:id="@+id/detalhe_prova_materia"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </TableRow>

    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Data:"/>

        <TextView
            android:id="@+id/detalhe_prova_data"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </TableRow>

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

        <ListView
            android:id="@+id/detalhe_prova_topicos"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_column="4" />
    </TableRow>

</TableLayout>

Olá Matheus!

Parece que está faltando definir os atributos layout_width e layout_height para os dois primeiros TableRow. Agora a exception faz sentido porque provavelmente o Android não está conseguindo definir o tamanho dos elementos ao abrir o XML do layout.

Tente adicionar esse atributos e veja se resolve!

Abraço!

infelizmente, não =/

11-07 15:10:12.312 19542-19542/? E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getVisibility()' on a null object reference
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.widget.LinearLayout.forceUniformHeight(LinearLayout.java:1379)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1367)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.widget.TableRow.onMeasure(TableRow.java:114)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.view.View.measure(View.java:18596)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1435)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.widget.TableLayout.measureChildBeforeLayout(TableLayout.java:464)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.widget.LinearLayout.measureVertical(LinearLayout.java:721)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.widget.TableLayout.measureVertical(TableLayout.java:476)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.widget.TableLayout.onMeasure(TableLayout.java:439)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.view.View.measure(View.java:18596)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.view.View.measure(View.java:18596)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.view.View.measure(View.java:18596)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1262)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.widget.LinearLayout.onMeasure(LinearLayout.java:614)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.view.View.measure(View.java:18596)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.view.View.measure(View.java:18596)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:447)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.view.View.measure(View.java:18596)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2897)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.view.View.measure(View.java:18596)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2248)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1306)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1548)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1191)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6642)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.view.Choreographer.doCallbacks(Choreographer.java:590)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.view.Choreographer.doFrame(Choreographer.java:560)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:145)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5942)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
11-07 15:10:12.312 19542-19542/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
11-07 15:10:12.632 904-19708/? E/android.os.Debug: !@Dumpstate > sdumpstate -k -t -z -d -o /data/log/dumpstate_app_error
11-07 15:10:12.863 19603-19603/? E/SMD: smd Interface open failed errno is 2 -1
11-07 15:10:13.343 19724-19724/? E/Diag_Lib:  Diag_LSM_Init: Failed to open handle to diag driver, error = 2
11-07 15:10:13.864 19603-19603/? E/SMD: smd Interface open failed errno is 2 -1
11-07 15:10:14.865 19603-19603/? E/SMD: smd Interface open failed errno is 2 -1
solução!

Olá Matheus!

O que está causando o problema é o atributo layout_column da ListView. Removendo esse atributo seu layout será renderizado mas a sua lista só ocupará o tamanho determinado pela primeira coluna. Para que a lista ocupe o espaço completo da sua tela, basta que a sua ListView seja retirada de dentro da tag TableRow como no exemplo abaixo:

<TableLayout ...>

    <!-- Os TextViews determinam as colunas da tabela então devem estar dentro do TableRow -->
    <TableRow ...>
        <TextView .../>
        <TextView .../>
    </TableRow ...>

    <!-- ListView ocupa a linha inteira então não deve estar dentro de um TableRow -->
    <ListView .../>

</TableLayout ...>

Abraço!