onActivityResult()错误

时间:2016-12-02 21:33:26

标签: java android

任何人都可以清楚地解释为什么onActivityResult产生以下错误这里不允许注释

“;”预期

运算符'=='不能应用于'int','null'

无法解析方法'getExtras()'

无法解析符号'ACTION_CALL'

无法解析符号'ACTION_VIEW'

您也可以解释如何修复它。谢谢。

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import static android.R.attr.data;


public class ContactIntentActivity extends AppCompatActivity {

    private final int PHONE = 0;
    private final int WEBSITE = 1;
    private ListView intentListView;
    private ArrayAdapter<String> adapter;
    private List<ContactObject> contactsList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_intent);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        // inflate my view
        setContentView(R.layout.activity_contact_intent);

        intentListView = (ListView) findViewById(R.id.listView1);

        // initialize the list and add item
        contactsList = new ArrayList<>();
        contactsList.add(new ContactObject("Android One", "111-111-1111", "www.naruto.com"));
        contactsList.add(new ContactObject("Android Two", "222-222-2222", "www.naruto.com"));
        contactsList.add(new ContactObject("Android Three", "333-333-3333", "www.naruto.com"));
        contactsList.add(new ContactObject("Android Four", "444-444-4444", "www.naruto.com"));

        List<String> listName = new ArrayList<>();
        for (int i = 0; i < contactsList.size(); i++) {
            listName.add(contactsList.get(i).getName());
        }

        // initialize the ArrayAdapter object
        adapter = new ArrayAdapter<>(ContactIntentActivity.this, android.R.layout.simple_list_item_1, listName);

        // set the adapter of the ListView
        intentListView.setAdapter(adapter);

        // setonclicklistener in adapterview cannot be applied here. changed to setonItemClickListener
        intentListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent i = new Intent(ContactIntentActivity.this, ContactPageActivity.class);
                i.putExtra("Object", contactsList.get(position));
                startActivityForResult(i, 0);
            }
        });


        @Override
        protected void onActivityResult ( int requestCode, int resultCode, Intent data)
        {
            if (data == null) {
                return;
            }
            Bundle resultData = data.getExtras();
            String value = resultData.getString("value");
            switch (resultCode) {
                case PHONE:
                    //Implicit intent to make a call
                    startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + value)));
                    break;
                case WEBSITE:
                    //Implicit intent to visit website
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://" + value)));
                    break;
            }
        }
    }

1 个答案:

答案 0 :(得分:2)

关闭onCreate功能:在上一次}注释之前添加@Override