gpt4 book ai didi

android - 在更改 GridView 以链接 xml 布局时显示 log cat 错误

转载 作者:行者123 更新时间:2023-11-30 03:13:48 25 4
gpt4 key购买 nike

我用新错误更改了日志猫。日志猫错误再次显示相同的错误,并且在日历中单击日期后模拟器强制关闭...我什至在 list 中声明了所有 Activity 然后错误继续....请帮忙

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 中如下。

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);


}

}
});
}

这是我的项目 list 文件,我已经更新了一些更改......

list 文件

    <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;
}

最佳答案

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?

您可能会在 GridView 的 Itemclick 上打开另一个 Activity 。您的 logcat 显示您忘记在 list 文件中注册您的 Activity 。确保您已在 list 文件中声明了您的 Activity 。

已编辑:

您正在重定向到 EventonItemClicklistener 上课而且你还没有申报你的 Event list 文件中的 Activity 。在您的 list 文件中声明它。

<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>

同时删除您在 list 中声明的​​ Adapter 类。 <activity android:name="com.examples.android.calendar.CalendarAdapter"> </activity>适配器从不在 list 中声明。

关于android - 在更改 GridView 以链接 xml 布局时显示 log cat 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20487485/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com