我已经用新的错误更改了日志猫.Log-cat错误再次显示相同的错误,并且在日历中单击日期后模拟器强制关闭... 我甚至在清单中声明了所有活动然后错误继续.... 请帮忙
12-11 11:31:05.745: W/dalvikvm(483): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-11 11:31:05.755: E/AndroidRuntime(483): FATAL EXCEPTION: main
12-11 11:31:05.755: E/AndroidRuntime(483): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.examples.android.calendar/android.util.EventLog$Event}; have you declared this activity in your AndroidManifest.xml?
12-11 11:31:05.755: E/AndroidRuntime(483): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
12-11 11:31:05.755: E/AndroidRuntime(483): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
12-11 11:31:05.755: E/AndroidRuntime(483): at android.app.Activity.startActivityForResult(Activity.java:2827)
12-11 11:31:05.755: E/AndroidRuntime(483): at android.app.Activity.startActivity(Activity.java:2933)
12-11 11:31:05.755: E/AndroidRuntime(483): at com.examples.android.calendar.CalendarView$4.onItemClick(CalendarView.java:104)
12-11 11:31:05.755: E/AndroidRuntime(483): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
12-11 11:31:05.755: E/AndroidRuntime(483): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
12-11 11:31:05.755: E/AndroidRuntime(483): at android.os.Handler.handleCallback(Handler.java:587)
12-11 11:31:05.755: E/AndroidRuntime(483): at android.os.Handler.dispatchMessage(Handler.java:92)
12-11 11:31:05.755: E/AndroidRuntime(483): at android.os.Looper.loop(Looper.java:123)
12-11 11:31:05.755: E/AndroidRuntime(483): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-11 11:31:05.755: E/AndroidRuntime(483): at java.lang.reflect.Method.invokeNative(Native Method)
12-11 11:31:05.755: E/AndroidRuntime(483): at java.lang.reflect.Method.invoke(Method.java:507)
12-11 11:31:05.755: E/AndroidRuntime(483): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-11 11:31:05.755: E/AndroidRuntime(483): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-11 11:31:05.755: E/AndroidRuntime(483): at dalvik.system.NativeStart.main(Native Method)
12-11 11:31:08.927: I/Process(483): Sending signal. PID: 483 SIG: 9
包含错误的代码在点击监听器的网格视图中如下所示。
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
TextView date = (TextView)v.findViewById(R.id.date);
LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(date instanceof TextView && !date.getText().equals("")) {
// Intent intent;
String day = date.getText().toString();
if(day.length()==1) {
day = "0"+day;
}
// return chosen date as string format
/** intent.putExtra("date", android.text.format.DateFormat.format("yyyy-MM", month)+"-"+day);
setResult(RESULT_OK, intent);
finish();
*/
//Intent intent = new Intent(v.getContext(), Event.class);
Intent intent = (new Intent(CalendarView.this, Event.class));
startActivity(intent);
// RelativeLayout layoutLeft = (RelativeLayout) inflate.inflate(
// R.layout.event, null);
}
}
});
}
这是我的项目清单文件,我已经更新了一些更改......
清单文件
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".CalendarViewSampleActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.examples.android.calendar.CalendarView"
android:label="com.examples.android.calendar.CalendarView"> </activity>
<activity android:name="com.examples.android.calendar.CalendarAdapter"
android:label="com.examples.android.calendar.CalendarAdapter" > </activity>
</application>
我添加了项目的完整代码,请验证代码。从gridview调用新xml布局的onclick事件显示错误。
要查看日历,这里是代码。
CalenderView.java文件
package com.examples.android.calendar;
public class CalendarView extends Activity {
public Calendar month;
public CalendarAdapter adapter;
public Handler handler;
public Context context;
public ArrayList<String> items; // container to store some random calendar items
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calendar);
month = Calendar.getInstance();
onNewIntent(getIntent());
items = new ArrayList<String>();
adapter = new CalendarAdapter(this, month);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(adapter);
handler = new Handler();
handler.post(calendarUpdater);
TextView title = (TextView) findViewById(R.id.title);
title.setText(android.text.format.DateFormat.format("MMMM yyyy", month));
TextView previous = (TextView) findViewById(R.id.previous);
previous.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(month.get(Calendar.MONTH)== month.getActualMinimum(Calendar.MONTH)) {
month.set((month.get(Calendar.YEAR)-1),month.getActualMaximum(Calendar.MONTH),1);
} else {
month.set(Calendar.MONTH,month.get(Calendar.MONTH)-1);
}
refreshCalendar();
}
});
TextView next = (TextView) findViewById(R.id.next);
next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(month.get(Calendar.MONTH)== month.getActualMaximum(Calendar.MONTH)) {
month.set((month.get(Calendar.YEAR)+1),month.getActualMinimum(Calendar.MONTH),1);
} else {
month.set(Calendar.MONTH,month.get(Calendar.MONTH)+1);
}
refreshCalendar();
}
});
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
TextView date = (TextView)v.findViewById(R.id.date);
LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(date instanceof TextView && !date.getText().equals("")) {
// Intent intent = new Intent();
String day = date.getText().toString();
if(day.length()==1) {
day = "0"+day;
}
// return chosen date as string format
/** intent.putExtra("date", android.text.format.DateFormat.format("yyyy-MM", month)+"-"+day);
setResult(RESULT_OK, intent);
finish();
*/
//Intent intent = new Intent(v.getContext(), Event.class);
Intent intent = (new Intent(CalendarView.this, Event.class));
startActivity(intent);
// RelativeLayout layoutLeft = (RelativeLayout) inflate.inflate(
// R.layout.event, null);
}
}
});
}
public void refreshCalendar()
{
TextView title = (TextView) findViewById(R.id.title);
adapter.refreshDays();
adapter.notifyDataSetChanged();
handler.post(calendarUpdater); // generate some random calendar items
title.setText(android.text.format.DateFormat.format("MMMM yyyy", month));
}
public void onNewIntent(Intent intent) {
String date = intent.getStringExtra("date");
String[] dateArr = date.split("-"); // date format is yyyy-mm-dd
month.set(Integer.parseInt(dateArr[0]), Integer.parseInt(dateArr[1]), Integer.parseInt(dateArr[2]));
}
public Runnable calendarUpdater = new Runnable() {
@Override
public void run() {
items.clear();
// format random values. You can implement a dedicated class to provide real values
for(int i=0;i<31;i++) {
Random r = new Random();
if(r.nextInt(10)>6)
{
items.add(Integer.toString(i));
}
}
adapter.setItems(items);
adapter.notifyDataSetChanged();
}
};
}
此事件将由gridview调用。此文件必须在日历中单击日期后显示.... event.xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Event Date"
android:textAppearance="?android:attr/textAppearanceMedium" />
<android.widget.Space
android:id="@+id/space1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textView2"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Event Discription"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
</TableLayout>
</RelativeLayout>
模拟器被强制停止显示此java文件中的错误 CalenderViewSampleActivity.java 文件
package com.examples.android.calendar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Toast;
public class CalendarViewSampleActivity extends Activity {
/** Called when the activity is first created. */
static final int DATE_DIALOG_ID = 0;
static final int PICK_DATE_REQUEST = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button openButton = (Button)findViewById(R.id.openButton);
openButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
DatePicker dp = (DatePicker)findViewById(R.id.datePicker1);
Intent intent = new Intent(v.getContext(),CalendarView.class);
intent.putExtra("date", dp.getYear()+"-"+dp.getMonth()+"-"+dp.getDayOfMonth());
startActivityForResult(intent, PICK_DATE_REQUEST);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_DATE_REQUEST) {
if (resultCode == RESULT_OK) {
Toast.makeText(getApplicationContext(), data.getStringExtra("date"), Toast.LENGTH_SHORT).show();
String[] dateArr = data.getStringExtra("date").split("-");
DatePicker dp = (DatePicker)findViewById(R.id.datePicker1);
dp.updateDate(Integer.parseInt(dateArr[0]), Integer.parseInt(dateArr[1]), Integer.parseInt(dateArr[2]));
}
}
}
}
CalenderAdapter.java文件
package com.examples.android.calendar;
import java.util.ArrayList;
import java.util.Calendar;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
public class CalendarAdapter extends BaseAdapter {
static final int FIRST_DAY_OF_WEEK =0; // Sunday = 0, Monday = 1
private Context mContext;
private java.util.Calendar month;
private Calendar selectedDate;
private ArrayList<String> items;
public CalendarAdapter(Context c, Calendar monthCalendar) {
month = monthCalendar;
selectedDate = (Calendar)monthCalendar.clone();
mContext = c;
month.set(Calendar.DAY_OF_MONTH, 1);
this.items = new ArrayList<String>();
refreshDays();
}
public void setItems(ArrayList<String> items) {
for(int i = 0;i != items.size();i++){
if(items.get(i).length()==1) {
items.set(i, "0" + items.get(i));
}
}
this.items = items;
}
public int getCount() {
return days.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new view for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
TextView dayView;
if (convertView == null) { // if it's not recycled, initialize some attributes
LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.calendar_item, null);
}
dayView = (TextView)v.findViewById(R.id.date);
// disable empty days from the beginning
if(days[position].equals("")) {
dayView.setClickable(false);
dayView.setFocusable(false);
}
else {
// mark current day as focused
if(month.get(Calendar.YEAR)== selectedDate.get(Calendar.YEAR) && month.get(Calendar.MONTH)== selectedDate.get(Calendar.MONTH) && days[position].equals(""+selectedDate.get(Calendar.DAY_OF_MONTH))) {
v.setBackgroundResource(R.drawable.item_background_focused);
}
else {
v.setBackgroundResource(R.drawable.list_item_background);
}
}
dayView.setText(days[position]);
// create date string for comparison
String date = days[position];
if(date.length()==1) {
date = "0"+date;
}
String monthStr = ""+(month.get(Calendar.MONTH)+1);
if(monthStr.length()==1) {
monthStr = "0"+monthStr;
}
// show icon if date is not empty and it exists in the items array
return v;
}
// refresh days of month .
public void refreshDays()
{
// clear items
items.clear();
int lastDay = month.getActualMaximum(Calendar.DAY_OF_MONTH);
int firstDay = (int)month.get(Calendar.DAY_OF_WEEK);
// figure size of the array
if(firstDay==1){
days = new String[lastDay+(FIRST_DAY_OF_WEEK*6)];
}
else {
days = new String[lastDay+firstDay-(FIRST_DAY_OF_WEEK+1)];
}
int j=FIRST_DAY_OF_WEEK;
// populate empty days before first real day
if(firstDay>1) {
for(j=0;j<firstDay-FIRST_DAY_OF_WEEK;j++) {
days[j] = "";
}
}
else {
for(j=0;j<FIRST_DAY_OF_WEEK*6;j++) {
days[j] = "";
}
j=FIRST_DAY_OF_WEEK*6+1; // sunday => 1, monday => 7
}
// populate days
int dayNumber = 1;
for(int i=j-1;i<days.length;i++) {
days[i] = ""+dayNumber;
dayNumber++;
}
}
// references to our items
public String[] days;
}
答案 0 :(得分:1)
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.examples.android.calendar/android.util.EventLog$Event}; have you declared this activity in your AndroidManifest.xml?
您可以打开另一个活动onItemclick gridview。您的logcat显示您忘记在清单文件中注册您的活动。确保您已在清单文件中声明了您的活动。
<强>编辑:强>
您正在重定向到Event
上的onItemClicklistener
班级,并且您尚未在清单文件中声明您的Event
活动。在您的清单文件中声明它。
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".CalendarViewSampleActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Event"> </activity> //Declared this line in your manifest.
</application>
同时删除已在清单中声明的Adapter类。 <activity android:name="com.examples.android.calendar.CalendarAdapter"> </activity>
适配器永远不会在清单中声明。
答案 1 :(得分:0)
即使您已经抵制,也要尝试以正确的方式放置,
我认为您已在申请标记之外宣布您的活动。
我认为它会起作用..
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".CalendarViewSampleActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.examples.android.calendar.CalendarView"
android:label="com.examples.android.calendar.CalendarView"> </activity>
<activity android:name="com.examples.android.calendar.CalendarAdapter"
android:label="com.examples.android.calendar.CalendarAdapter" > </activity>
</application>
答案 2 :(得分:0)
你有“android.content.ActivityNotFoundException”异常......这意味着你没有把你的活动放在清单中......