Android ARToolkit — изображение с ошибкой

Приложение работает хорошо, но после того, как я поместил setContentView (R.layout.activity_main) в свой класс, чтобы установить содержимое и показать изображение, мне сообщает об ошибке:

android.view.InflateException: Binary XML file line #2: Error inflating class android.support.design.widget.CoordinatorLayout

Я унаследован от AndARActivity, чтобы сделать простое приложение для распознавания маркеров.

XML с проблемой таков:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:fitsSystemWindows="true"
      tools:context="webdata.com.mascotcollector.MainActivity">

      <android.support.design.widget.AppBarLayout
          android:layout_height="wrap_content"
          android:layout_width="match_parent"
          android:theme="@style/AppTheme.AppBarOverlay">

          <android.support.v7.widget.Toolbar
              android:id="@+id/toolbar"
              android:layout_width="match_parent"
              android:layout_height="?attr/actionBarSize"
              android:background="?attr/colorPrimary"
              app:popupTheme="@style/AppTheme.PopupOverlay" />

      </android.support.design.widget.AppBarLayout>

      <include layout="@layout/content_main"/>

      <android.support.design.widget.FloatingActionButton
          android:id="@+id/fab"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="bottom|end"
          android:layout_margin="@dimen/fab_margin"
          app:srcCompat="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

Что я делаю неправильно?


person Doro    schedule 13.01.2017    source источник
comment
Опубликуйте свои файлы Activity и gradle   -  person Pztar    schedule 13.01.2017


Ответы (2)


Убедитесь, что у вас есть правильная зависимость в файле gradle

compile 'com.android.support:design:25.0.1'
person Pztar    schedule 13.01.2017

MainActivity.java (setContentView(R.layout.activity_main); это инструкция с проблемой):

package webdata.com.mascotcollector;

/**
 * Created by ilie on 11.11.2016.
 */

import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.Random;

import edu.dhbw.andar.ARToolkit;
import edu.dhbw.andar.AndARActivity;
import edu.dhbw.andar.exceptions.AndARException;

public class MainActivity extends AndARActivity {
    private static final String TAG = "MainActivity";

    private View m_RootLayout;

    private TextView m_MessageTextView;
    private TextView m_XPosLabel;
    private TextView m_YPosLabel;
    private TextView m_ZPosLabel;
    private TextView m_MarkerNameLabel;
    private TextView m_ScorePosLabel;
    private TextView m_XPosValue;
    private TextView m_YPosValue;
    private TextView m_ZPosValue;
    private TextView m_MarkerNameValue;
    private TextView m_ScorePosValue;

    private ARToolkit m_ARToolkit;
    private Cube[] cubesArr = new Cube[6];
    private Utils utils = new Utils();

    private double[] centerMarker = {0, 0};
    private float[] grayColor = {0.5f, 0.5f, 0.5f, 1.0f},
                    redColor = {1.0f, 0f, 0f, 1.0f},
                    yellowColor = {1.0f, 0.8f, 0.0f, 1.0f},
                    greenColor = {0.0f, 0.6f, 0.0f, 1.0f},
                    blackColor = {0.0f, 0.0f, 0.0f, 1.0f};
    private double markerWidth = 80.0;
    int score = 0;
    private Random rand = new Random();
    private int randInt = 0;

    ImageView mImageView;

    // Dupa ce se elibereaza click-ul lung, se intra pe un click scurt, ceea ce necesita
    // introducerea unei variabile de control (false - click lung; true - click scurt)
    private boolean longClick = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        m_RootLayout = findViewById(android.R.id.content);
        //m_RootLayout.setOnTouchListener(this);
        //setContentView(R.layout.activity_main);

        m_RootLayout.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                longClick = true;
                for (int i = 0; i < cubesArr.length; i++) {
                    if (cubesArr[i].isVisible() && cubesArr[i].getMarkerName().equals(m_MarkerNameValue.getText().toString())) {
                        if (cubesArr[i].getColor() != blackColor) {
                            // Daca nu este o capcana stabilita anterior, atunci transforma cubul in capcana.
                            cubesArr[i].setTrap(true);
                            cubesArr[i].setColor(blackColor);
                        } else {
                            // Daca este o capcana stabilita anterior, atunci scade punctajul jucatorului si
                            // elimina capcana.
                            score -= 20;
                            cubesArr[i].setTrap(false);
                            cubesArr[i].setColor(grayColor);
                            System.out.println("A fost o capcana stabilita anterior!");
                        }
                    }
                }
                return false;
            }
        });

        m_RootLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (longClick == false) {
                    for (int i = 0; i < cubesArr.length; i++) {
                        if (cubesArr[i].isVisible() && cubesArr[i].getMarkerName().equals(m_MarkerNameValue.getText().toString())) {
                            if (!cubesArr[i].isTrap()) {
                                // Daca nu este capcana, la o atingere a cubului stabileste un punctaj pentru
                                // jucator in functie de culoarea care iese.
                                if (cubesArr[i].getColor() == grayColor) {
                                    randInt = rand.nextInt(3);

                                    if (randInt == 2) {
                                        cubesArr[i].setColor(greenColor);
                                        score += 5;
                                    } else if (randInt == 1) {
                                        cubesArr[i].setColor(yellowColor);
                                    } else {
                                        cubesArr[i].setColor(redColor);
                                        score -= 5;
                                    }
                                }
                            } else {
                                // Daca este o capcana, scade punctajul jucatorului cu 20 de unitati.
                                score -= 20;
                                cubesArr[i].setTrap(false);
                                cubesArr[i].setColor(grayColor);
                                System.out.println("A fost o capcana!");
                            }
                        }
                    }
                }
                else {
                    System.out.println("ESTE LONG");
                    longClick = false;
                }
            }
        });

        CreateLayout((FrameLayout)m_RootLayout);
        CustomRenderer renderer = new CustomRenderer();
        setNonARRenderer(renderer);


        //mImageView.setImageResource(R.drawable.pisica);

        try {
            m_ARToolkit = getArtoolkit();

            cubesArr[0] = new Cube("ACS_Cube", "marker16_acs.patt", markerWidth, centerMarker, grayColor, "marker16_acs.patt");
            cubesArr[1] = new Cube("ARTest_Cube", "marker16_artest.patt", markerWidth, centerMarker, grayColor, "marker16_artest.patt");
            cubesArr[2] = new Cube("DSRV_Cube", "marker16_dsrv.patt", markerWidth, centerMarker, grayColor, "marker16_dsrv.patt");
            cubesArr[3] = new Cube("UPB_Cube", "marker16_upb.patt", markerWidth, centerMarker, grayColor, "marker16_upb.patt");
            cubesArr[4] = new Cube("Cube_1", "marker16_cube1.patt", markerWidth, centerMarker, grayColor, "marker16_cube1.patt");
            cubesArr[5] = new Cube("Cube_2", "marker16_cube2.patt", markerWidth, centerMarker, grayColor, "marker16_cube2.patt");

            for (int i = 0; i < cubesArr.length; i++) {
                m_ARToolkit.registerARObject(cubesArr[i]);
            }
        } catch (AndARException ex) {
            Log.e(TAG, "AndAR EXCEPTION: " + ex.getMessage());
        }

        startPreview();

        new Thread(new Runnable() {
            public void run() {
                while(true) {
                    try {
                        Thread.sleep(30);

                        m_RootLayout.post(new Runnable() {
                            public void run() {
                                boolean objectVisible = false;

                                for (int i = 0; i < cubesArr.length; i++) {
                                    if (cubesArr[i].isVisible()) {
                                        objectVisible = utils.chkCube(cubesArr[i], objectVisible,
                                                        m_XPosValue, m_YPosValue, m_ZPosValue, m_MarkerNameValue, m_MessageTextView);
                                    } else {
                                        if (!cubesArr[i].isTrap()) {
                                            cubesArr[i].setColor(grayColor);
                                        } else {
                                            cubesArr[i].setColor(blackColor);
                                        }
                                    }
                                }

                                if (!objectVisible) {
                                    m_XPosValue.setText(R.string.emptyString);
                                    m_YPosValue.setText(R.string.emptyString);
                                    m_ZPosValue.setText(R.string.emptyString);
                                    m_MarkerNameValue.setText(R.string.emptyString);
                                    m_MessageTextView.setText("");
                                }

                                m_ScorePosValue.setText(String.valueOf(score));
                            }
                        });
                    } catch (Exception e) {
                        System.out.println("EXCEPTION !!!");
                    }
                }
            }
        }).start();



        //setContentView(R.layout.content_main);
        //TODO: de revizuit activity_main.xml
        setContentView(R.layout.activity_main);
        //mImageView = (ImageView) findViewById(R.id.imageView);
        //mImageView.setImageResource(R.drawable.pisica);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    private void CreateLayout(FrameLayout rootLayout) {
        float densityDpi = this.getResources().getDisplayMetrics().density;
        int marginPixel = (int)densityDpi*5;

        LinearLayout labelsLayout = new LinearLayout(this);
        LinearLayout.LayoutParams layoutParamsRoot = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        labelsLayout.setOrientation(LinearLayout.VERTICAL);
        labelsLayout.setLayoutParams(layoutParamsRoot);

        LinearLayout.LayoutParams layoutParams;

        //START set xPos
        m_XPosLabel = new TextView(this);
        layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(marginPixel, 0, 0, 0);
        m_XPosLabel.setLayoutParams(layoutParams);
        m_XPosLabel.setText(R.string.xPosLabel);

        m_XPosValue = new TextView(this);
        layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(marginPixel, 0, 0, 0);
        m_XPosValue.setLayoutParams(layoutParams);
        m_XPosValue.setText(R.string.emptyString);

        LinearLayout xlabelsLayout = new LinearLayout(this);
        xlabelsLayout.setOrientation(LinearLayout.HORIZONTAL);
        layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(marginPixel, 0, 0, 0);
        xlabelsLayout.setLayoutParams(layoutParams);
        xlabelsLayout.addView(m_XPosLabel);
        xlabelsLayout.addView(m_XPosValue);
        //END set xPos

        //START set yPos
        m_YPosLabel = new TextView(this);
        layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(marginPixel, 0, 0, 0);
        m_YPosLabel.setLayoutParams(layoutParams);
        m_YPosLabel.setText(R.string.yPosLabel);

        m_YPosValue = new TextView(this);
        layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(marginPixel, 0, 0, 0);
        m_YPosValue.setLayoutParams(layoutParams);
        m_YPosValue.setText(R.string.emptyString);

        LinearLayout ylabelsLayout = new LinearLayout(this);
        ylabelsLayout.setOrientation(LinearLayout.HORIZONTAL);
        layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(marginPixel, 0, 0, 0);
        ylabelsLayout.setLayoutParams(layoutParams);
        ylabelsLayout.addView(m_YPosLabel);
        ylabelsLayout.addView(m_YPosValue);
        //END set yPos

        //START set zPos
        m_ZPosLabel = new TextView(this);
        layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(marginPixel, 0, 0, 0);
        m_ZPosLabel.setLayoutParams(layoutParams);
        m_ZPosLabel.setText(R.string.zPosLabel);

        m_ZPosValue = new TextView(this);
        layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(marginPixel, 0, 0, 0);
        m_ZPosValue.setLayoutParams(layoutParams);
        m_ZPosValue.setText(R.string.emptyString);

        LinearLayout zlabelsLayout = new LinearLayout(this);
        zlabelsLayout.setOrientation(LinearLayout.HORIZONTAL);
        layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(marginPixel, 0, 0, 0);
        zlabelsLayout.setLayoutParams(layoutParams);
        zlabelsLayout.addView(m_ZPosLabel);
        zlabelsLayout.addView(m_ZPosValue);
        //END set zPos

        //START set marker name
        m_MarkerNameLabel = new TextView(this);
        layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(marginPixel, 0, 0, 0);
        m_MarkerNameLabel.setLayoutParams(layoutParams);
        m_MarkerNameLabel.setText(R.string.markerNameLabel);

        m_MarkerNameValue = new TextView(this);
        layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(marginPixel, 0, 0, 0);
        m_MarkerNameValue.setLayoutParams(layoutParams);
        m_MarkerNameValue.setText(R.string.emptyString);

        LinearLayout markerNamelabelsLayout = new LinearLayout(this);
        markerNamelabelsLayout.setOrientation(LinearLayout.HORIZONTAL);
        layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(marginPixel, 0, 0, 0);
        markerNamelabelsLayout.setLayoutParams(layoutParams);
        markerNamelabelsLayout.addView(m_MarkerNameLabel);
        markerNamelabelsLayout.addView(m_MarkerNameValue);
        //END set marker name

        //START set score
        m_ScorePosLabel = new TextView(this);
        layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(marginPixel, 0, 0, 0);
        m_ScorePosLabel.setLayoutParams(layoutParams);
        m_ScorePosLabel.setText(R.string.scoreLabel);

        m_ScorePosValue = new TextView(this);
        layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(marginPixel, 0, 0, 0);
        m_ScorePosValue.setLayoutParams(layoutParams);
        m_ScorePosValue.setText(R.string.emptyString);

        LinearLayout scorelabelsLayout = new LinearLayout(this);
        scorelabelsLayout.setOrientation(LinearLayout.HORIZONTAL);
        layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(marginPixel, 0, 0, 0);
        scorelabelsLayout.setLayoutParams(layoutParams);
        scorelabelsLayout.addView(m_ScorePosLabel);
        scorelabelsLayout.addView(m_ScorePosValue);
        //END set score

        //END set msg
        m_MessageTextView = new TextView(this);
        layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(marginPixel, 0, 0, 0);
        m_MessageTextView.setLayoutParams(layoutParams);
        //END set msg

        //Add to view
        labelsLayout.addView(xlabelsLayout);
        labelsLayout.addView(ylabelsLayout);
        labelsLayout.addView(zlabelsLayout);
        labelsLayout.addView(markerNamelabelsLayout);
        labelsLayout.addView(scorelabelsLayout);
        labelsLayout.addView(m_MessageTextView);

        rootLayout.addView(labelsLayout);
    }

    @Override
    public void uncaughtException(Thread t, Throwable ex) {
        Log.e(TAG, "ViewWorldSample EXCEPTION:" + ex.getMessage());
        finish();
    }
}

Файл градиента (я пробовал с 25.0.1 и 25.1.0 безрезультатно):

apply plugin: 'com.android.application'

android {
      compileSdkVersion 25
      buildToolsVersion "25.0.0"
      defaultConfig {
          applicationId "webdata.com.mascotcollector"
          minSdkVersion 19
          targetSdkVersion 25
          versionCode 1
          versionName "1.0"
          testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
      }
      buildTypes {
          release {
              minifyEnabled false
              proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
          }
      }
}

dependencies {
      compile fileTree(include: ['*.jar'], dir: 'libs')
      androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
          exclude group: 'com.android.support', module: 'support-annotations'
      })
      compile 'com.android.support:appcompat-v7:25.0.0'
      compile 'com.android.support:design:25.0.0'
      testCompile 'junit:junit:4.12'
      compile files('libs/AndAR.jar')
}
person Doro    schedule 13.01.2017