Code does not work

Source: Internet
Author: User

=============== Problem description ====================


I want to run an APK to obtain the contact information of the mobile phone address book. The Code is as follows:

Mainactivity. Java:


Package com. example. getphonenumber;

Import Android. Support. v7.app. actionbaractivity;
Import Android. OS. Bundle;
Import Android. View. Menu;
Import Android. View. menuitem;


Public class mainactivity extends actionbaractivity {

@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. activity_main );

}


@ Override
Public Boolean oncreateoptionsmenu (menu ){
// Inflate the menu; this adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R. Menu. Main, menu );
Return true;
}

@ Override
Public Boolean onoptionsitemselected (menuitem item ){
// Handle action bar item clicks here. The action bar will
// Automatically handle clicks on the home/Up button, so long
// As you specify a parent activity in androidmanifest. xml.
Int id = item. getitemid ();
If (ID = R. Id. action_settings ){
Return true;
}
Return super. onoptionsitemselected (item );
}



}




Activity01.java:


Package com. example. getphonenumber;

Import Android. App. activity;
Import Android. content. contentresolver;
Import Android. database. cursor;
Import Android. OS. Bundle;
Import Android. provider. contactscontract;
Import Android. provider. contactscontract. phonelookup;
Import Android. widget. textview;

Public class activity01 extends activity {
Private int numberfieldcolumnindex;
Private string contact;

Public void oncreate (bundle savedinstancestate ){
Textview TV = new textview (this );
String string = "";
Super. oncreate (savedinstancestate );
Contentresolver Cr = getcontentresolver ();
Cursor cursor = Cr. Query (contactscontract. Contacts. content_uri, null );
While (cursor. movetonext ())
{Int namefieldcolumnindex = cursor. getcolumnindex (phonelookup. display_name );
String number = cursor. getstring (numberfieldcolumnindex );
String + = (contact + ":" + number + "\ n ");



}
Cursor. Close ();
TV. settext (string );
Setcontentview (TV );
}

}



Getphonenumber mainfest:

<? XML version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
Package = "com. example. getphonenumber"
Android: versioncode = "1"
Android: versionname = "1.0" type = "codeph" text = "/codeph">

<Uses-SDK
Android: minsdkversion = "8"
Android: targetsdkversion = "8"/>
<Uses-Permission
Android: Name = "android. Permission. read_contacts">

</Uses-Permission>
<Application
Android: allowbackup = "true"
Android: icon = "@ drawable/ic_launcher"
Android: Label = "@ string/app_name"
Android: theme = "@ style/apptheme">
<Activity
Android: Name = ". mainactivity"
Android: Label = "@ string/app_name">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. Main"/>

<Category Android: Name = "android. Intent. Category. launcher"/>
</Intent-filter>
</Activity>

</Application>

</Manifest>


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"
Android: paddingbottom = "@ dimen/activity_vertical_margin"
Android: paddingleft = "@ dimen/activity_horizontal_margin"
Android: paddingright = "@ dimen/activity_horizontal_margin"
Android: paddingtop = "@ dimen/activity_vertical_margin"
Tools: context = "com. example. getphonenumber. mainactivity">

<Textview
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello_world"/>

</Relativelayout>




But I only run Hello world.

============= Solution 1 ======================


<Activity
Android: Name = ". mainactivity"
Android: Label = "@ string/app_name">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. Main"/>

<Category Android: Name = "android. Intent. Category. launcher"/>
</Intent-filter>
</Activity>

Change mainactivity to your activity01.

============= Solution 2 ======================


...... In code format,

Code does not work

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.