2016-06-06 4 views
-1

ich diesen Fehler haben: Verfahren ein Verfahren von einem Supertyp nicht außer Kraft setzen oderFehler: Methode überschreiben oder eine Methode nicht von einem Supertyp implementieren onCreateView

implementieren

Mein Code:

public class MainActivity erstreckt Fragment {

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

    View rootView = inflater.inflate(R.layout.fragment_main, container, false); 

    final ListView list = (ListView) rootView.findViewById(android.R.id.list); 
    final List<Map<String, Object>> contacts = retrieveContacts(getActivity().getContentResolver()); 

    if (contacts != null) 
    { 
     final SimpleAdapter adapter = new SimpleAdapter(getActivity(), contacts, R.layout.activity_main, new String[] { "name", "photo" }, new int[] { R.id.name, R.id.photo }); 
     adapter.setViewBinder(new SimpleAdapter.ViewBinder() 
     { 

      @Override 
      public boolean setViewValue(View view, Object data, String textRepresentation) 
      { 
       if ((view instanceof ImageView) & (data instanceof Bitmap)) 
       { 
        final ImageView image = (ImageView) view; 
        final Bitmap photo = (Bitmap) data; 
        image.setImageBitmap(photo); 
        return true; 
       } 
       return false; 
      } 
     }); 

     list.setAdapter(adapter); 
    } 

    return rootView; 
} 

private Bitmap getPhoto(ContentResolver contentResolver, long contactId) 
{ 
    Bitmap photo = null; 
    final Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId); 
    final Uri photoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY); 
    final Cursor cursor = contentResolver.query(photoUri, new String[] { ContactsContract.Contacts.Photo.DATA15 }, null, null, null); 

    if (cursor == null) 
    { 
     Log.e("getPhoto", "Cannot retrieve the photo of the contact with id '" + contactId + "'"); 
     return null; 
    } 

    if (cursor.moveToFirst() == true) 
    { 
     final byte[] data = cursor.getBlob(0); 

     if (data != null) 
     { 
      photo = BitmapFactory.decodeStream(new ByteArrayInputStream(data)); 
     } 
    } 

    if (cursor.isClosed() == false) 
    { 
     cursor.close(); 
    } 

    return photo; 
} 
private List<Map<String, Object>> retrieveContacts(ContentResolver contentResolver) 
{ 
    final List<Map<String, Object>> contacts = new ArrayList<Map<String, Object>>(); 
    final Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, new String[] { ContactsContract.Data.DISPLAY_NAME, 
      ContactsContract.Data._ID, ContactsContract.Contacts.HAS_PHONE_NUMBER }, null, null, null); 

    if (cursor == null) 
    { 
     Log.e("retrieveContacts", "Cannot retrieve the contacts"); 
     return null; 
    } 

    if (cursor.moveToFirst() == true) 
    { 
     do 
     { 
      final long id = Long.parseLong(cursor.getString(cursor.getColumnIndex(ContactsContract.Data._ID))); 
      final String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME)); 
      final int hasPhoneNumber = cursor.getInt(cursor.getColumnIndex(ContactsContract.Data.HAS_PHONE_NUMBER)); 

      if (hasPhoneNumber > 0) 
      { 
       final Bitmap photo = getPhoto(contentResolver, id); 

       final Map<String, Object> contact = new HashMap<String, Object>(); 
       contact.put("name", name); 
       contact.put("photo", photo); 

       contacts.add(contact); 
      } 
     } 
     while (cursor.moveToNext() == true); 
    } 

    if (cursor.isClosed() == false) 
    { 
     cursor.close(); 
    } 

    return contacts; 
} 

}

Logcat:

06-06 12: 04: 42.579 3924-3924/net.lelandaislaposte.steven.contact E/AndroidRuntime: FATALE AUSNAHME: Haupt Prozess: net.lelandaislaposte.steven.contact, PID: 3924 java.lang.RuntimeException: Unable zu instanziieren Aktivität ComponentInfo {net.lelandaislaposte.steven.contact/net.lelandaislaposte.steven.contact.MainActivity}: java.lang.ClassCastException: net.lelandaislaposte.steven.contact.MainActivity kann nicht in android.app.Activity um umgewandelt werden android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2327) bei android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2476) bei android.app.ActivityThread.-wrap11 (ActivityThread.java) bei Android .app.ActivityThread $ H.handleMessage (ActivityThread.java:1344) bei android.os.Handler.dispatchMessage (Handler.java:102) bei android.os.Looper.loop (Looper.java:148) bei Android .app.ActivityThread.main (ActivityThread.java:5417) bei java.lang.reflect.Method.invoke (systemeigene Methode) bei com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:726) bei com.android.internal.os.ZygoteInit.main (ZygoteInit.java:616) verursacht durch: java.lang.ClassCastException: net.lelandaislaposte.steven.contact.MainActivity kann nicht in android.app.Activity um umgewandelt werden android.app.Instrumentati on.newActivity (Instrumentation.java:1067) bei android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2317) bei android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2476) bei android.app.ActivityThread. -wrap11 (ActivityThread.java) bei android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1344) bei android.os.Handler.dispatchMessage (Handler.java:102) bei android.os.Looper.loop (Looper.java:148) bei android.app.ActivityThread.main (ActivityThread.java:5417) bei java.lang.reflect.Method.invoke (native Methode) bei com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:726) bei com.android.internal.os.ZygoteInit.main (ZygoteInit.java:616)

Ich weiß nicht, woher kommt das?

Es sollte das Foto und den Namen der Kontakte im Hone gespeichert Rückkehr

Antwort

0

Machen Sie die onCreate Methode public, als die Super-Methode public ist, und Sie können es nicht außer Kraft setzen mit einem schwächeren Zugriffsmodifikator.

0

Die onCreateView() ist eine Methode der Fragment Klasse

In Ihrer Aktivität Ihre AppCompatActivity erweitern dh eine Unterklasse von AppCompatActivity Schaffung Es gibt keine onCreateView Methode (LayoutInflater inflater, Viewgroup Behälter, Bundle savedInstanceState) in AppCompatActivity class

Sie sind also nicht ovveriding die merhod von Super in Ihrem Fall AppComptrActivity

Deshalb sollten Sie die Fehlermeldung, dass Methode nicht überschreiben seine Supertyp erhalten

+0

Dankten, aber neuer Fehler: Verursacht durch: java.lang.ClassCastException: net.lelandaislaposte.steven.contact.MainActivity nicht gegossen werden können, um android.app.Activity –

+0

@StevenLnds können Sie sagen, in welcher Zeile Sie diesen Fehler – user5894647

+0

@StevenLnds das Logcat sagt, wo die classcastxception aufgetreten ist, wäre es einfach, wenn Sie sagen, wo die Ausnahme aufgetreten ist – user5894647

0

Sie können unter Methoden in Ihrer Aktivitätsklasse cverride:

@Override 
public View onCreateView(String name, Context context, AttributeSet attrs) { 
    return super.onCreateView(name, context, attrs); 
} 

@Override 
public View onCreateView(View parent, String name, Context context, AttributeSet attrs) { 
    return super.onCreateView(parent, name, context, attrs); 
} 
Verwandte Themen