2016-06-24 2 views
0

gonflage Je suis en train de gonfler une mise en page dans un fragment et une erreur très frustrantandroid.view.InflateException: ligne fichier XML binaire # 6: Erreur classe EditText

POURQUOI SEULEMENT erreur entraînant EDITTEXT

Mon mise en page de fragment code usb_view.xml

<?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"> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/editText" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Begin" 
     android:id="@+id/buttonStart" 
     android:layout_below="@+id/editText" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:onClick="onClickStart"/> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Send" 
     android:id="@+id/buttonSend" 
     android:onClick="onClickSend" 
     android:layout_below="@+id/editText" 
     android:layout_toRightOf="@+id/buttonStart" 
     android:layout_toEndOf="@+id/buttonStart" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView" 
     android:layout_below="@+id/buttonSend" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignRight="@+id/editText" 
     android:layout_alignEnd="@+id/editText" 
     android:layout_alignParentBottom="true" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Stop" 
     android:id="@+id/buttonStop" 
     android:layout_below="@+id/editText" 
     android:layout_toRightOf="@+id/buttonSend" 
     android:layout_toEndOf="@+id/buttonSend" 
     android:onClick="onClickStop"/> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Clear" 
     android:id="@+id/buttonClear" 
     android:layout_below="@+id/editText" 
     android:layout_toRightOf="@+id/buttonStop" 
     android:layout_toEndOf="@+id/buttonStop" 
     android:onClick="onClickClear"/> 
</LinearLayout> 

Mon code fragment Usb.java

package com.example.rajat.blueusb 
import android.app.Fragment; 
import android.app.PendingIntent; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.hardware.usb.UsbDevice; 
import android.hardware.usb.UsbDeviceConnection; 
import android.hardware.usb.UsbManager; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

import com.felhr.usbserial.UsbSerialDevice; 
import com.felhr.usbserial.UsbSerialInterface; 

import java.io.UnsupportedEncodingException; 
import java.util.HashMap; 
import java.util.Map; 

public class Usb extends Fragment { 
    public Usb(){} 


    public final String ACTION_USB_PERMISSION = "com.example.rajat.blueusb.USB_PERMISSION"; 
    Button startButton, sendButton, clearButton, stopButton; 
    TextView textView; 
    EditText editText; 
    UsbManager usbManager; 
    UsbDevice device; 
    UsbSerialDevice serialPort; 
    UsbDeviceConnection connection; 

    UsbSerialInterface.UsbReadCallback mCallback = new UsbSerialInterface.UsbReadCallback() { //Defining a Callback which triggers whenever data is read. 
     @Override 
     public void onReceivedData(byte[] arg0) { 
      String data = null; 
      try { 
       data = new String(arg0, "UTF-8"); 
       data.concat("/n"); 
       tvAppend(textView, data); 
      } catch (UnsupportedEncodingException e) { 
       e.printStackTrace(); 
      } 


     } 
    }; 
    private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { //Broadcast Receiver to automatically start and stop the Serial connection. 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      if (intent.getAction().equals(ACTION_USB_PERMISSION)) { 
       boolean granted = intent.getExtras().getBoolean(UsbManager.EXTRA_PERMISSION_GRANTED); 
       if (granted) { 
        connection = usbManager.openDevice(device); 
        serialPort = UsbSerialDevice.createUsbSerialDevice(device, connection); 
        if (serialPort != null) { 
         if (serialPort.open()) { //Set Serial Connection Parameters. 
          setUiEnabled(true); 
          serialPort.setBaudRate(9600); 
          serialPort.setDataBits(UsbSerialInterface.DATA_BITS_8); 
          serialPort.setStopBits(UsbSerialInterface.STOP_BITS_1); 
          serialPort.setParity(UsbSerialInterface.PARITY_NONE); 
          serialPort.setFlowControl(UsbSerialInterface.FLOW_CONTROL_OFF); 
          serialPort.read(mCallback); 
          tvAppend(textView,"Serial Connection Opened!\n"); 

         } else { 
          Log.d("SERIAL", "PORT NOT OPEN"); 
         } 
        } else { 
         Log.d("SERIAL", "PORT IS NULL"); 
        } 
       } else { 
        Log.d("SERIAL", "PERM NOT GRANTED"); 
       } 
      } else if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) { 
       onClickStart(startButton); 
      } else if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_DETACHED)) { 
       onClickStop(stopButton); 

      } 
     } 

     ; 
    }; 

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

     View rootView = inflater.inflate(R.layout.usb_view, container, false); 
     usbManager = (UsbManager) getActivity(). getSystemService(Context.USB_SERVICE); 
     startButton = (Button)getView(). findViewById(R.id.buttonStart); 
     sendButton = (Button)getView(). findViewById(R.id.buttonSend); 
     clearButton = (Button)getView(). findViewById(R.id.buttonClear); 
     stopButton = (Button)getView(). findViewById(R.id.buttonStop); 
     editText = (EditText)getView().findViewById(R.id.editText); 
     textView = (TextView)getView().findViewById(R.id.textView); 
     setUiEnabled(false); 
     IntentFilter filter = new IntentFilter(); 
     filter.addAction(ACTION_USB_PERMISSION); 
     filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED); 
     filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED); 
     getActivity().registerReceiver(broadcastReceiver, filter); 

     return rootView; 
    } 

    public void setUiEnabled(boolean bool) { 
     startButton.setEnabled(!bool); 
     sendButton.setEnabled(bool); 
     stopButton.setEnabled(bool); 
     textView.setEnabled(bool); 

    } 

    public void onClickStart(View view) { 

     HashMap<String, UsbDevice> usbDevices = usbManager.getDeviceList(); 
     if (!usbDevices.isEmpty()) { 
      boolean keep = true; 
      for (Map.Entry<String, UsbDevice> entry : usbDevices.entrySet()) { 
       device = entry.getValue(); 
       int deviceVID = device.getVendorId(); 

       if (deviceVID ==1659)//Arduino Vendor ID 
       { 
        PendingIntent pi = PendingIntent.getBroadcast(getActivity().getApplicationContext(), 0, new Intent(ACTION_USB_PERMISSION), 0); 
        usbManager.requestPermission(device, pi); 
        keep = false; 

        break; 
       } else { 
        connection = null; 
        device = null; 
       } 

       if (!keep) 
        break; 
      } 
     } 
    } 

    public void onClickSend(View view) { 
     String string = editText.getText().toString(); 
     serialPort.write(string.getBytes()); 
     tvAppend(textView, "\nData Sent : " + string + "\n"); 
    } 

    public void onClickStop(View view) { 
     setUiEnabled(false); 
     serialPort.close(); 
     tvAppend(textView,"\nSerial Connection Closed! \n"); 

    } 

    public void onClickClear(View view) { 
     textView.setText(" "); 
    } 

    private void tvAppend(TextView tv, CharSequence text) { 
     final TextView ftv = tv; 
     final CharSequence ftext = text; 

     getActivity().runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       ftv.append(ftext); 
      } 
     }); 
    } 

} 

Mon code MainActivity.java

package com.example.rajat.blueusb; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.view.View; 
import android.support.design.widget.NavigationView; 
import android.support.v4.view.GravityCompat; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.app.Fragment; 
import android.app.FragmentManager; 
public class MainActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawer.setDrawerListener(toggle); 
     toggle.syncState(); 

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 
    } 

    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

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

    @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(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     Fragment fragment=null; 
     int id = item.getItemId(); 

     if (id == R.id.nav_usb) { 
      fragment = new Usb(); 
     } else if (id == R.id.nav_blue) { 
      fragment = new Bluetooth(); 

     } 

     if (fragment != null) { 
      FragmentManager fragmentManager = getFragmentManager(); 
      fragmentManager.beginTransaction() 
        .replace(R.id.content_main, fragment).commit(); 
     } 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 
} 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start"> 

    <include 
     layout="@layout/app_bar_main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 
     app:headerLayout="@layout/nav_header_main" 
     app:menu="@menu/activity_main_drawer" /> 


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

LogCat:

android.view.InflateException: Binary XML file line #6: Error inflating class EditText 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
    at com.example.rajat.blueusb.Usb.onCreateView(Usb.java:109) 
    at android.app.Fragment.performCreateView(Fragment.java:2069) 
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:899) 
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1072) 
+0

S'il vous plaît afficher l'ensemble de la pile Java trace, y compris toutes les « Causé par » parties après ce que vous avez montré ici. – CommonsWare

+0

@CommonsWare \t \t Causée par: android.content.res.Resources $ NotFoundException: res de fichier/drawable-v21/abc_edit_text_material.xml de ressources drawable ID # 0x7f020015 – Rajat

Répondre

0

Vous ne pouvez pas aligner votre vue à gauche et à droite en même temps.
Si vous wan't pour aligner votre EditText à gauche:

<EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/editText" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentStart="true" /> 
+0

erreur reste le même – Rajat

0

changement import android.app.Fragment; à

import android.support.v4.app.Fragment; 
public class Usb extends Fragment 
+0

@Rajat vérifier ma réponse .... !! –

+0

Mon MainActivity.java donnant ce fragment d'erreur = new Usb(); – Rajat

+0

Erreur: (86, 24) erreur: types incompatibles: Usb ne peut pas être converti en fragment – Rajat