2014-05-18 2 views
0

Je suis ici pour vous poser une question en tant que débutant dans le développement android. En ce moment je suis en train de suivre un tutoriel qui enseigne les bases d'Android en développement dans Android Studio, mais j'ai rencontré un problème avec mon Samsung Galaxy S5. Ce que j'essaye de faire est d'importer une image de la galerie et la mettre dans un ImageView mais quand j'essaye de lancer l'application j'obtiens une erreur de plantage. Je sais que beaucoup de gens ont eu ce problème et j'ai téléchargé le SDK samsung, mais je ne sais pas quoi et comment utiliser pour ce problème. (Désolé, mais j'ai commencé tout cela hier et je ne peux pas trouver tout ce qui peut me aider ...)ImageView bloque mon application android sur Samsung Galaxy S5

Le code est ici:

activity_main.xml

<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" 
tools:context="${packageName}.${activityClass}"> 


<TabHost 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/tabHost" 
    android:layout_alignParentTop="false"> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical"> 

     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"></TabWidget> 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 

      <LinearLayout 
       android:id="@+id/tabCreator" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:orientation="vertical"> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textAppearance="?android:attr/textAppearanceLarge" 
        android:text="Contatto" 
        android:id="@+id/textView" 
        android:layout_gravity="center_horizontal" /> 

       <ImageView 
        android:layout_width="100dp" 
        android:layout_height="100dp" 
        android:id="@+id/imgContact" 
        android:layout_gravity="center_horizontal" 
        android:layout_marginTop="10dp" 
        android:src="@drawable/no_user_logo" 
        android:clickable="false" /> 

       <EditText 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:inputType="textPersonName" 
        android:ems="10" 
        android:id="@+id/txtName" 
        android:textAllCaps="false" 
        android:hint="Nome" 
        android:layout_marginTop="10dp" /> 

       <EditText 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:inputType="phone" 
        android:ems="10" 
        android:id="@+id/txtNumber" 
        android:hint="Numero di Telefono" 
        android:layout_marginTop="10dp" 
        android:layout_below="@id/txtName" 
        android:layout_alignParentLeft="true" 
        android:layout_alignParentStart="false" /> 

       <EditText 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:inputType="textEmailAddress" 
        android:ems="10" 
        android:id="@+id/txtMail" 
        android:layout_marginTop="10dp" 
        android:hint="Email" /> 

       <Button 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Aggiungi Contatto" 
        android:id="@+id/btnAdd" 
        android:layout_centerVertical="true" 
        android:layout_centerHorizontal="true" 
        android:enabled="false" 
        android:layout_gravity="center_horizontal" /> 

      </LinearLayout> 

      <LinearLayout 
       android:id="@+id/tabList" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:orientation="vertical"> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textAppearance="?android:attr/textAppearanceLarge" 
        android:text="Contatti" 
        android:id="@+id/textView2" 
        android:layout_gravity="center_horizontal" /> 

       <ListView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:id="@+id/banana" 
        android:layout_gravity="center_horizontal" 
        android:layout_marginTop="10dp" /> 
      </LinearLayout> 

     </FrameLayout> 

    </LinearLayout> 
</TabHost> 

</RelativeLayout> 

MainActivity.java

public class MainActivity extends Activity { 

EditText txtName, txtPhone, txtEmail; 
List<Contact> lstContacts = new ArrayList<Contact>(); 
ListView lstViewContact; 
ImageView img = (ImageView)findViewById(R.id.imgContact); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    txtName = (EditText) findViewById(R.id.txtName); 
    txtPhone = (EditText) findViewById(R.id.txtNumber); 
    txtEmail = (EditText) findViewById(R.id.txtMail); 
    lstViewContact = (ListView)findViewById(R.id.banana); 

    final Button btn = (Button) findViewById(R.id.btnAdd); 
    btn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      addContact(txtName.getText().toString().trim(), txtPhone.getText().toString(), txtEmail.getText().toString()); 
      Toast.makeText(getApplicationContext(), "Banana", Toast.LENGTH_SHORT).show(); 
     } 
    }); 
    img.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent intent = new Intent(); 
      intent.setType("image/*"); 
      intent.setAction(Intent.ACTION_GET_CONTENT); 
      startActivityForResult(Intent.createChooser(intent, "Select Contact Image"), 1); 
     } 
    }); 

    txtName.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) { 

     } 

     @Override 
     public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) { 
      boolean goName = false, goPhone = false; 
      if(txtName.length() > 0) goName = txtName.getText().toString().trim().length() > 0; 
      if(txtPhone.length() > 0) goPhone = txtPhone.getText().toString().trim().length() > 0; 

      btn.setEnabled(goName && goPhone); 
     } 

     @Override 
     public void afterTextChanged(Editable editable) { 

     } 
    }); 
    txtPhone.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) { 

     } 

     @Override 
     public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) { 
      boolean goName = false, goPhone = false; 
      if(txtName.length() > 0) goName = txtName.getText().toString().trim().length() > 0; 
      if(txtPhone.length() > 0) goPhone = txtPhone.getText().toString().trim().length() > 0; 

      btn.setEnabled(goName && goPhone); 
     } 

     @Override 
     public void afterTextChanged(Editable editable) { 

     } 
    }); 


    TabHost tabHost = (TabHost)findViewById(R.id.tabHost); 
    tabHost.setup(); 

    TabHost.TabSpec tabSpec = tabHost.newTabSpec("creator"); 
    tabSpec.setContent(R.id.tabCreator); 
    tabSpec.setIndicator("Creator"); 
    tabHost.addTab(tabSpec); 

    tabSpec = tabHost.newTabSpec("list"); 
    tabSpec.setContent(R.id.tabList); 
    tabSpec.setIndicator("List"); 
    tabHost.addTab(tabSpec); 

} 

public void onActivityResult(int reqCode, int resCode, Intent data) { 
    if(resCode == RESULT_OK) { 
     if(reqCode == 1) img.setImageURI(data.getData()); 
    } 
} 

private class lstContactsAdapter extends ArrayAdapter<Contact> { 
    public lstContactsAdapter() { 
     super(MainActivity.this, R.layout.listview_item, lstContacts); 
    } 

    @Override 
    public View getView(int position, View view, ViewGroup parent) { 
     //Se la view non esiste io la metto come listview_item all'interno della lista alla fine (non al root) 
     if(view == null) view = getLayoutInflater().inflate(R.layout.listview_item, parent, false); 

     Contact currentContact = lstContacts.get(position); 

     TextView name = (TextView)view.findViewById(R.id.contactName); 
     TextView phone = (TextView)view.findViewById(R.id.contactPhone); 
     TextView mail = (TextView)view.findViewById(R.id.contactMail); 
     name.setText(currentContact.getName()); 
     phone.setText(currentContact.getPhone()); 
     mail.setText(currentContact.getEmail()); 

     return view; 
    } 
} 
private void addContact(String name, String phone, String email) { 
    lstContacts.add(new Contact(name, phone, email)); 
    ArrayAdapter<Contact> adapter = new lstContactsAdapter(); 
    lstViewContact.setAdapter(adapter); 
} 
} 

Je suis désolé de demander cela sans une bonne connaissance, mais je hop Vous comprendrez et m'aiderez. Merci d'avoir lu et désolé pour toute erreur grammaticale que j'ai faite.

+0

Publiez votre stacktrace à partir de logcat. – vjdhama

+0

Je ne sais vraiment pas comment l'obtenir, mais je pense que c'est [this] (http://imgur.com/gxd13rW). J'espère que c'est, sinon, faites-moi savoir ... Considérez que je suis un développeur C# et que je ne suis pas très habitué à ces outils ... – Zavian

Répondre

0

Dans votre classe, à la place de ce

public void onActivityResult(int reqCode, int resCode, Intent data) { 
    if(resCode == RESULT_OK) { 
     if(reqCode == 1) img.setImageURI(data.getData()); 
     } 
} 

mis ce,

@Override 
public void onActivityResult(int reqCode, int resCode, Intent data) { 
    if(resCode == RESULT_OK) { 
     if(reqCode == 1 && data.getData() != null) 
      img.setImageURI(data.getData()); 
     } 
    super.onActivityResult(reqCode, resCode, data); 
} 

Vous devez toujours appeler super méthode de classe pour assurer un comportement par défaut avec votre propre. En outre, l'annotation @Override s'assure que onActivityResult est appelé callback. Enfin, data.getData() != null garantit que vous n'obtenez pas un NPE.

Questions connexes