我在android啓動器上工作,它包含三個活動,第一個活動是HomeActivity
,其中包含四個按鈕,這些按鈕將執行意向操作,其餘活動爲空,但在HomeActivity
單擊時聯繫人按鈕它會顯示聯繫人列表,但是當我選擇其中一個聯繫人時,它什麼也沒有顯示,其餘的按鈕也像聯繫人按鈕一樣,它不會跳轉到OnActivityResult()
方法,所以我該如何解決這個問題?Android ViewPager問題
下面是我的代碼: -
MainHomeActivity.java
public class MainHomeActivity extends AppCompatActivity {
DevicePolicyManager devicePolicyManager;
ComponentName adminComponent;
Button apps;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Button lock = (Button) findViewById(R.id.button_lock);
lock.setOnClickListener(btnListener);
Settings.System.putInt(getContentResolver(),Settings.System.SCREEN_OFF_TIMEOUT,15000);
apps = (Button) findViewById(R.id.main_menu);
}
Button.OnClickListener btnListener = new Button.OnClickListener() {
@Override
public void onClick(View v) {
adminComponent = new ComponentName(MainHomeActivity.this, AdminReceiver.class);
devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
if (!devicePolicyManager.isAdminActive(adminComponent)) {
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, adminComponent);
startActivityForResult(intent, 50);
} else {
devicePolicyManager.lockNow();
}
apps.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainHomeActivity.this, AppsListActivity.class);
startActivity(i);
}
});
}
};
}
HomeActivity.java
public class HomeActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_home_activity);
ViewPager viewPage = (ViewPager) findViewById(R.id.viewpager);
viewPage.setAdapter(new CustomPageAdapter(this));
}
public void contact(View view){
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
if(intent.resolveActivity(getPackageManager()) != null){
startActivityForResult(intent,10);
}
}
public void audioIntent(View view){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getParent()+"/ext_card");
intent.setDataAndType(uri,"audio/*");
startActivityForResult(intent,20);
}
public void videoIntent(View v){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getParent()+"/ext_card");
intent.setDataAndType(uri,"video/*");
startActivityForResult(intent,30);
}
public void pdfIntent(View view){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getParent()+"/ext.card");
intent.setDataAndType(uri,"pdf/*");
startActivityForResult(intent,40);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == RESULT_OK && resultCode == 10){
Uri contactUri = data.getData();
Intent contactIntent = new Intent(Intent.ACTION_CALL);
contactIntent.setData(contactUri);
startActivity(contactIntent);
}
if(resultCode == 20 && requestCode == RESULT_OK){
Uri audioUri = data.getData();
Intent audioIntent = new Intent(Intent.ACTION_VIEW);
audioIntent.setDataAndType(audioUri,"audio/*");
startActivity(audioIntent);
}
if(requestCode == RESULT_OK && resultCode == 30){
Uri videoUri = data.getData();
Intent videoIntent = new Intent(Intent.ACTION_VIEW);
videoIntent.setDataAndType(videoUri,"video/*");
startActivity(videoIntent);
}
if(requestCode == RESULT_OK && resultCode == 40){
Uri pdfUri = data.getData();
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(pdfUri,"pdf/*");
startActivity(pdfIntent);
}
}
}
CustomPagerAdapter.java
public class CustomPageAdapter extends PagerAdapter {
private Context mContext;
public CustomPageAdapter(Context context){
mContext = context;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ModelObject modelObject = ModelObject.values()[position];
LayoutInflater inflater = LayoutInflater.from(mContext);
ViewGroup layout = (ViewGroup) inflater.inflate(modelObject.getLayoutResId(),container,false);
container.addView(layout);
return layout;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public int getCount() {
return ModelObject.values().length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public CharSequence getPageTitle(int position) {
ModelObject customPagerEnum = ModelObject.values()[position];
return mContext.getString(customPagerEnum.getTitleResId());
}
}
這是應用程序的截圖: -
我已經提供onClick屬性到xml文件中的每個按鈕。 –
爲什麼Android Studio標籤?這不是一個關於該工具的問題。 – EJoshuaS