我正在使用android开发针对作业门户的移动应用程序,因为我有列表视图,我在列表视图中使用了图像视图日期colomn我的要求是如果我单击imageview则自动添加日历事件。下面是我使用的代码,在我运行应用程序后,模拟器显示应用程序意外关闭,有些时候没有关于日历事件的响应仍然可以正常工作。请建议我感谢您的帮助。
Button btnLogin;
Button btnRegister;
Button btnsignUp;
ViewFlipper viewFlipper;
EditText etUserName;
EditText etPassword;
EditText etUserNameSignUp;
EditText etPasswordSignUp;
EditText etPhoneSignUp;
Button btnExit;
Button btnHome;
Button btnNotifications;
Button btnAvalability;
Button btnUpdate;
Button btnPublish;
Button btnStatus;
Button btnNotify;
String[] companies = new String[] { "Infosys", "Wipro", "Cisco", "ibm",
"tcs", "NWS" };
int[] images = new int[] { R.drawable.notifications,
R.drawable.notifications, R.drawable.notifications,
R.drawable.notifications, R.drawable.notifications,
R.drawable.notifications,
};
String[] dates = new String[] { "21/08/12", "22/08/12", "20/08/12",
"16/08/12", "18/08/12", "24/08/12", };
String[] type = new String[] { "Face To Face", "Written", "Online",
"telephonc", "group discussion", "telephonic" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.home);
userName.add("Raghu");
password.add("Raghu");
viewFlipper = (ViewFlipper) findViewById(R.id.main);
btnLogin = (Button) findViewById(R.id.loginButton);
btnLogin.setOnClickListener(this);
btnRegister = (Button) findViewById(R.id.signupButton);
btnRegister.setOnClickListener(this);
btnsignUp = (Button) findViewById(R.id.signupButton);
btnsignUp.setOnClickListener(this);
etUserName = (EditText) findViewById(R.id.userEditText);
etPassword = (EditText) findViewById(R.id.pwEditText);
etUserNameSignUp = (EditText) findViewById(R.id.regNameEditText);
etPasswordSignUp = (EditText) findViewById(R.id.regEmailEditText);
etPhoneSignUp = (EditText) findViewById(R.id.regPhEditText);
btnHome = (Button) findViewById(R.id.homeButton);
btnHome.setOnClickListener(this);
btnExit = (Button) findViewById(R.id.exitButton);
btnExit.setOnClickListener(this);
btnNotifications = (Button) findViewById(R.id.listButton);
btnNotifications.setOnClickListener(this);
btnAvalability = (Button) findViewById(R.id.rightButton);
btnAvalability.setOnClickListener(this);
btnUpdate = (Button) findViewById(R.id.plusButton);
btnUpdate.setOnClickListener(this);
btnPublish = (Button) findViewById(R.id.publishButton);
btnPublish.setOnClickListener(this);
btnStatus = (Button) findViewById(R.id.updateButton);
btnStatus.setOnClickListener(this);
btnNotify = (Button) findViewById(R.id.notificationButton);
btnNotify.setOnClickListener(this);
// Each row in the list stores company name, interview date,
// notification icon and type of interview
List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < 6; i++) {
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("cmp", companies[i]);
hm.put("date", " " + dates[i]);
hm.put("img", Integer.toString(images[i]));
hm.put("type", " " + type[i]);
aList.add(hm);
}
// Keys used in Hashmap
String[] from = { "cmp", "date", "img", "type" };
// Ids of views in lvrow.xml
int[] to = { R.id.cmp, R.id.date, R.id.imgv, R.id.type };
// Instantiating an adapter to store each items
// R.layout.lvrow.xml defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList,
R.layout.listview, from, to);
// Getting a reference to listview of listvi.xml layout file
ListView listView = (ListView) findViewById(R.id.listview);
// Setting the adapter to the listView
listView.setAdapter(adapter);
/*listView.setOnItemClickListener(listener);*/
}
OnItemClickListener listener = new OnItemClickListener() {
// @Override
// onItemClick(AdapterView<?> parent, View view, int position, long id)
// {
// ((TextView) view.findViewById(R.id.yourTextViewId)).getText();
// // or do your stuff
// }
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
ImageView viewObj = (ImageView) arg1.findViewById(R.id.imgv);
viewObj.setImageResource(R.drawable.bgrighticon);
}
};
private void readContent(String uriString) {
Uri calendarUri;
Uri eventUri;
if (android.os.Build.VERSION.SDK_INT <= 7 )
{
//the old way
calendarUri = Uri.parse("content://calendar/calendars");
eventUri = Uri.parse("content://calendar/events");
}
else
{
//the new way
calendarUri = Uri.parse("content://com.android.calendar/calendars");
eventUri = Uri.parse("content://com.android.calendar/events");
}
}
private void addEvent(){
try {
ContentValues event = new ContentValues();
event.put("calendar_id", "1");
event.put("title", "tet event");
event.put("description", "hello this is testing of event");
event.put("eventLocation", "Ahmedabad");
Calendar c = Calendar.getInstance();
long date = c.getTimeInMillis();
event.put("dtstart", date);
event.put("dtend", date);
event.put("allDay", 1);
event.put("eventStatus", 1);
event.put("hasAlarm", 1);
Uri eventsUri = Uri.parse("content://calendar/events");
Uri url = getContentResolver().insert(eventsUri, event);
Log.e("uri", url.toString());
} catch (Exception e) {
Log.e("error", e.getMessage());
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
public void onClick(View v) {
if (v == btnLogin) {
if (userName.contains(etUserName.getText().toString())
&& password.contains(etPassword.getText().toString()))
{
viewFlipper.setDisplayedChild(3);
} else {
}
loggedin = true;
} else if (v == btnRegister) {
viewFlipper.setDisplayedChild(5);
} else if (v == btnsignUp) {// reister new user
// Take the values from the user entered data and save it in
// Arraylist
} else if (v == btnHome) {
if (loggedin) {
viewFlipper.setDisplayedChild(3);
} else {
viewFlipper.setDisplayedChild(0);
}
} else if (v == btnExit) {
System.exit(0);
} else if (v == btnNotifications) {
if (loggedin) {
viewFlipper.setDisplayedChild(4);
}
} else if (v == btnAvalability) {
if (loggedin) {
viewFlipper.setDisplayedChild(2);
}
} else if (v == btnUpdate) {
if (loggedin) {
viewFlipper.setDisplayedChild(1);
}
}
else if (v == btnPublish) {
if (loggedin) {
viewFlipper.setDisplayedChild(2);
}
} else if (v == btnStatus) {
if (loggedin) {
viewFlipper.setDisplayedChild(1);
}
} else if (v == btnNotify) {
if (loggedin) {
viewFlipper.setDisplayedChild(4);
}
}
}
}
答案 0 :(得分:0)
日历是Google Apps的一部分,因此无法在模拟器中使用。
第一种方法:在安装了Google日历应用的Android设备上测试您的应用。
第二种方式:在您的模拟器中安装Google日历应用并在其中测试您的应用。
This answer会为您提供更多帮助。
编辑:
您可以检查是否安装了具有特定包名称的应用程序
public boolean check()
{
try{
ApplicationInfo info = getPackageManager().getApplicationInfo("packagename", 0 );
return true;
} catch( PackageManager.NameNotFoundException e ){
return false;
}
}