在更改网格视图以链接 xml 布局时显示 log cat 错误

Posted

技术标签:

【中文标题】在更改网格视图以链接 xml 布局时显示 log cat 错误【英文标题】:on changes to grid view to link xml layout shows log cat error 【发布时间】:2013-12-10 06:17:04 【问题描述】:

我已经用新的错误更改了日志猫。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_
            android:layout_
            android:orientation="vertical" >

            <TableLayout
                android:layout_
                android:layout_ >

                <TableRow
                    android:id="@+id/tableRow1"
                    android:layout_
                    android:layout_ >

                    <TextView
                        android:id="@+id/textView1"
                        android:layout_
                        android:layout_
                        android:text="Event Date"
                        android:textAppearance="?android:attr/textAppearanceMedium" />

                    <android.widget.Space
                        android:id="@+id/space1"
                        android:layout_
                        android:layout_ />

                    <TextView
                        android:id="@+id/textView2"
                        android:layout_
                        android:layout_
                        android:text="Event Discription"
                        android:textAppearance="?android:attr/textAppearanceMedium" />

                </TableRow>

                <TableRow
                    android:id="@+id/tableRow2"
                    android:layout_
                    android:layout_ >
                </TableRow>

            </TableLayout>

        </RelativeLayout>

Emulator 被强制停止在这个 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;
        

【问题讨论】:

你能把你的androidmanifest.xml文件和你写的代码贴出来吗.. 我已经发布了答案.. 请帮我解决这个问题,我什至清理了项目。 我已经更新了代码和新的日志猫错误 【参考方案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?

您可能会在您的gridview 的onItemclick 上打开另一个活动。您的 logcat 显示您忘记在清单文件中注册您的活动。确保您已在清单文件中声明您的活动。

已编辑:

您正在重定向到onItemClicklistener 上的Event 类,并且您尚未在清单文件中声明您的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>

同时删除您在清单中声明的​​适配器类。 &lt;activity android:name="com.examples.android.calendar.CalendarAdapter"&gt; &lt;/activity&gt; 适配器从未在清单中声明。

【讨论】:

您未发布的清单所需的代码。而且您还删除了 logcat。为什么? 你能用新的 logcat 错误和一些包含错误的相关代码来更新你的问题吗?【参考方案2】:

你有 "android.content.ActivityNotFoundException" Excpetion...这意味着您没有将您的 Activity 放入 Manifest...

【讨论】:

【参考方案3】:

即使你已经抗拒了,那么尝试以正确的方式放置。

我认为您已在应用程序标签之外声明您的活动

我认为它会起作用..

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

【讨论】:

以上是关于在更改网格视图以链接 xml 布局时显示 log cat 错误的主要内容,如果未能解决你的问题,请参考以下文章

工具栏折叠时显示视图

UIPageViewController在以编程方式设置时显示空白视图控制器

单击链接时显示警报视图

当表单中没有任何更改时显示警报框

在 extjs 网格视图中显示文本框

如何在 CRM 的子网格形式中单击按钮时显示查找窗口?