簡體   English   中英

ImageView在Samsung Galaxy S5上使我的android應用程序崩潰

[英]ImageView crashes my android application on Samsung Galaxy S5

我在這里問你一個問題,作為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);
}
}

很抱歉在沒有適當知識的情況下提出這個問題,但是希望您能理解並幫助我。 感謝您的閱讀,對於我所做的任何語法錯誤,我們深表歉意。

在您的課堂上,代替這個

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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM