ImageView在三星Galaxy S5上崩溃我的Android应用程序

时间:2014-05-18 12:37:38

标签: java android imageview galaxy

我在这里问你一个问题,作为android开发中的新手。现在我正在尝试按照Android Studio教授android开发基础知识的教程,但我遇到了Samsung Galaxy S5的问题。

我尝试做的是从图库中导入图像并将其放入ImageView但是当我尝试启动应用程序时出现崩溃错误。我知道很多人都有这个问题,我下载了三星SDK,但我不知道如何使用这个问题。 (对不起,但我昨天开始这一切,我找不到任何可以帮助我的东西......)

代码在这里:

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);
}
}

我很抱歉在没有适当知识的情况下提出这个问题,但我希望你能理解并帮助我。感谢您阅读并抱歉我所做的任何语法错误。

1 个答案:

答案 0 :(得分:0)

在你的课堂上,取代这个

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

把它,

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

您应该始终调用super类方法以确保您自己的默认行为。此外,@Override注释可确保onActivityResult被称为callback。最后,data.getData() != null确保您没有获得NPE。