I'm here to ask you a question as a newbie in android developing. Right now I'm trying to follow a tutorial which teaches the basics of android developing in Android Studio
, but I encountered an issue with my Samsung Galaxy S5
.
What I'm trying to do is to import an image from the gallery and put it into an ImageView
but when I try to start the application I get a crash error. I know that many people had this issue and I downloaded the samsung SDK, but I don't know what and how to use for this problem. (Sorry, but I started all this yesterday and I can't find anything that can help me...)
The code is here:
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);
}
}
I'm so sorry for asking this without a proper knowledge, but I hope you'll understand and help me. Thank you for reading and sorry for any grammatical error I did.
In your class, in place of this
public void onActivityResult(int reqCode, int resCode, Intent data) {
if(resCode == RESULT_OK) {
if(reqCode == 1) img.setImageURI(data.getData());
}
}
put this,
@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);
}
You should always call super
class method to ensure default behaviour with your own. Also, @Override
annotation makes sure that onActivityResult
is called as a callback
. Lastly, data.getData() != null
ensures that you don't get a NPE.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.