传递数据时出错

时间:2012-10-02 09:14:38

标签: android android-intent pass-data

我有一个名单列表视图(从数据库导入)。当点击列表中的名字时,我想从数据库中获取名称的详细信息,所以我必须将名称传递给我所在的下一个班级检索详细信息。我正在尝试将名称从一个类传递到另一个类。我不知道我是否错误地传递字符串或以错误的方式获取字符串的名称。

contact.java

public class Contacts extends Activity implements OnClickListener {
    int NewContact_Request_Code = 1;
    Button newcontact;
    ListView listview;
    public static final String LOG_TAG = "Contacts";
    int mInt = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contactview);// Set the content to contactview.xml

        // newcontact = NEW CONTACT button
        // listview = MyList List View
        newcontact = (Button) findViewById(R.id.baddcontact);
        listview = (ListView) findViewById(R.id.mylist);

        // Make a New Database
        DBContact info = new DBContact(this);
        // Open , get Information from database and close it
        info.open();
        String[] data = info.queryAll();
        info.close();
        // listview = getListView();
        listview.setTextFilterEnabled(true);
        // Display the names
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(Contacts.this,
                android.R.layout.simple_list_item_1, data);
        listview.setAdapter(adapter);
        listview.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?>listview, View view,
                    int position, long id) {

                String nameclicked = ((TextView)view).getText().toString();
                Intent viewintent = new Intent(Contacts.this, ViewContact.class);
                viewintent.putExtra("name_clicked", nameclicked);
                startActivity(viewintent);

            }
        });
        newcontact.setOnClickListener(this);

    }

    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent newintent = new Intent(Contacts.this, AddNewContact.class);
        // start activity for result - to get the name of the new contact
        startActivityForResult(newintent, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        // pass the value of the string via cursor and update the list
    }

}

viewcontact.java

public class ViewContact extends Activity implements OnClickListener {
    Button ViewPPhone, ViewHPhone, ViewOPhone, EditContact;
    TextView ViewName;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewcontact);
        savedInstanceState = getIntent().getExtras();
        String name = savedInstanceState.getString("name_clicked");
        Long l = Long.parseLong(name);
        DBContact getdetails = new DBContact(this);
        getdetails.open();
        String returnedname = getdetails.getName(l);
        String returnedpphone = getdetails.getPphone(l);
        String returnedhphone = getdetails.getHphone(l);
        String returnedophone = getdetails.getOphone(l);
        getdetails.close();
        ViewName.setText(returnedname);
        ViewPPhone.setText(returnedpphone);
        ViewHPhone.setText(returnedhphone);
        ViewOPhone.setText(returnedophone);

        EditContact = (Button) findViewById(R.id.bEditContact);
        EditContact.setOnClickListener(this);
        ViewPPhone = (Button) findViewById(R.id.ViewPersonalPhoneNumber);
        ViewPPhone.setOnClickListener(this);
        ViewHPhone = (Button) findViewById(R.id.ViewHomePhoneNumber);
        ViewHPhone.setOnClickListener(this);
        ViewOPhone = (Button) findViewById(R.id.ViewOfficePhoneNumber);
        ViewOPhone.setOnClickListener(this);

    }

    public void onClick(View view) {
        // TODO Auto-generated method stub
        switch (view.getId()) {
        case R.id.ViewPersonalPhoneNumber:
        /*  Intent dialpersonalphone = new
            Intent(android.content.Intent.ACTION_DIAL,
            Uri.parse("returnedpphone"));
            startActivity(dialpersonalphone );*/
            break;

        case R.id.ViewHomePhoneNumber:
            /*Intent dialhome = new
            Intent(android.content.Intent.ACTION_DIAL,
            Uri.parse("returnedhphone"));
            startActivity(dialhome);*/
            break;

        case R.id.ViewOfficePhoneNumber:
            /*Intent dialoffice = new
            Intent(android.content.Intent.ACTION_DIAL,
            Uri.parse("returnedophone"));
            startActivity(dialoffice);*/
            break;

        case R.id.bEditContact:
            startActivity(new Intent("com.example.contactlist.EDITCONTACT"));
            break;
        }
    }

}

logcat的

10-02 10:30:23.064: E/AndroidRuntime(1045): FATAL EXCEPTION: main
10-02 10:30:23.064: E/AndroidRuntime(1045): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.contactlist/com.example.contactlist.ViewContact}: java.lang.NumberFormatException: Invalid long: "nishanth"
10-02 10:30:23.064: E/AndroidRuntime(1045):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-02 10:30:23.064: E/AndroidRuntime(1045):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-02 10:30:23.064: E/AndroidRuntime(1045):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-02 10:30:23.064: E/AndroidRuntime(1045):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-02 10:30:23.064: E/AndroidRuntime(1045):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-02 10:30:23.064: E/AndroidRuntime(1045):     at android.os.Looper.loop(Looper.java:137)
10-02 10:30:23.064: E/AndroidRuntime(1045):     at android.app.ActivityThread.main(ActivityThread.java:4745)
10-02 10:30:23.064: E/AndroidRuntime(1045):     at java.lang.reflect.Method.invokeNative(Native Method)
10-02 10:30:23.064: E/AndroidRuntime(1045):     at java.lang.reflect.Method.invoke(Method.java:511)
10-02 10:30:23.064: E/AndroidRuntime(1045):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-02 10:30:23.064: E/AndroidRuntime(1045):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-02 10:30:23.064: E/AndroidRuntime(1045):     at dalvik.system.NativeStart.main(Native Method)
10-02 10:30:23.064: E/AndroidRuntime(1045): Caused by: java.lang.NumberFormatException: Invalid long: "nishanth"
10-02 10:30:23.064: E/AndroidRuntime(1045):     at java.lang.Long.invalidLong(Long.java:125)
10-02 10:30:23.064: E/AndroidRuntime(1045):     at java.lang.Long.parse(Long.java:362)
10-02 10:30:23.064: E/AndroidRuntime(1045):     at java.lang.Long.parseLong(Long.java:353)
10-02 10:30:23.064: E/AndroidRuntime(1045):     at java.lang.Long.parseLong(Long.java:319)
10-02 10:30:23.064: E/AndroidRuntime(1045):     at com.example.contactlist.ViewContact.onCreate(ViewContact.java:23)
10-02 10:30:23.064: E/AndroidRuntime(1045):     at android.app.Activity.performCreate(Activity.java:5008)
10-02 10:30:23.064: E/AndroidRuntime(1045):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-02 10:30:23.064: E/AndroidRuntime(1045):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-02 10:30:23.064: E/AndroidRuntime(1045):     ... 11 more

4 个答案:

答案 0 :(得分:1)

你正试图从savedInstanceState Bundle获取字符串。 您要做的是从String开始的Intent获取activity

getIntent().getStringExtra("name_clicked");

修改

可能是因为您尝试将String name解析为Long。你确定这个名字都是数字吗?

如果您提供堆栈跟踪

,也可以更容易确定问题

答案 1 :(得分:1)

传递

viewintent.putExtra("name_clicked", nameclicked);

进入viewcontact.java

String name = getIntent().getStringExtra("name_clicked");

修改 ----------

您正在向其他活动发送字符串,然后在捕获后尝试将字符串转换为

出于这个原因,你得到 NumberFormatException

请参阅 ViewContact.java 23,无需将字符串转换为

Caused by: java.lang.NumberFormatException: Invalid long: "nishanth"

at com.example.contactlist.ViewContact.onCreate(ViewContact.java:23)

答案 2 :(得分:1)

当我使用bundle传递信息时,我发现我需要实例化一个os.Bundle对象,然后我继续填充我的包信息,然后将此包添加到意图中。

修改您的contact.java代码以便阅读;

    Intent viewintent = new Intent(Contacts.this, ViewContact.class);
    final Bundle bundle = new Bundle();
    bundle.putString("name_clicked", nameclicked);
    viewintent.putExtras(bundle);
    startActivity(viewintent);

并重新运行以查看是否有效。

修改

问题是String nameclicked = ((TextView)view).getText().toString();没有返回长 - 它返回联系人名称“nishanth”!在logcat日志中明确指出了这一点;

  

10-02 10:30:23.064:E / AndroidRuntime(1045):引起:   java.lang.NumberFormatException:无效长:“nishanth”

((TextView)view)返回的视图不是电话号码 - 而是由您的密钥“name_clicked”提到的联系人姓名。更改此视图以定位电话号码。请注意,电话号码可以包含“+”和“ - ”符号,因此检索所选电话号码的实现并非万无一失。

答案 3 :(得分:1)

看到这个我们有想法使用setter和getters它非常有用这个lisk点击

 list.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                      

                Object  ob = (Object) view.getTag();                 
                if(ob != null) 
                {                       
                    Data da = (Data) view.getTag();              
                    String a = da.getSelected();
                    String a1 = da.getfunction();
                    int a2 = da.getImage();

                    Log.i(" val one " ,"_" +a);
                    Log.i(" val two ", "_"  +a1);
                    Log.i(" val three","_" +a2); 

                    Intent intent = new Intent(ListMobileActivity.this, Display.class);                         
                    intent.putExtra("passed", a);
                    intent.putExtra("passed1", a1);     
                    intent.putExtra("passed2" ,a2);                      

                    startActivity(intent);          
                }
            }        
        });

第二项活动

 TextView text = (TextView)findViewById(R.id.textView);
            TextView text1 = (TextView)findViewById(R.id.textView1);
            ImageView image = (ImageView)findViewById(R.id.imageView);

            message = getIntent().getExtras().getString("passed");
            message1 = getIntent().getExtras().getString("passed1");
            message2 = getIntent().getExtras().getInt("passed2");                          

            text.setText(message);
            text1.setText(message1);
            image.setBackgroundResource(message2);  

这是setter和getter类

   public class Data {

    private String first;
    private String value;   
    private int image; 

    public void setSelected(String str) {
        this.first = str;
    }
    public String getSelected() {
        return first;
    }
    public void setfunction(String string) {
        this.value = string;
    }
    public String getfunction() {
        return value;
    }
    public void setImage(int image2) {
        this.image = image2;
    }
    public int getImage() {
        return image;
    }    

}