活动作为主要活动成功启动,但应用程序在通过按钮单击启动时崩溃

时间:2013-09-09 10:11:26

标签: android android-intent

Iam尝试通过“ManagerDashboard.class”中的按钮点击事件启动“ CalendarView.class ,但应用在按钮点击期间崩溃。 CalendarView.class 如果我将其作为清单中的主要活动,则会成功启动。

我试图浏览类似的问题,但没有找到我想要的答案。

Logcat输出:

09-09 09:54:02.496: E/AndroidRuntime(1645): FATAL EXCEPTION: main
09-09 09:54:02.496: E/AndroidRuntime(1645): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.demi.attendance/android.widget.CalendarView}; have you declared this activity in your AndroidManifest.xml?
09-09 09:54:02.496: E/AndroidRuntime(1645):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1618)
09-09 09:54:02.496: E/AndroidRuntime(1645):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
09-09 09:54:02.496: E/AndroidRuntime(1645):     at android.app.Activity.startActivityForResult(Activity.java:3370)
09-09 09:54:02.496: E/AndroidRuntime(1645):     at android.app.Activity.startActivityForResult(Activity.java:3331)
09-09 09:54:02.496: E/AndroidRuntime(1645):     at android.app.Activity.startActivity(Activity.java:3566)
09-09 09:54:02.496: E/AndroidRuntime(1645):     at android.app.Activity.startActivity(Activity.java:3534)
09-09 09:54:02.496: E/AndroidRuntime(1645):     at com.demi.manager.ManagerDashboard$1.onClick(ManagerDashboard.java:42)
09-09 09:54:02.496: E/AndroidRuntime(1645):     at android.view.View.performClick(View.java:4204)
09-09 09:54:02.496: E/AndroidRuntime(1645):     at android.view.View$PerformClick.run(View.java:17355)
09-09 09:54:02.496: E/AndroidRuntime(1645):     at android.os.Handler.handleCallback(Handler.java:725)
09-09 09:54:02.496: E/AndroidRuntime(1645):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-09 09:54:02.496: E/AndroidRuntime(1645):     at android.os.Looper.loop(Looper.java:137)
09-09 09:54:02.496: E/AndroidRuntime(1645):     at android.app.ActivityThread.main(ActivityThread.java:5041)
09-09 09:54:02.496: E/AndroidRuntime(1645):     at java.lang.reflect.Method.invokeNative(Native Method)
09-09 09:54:02.496: E/AndroidRuntime(1645):     at java.lang.reflect.Method.invoke(Method.java:511)
09-09 09:54:02.496: E/AndroidRuntime(1645):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-09 09:54:02.496: E/AndroidRuntime(1645):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-09 09:54:02.496: E/AndroidRuntime(1645):     at dalvik.system.NativeStart.main(Native Method)
09-09 10:21:59.325: E/Trace(2139): error opening trace file: No such file or directory (2)

清单:

    <application
    android:allowBackup="true"
    android:icon="@drawable/mainicon"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:uiOptions="splitActionBarWhenNarrow"
    android:vmSafeMode="true" >

<activity
            android:name="com.demi.manager.ManagerDashboard"
            android:configChanges="orientation"
            android:screenOrientation="portrait" >
               <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
            <activity
            android:name="com.demi.calendar.CalendarView"
            android:configChanges="orientation"
            android:screenOrientation="portrait" >
        </activity>

      </application>

CalendarView.java

       package com.demi.calendar;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.Locale;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.demi.attendance.R;
public class CalendarView extends Activity {

    public GregorianCalendar month, itemmonth;// calendar instances.

    public CalendarAdapter adapter;// adapter instance
    public Handler handler;// for grabbing some event values for showing the dot
                            // marker.
    public ArrayList<String> items; // container to store calendar items which
                                    // needs showing the event marker
    ArrayList<String> event;
    LinearLayout rLayout;
    ArrayList<String> date;
    ArrayList<String> desc;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calendar);
        Locale.setDefault(Locale.US); 

        rLayout = (LinearLayout) findViewById(R.id.text);
        month = (GregorianCalendar) GregorianCalendar.getInstance();
        //itemmonth = (GregorianCalendar) month.clone();

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

        RelativeLayout previous = (RelativeLayout) findViewById(R.id.previous);

        previous.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                setPreviousMonth();
                refreshCalendar();
            } 
        });

        RelativeLayout next = (RelativeLayout) findViewById(R.id.next);
        next.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                setNextMonth();
                refreshCalendar();

            }
        });

        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {
                // removing the previous view if added
                if (((LinearLayout) rLayout).getChildCount() > 0) {
                    ((LinearLayout) rLayout).removeAllViews();
                }
                desc = new ArrayList<String>();
                date = new ArrayList<String>();
                ((CalendarAdapter) parent.getAdapter()).setSelected(v);
                String selectedGridDate = CalendarAdapter.dayString
                        .get(position);
                String[] separatedTime = selectedGridDate.split("-");
                String gridvalueString = separatedTime[2].replaceFirst("^0*",
                        "");// taking last part of date. ie; 2 from 2012-12-02.
                int gridvalue = Integer.parseInt(gridvalueString);
                // navigate to next or previous month on clicking offdays.
                if ((gridvalue > 10) && (position < 8)) {
                    setPreviousMonth();
                    refreshCalendar();
                } else if ((gridvalue < 7) && (position > 28)) {
                    setNextMonth();
                    refreshCalendar();
                }
                ((CalendarAdapter) parent.getAdapter()).setSelected(v);

                for (int i = 0; i < Utility.startDates.size(); i++) {
                    if (Utility.startDates.get(i).equals(selectedGridDate)) {
                        desc.add(Utility.nameOfEvent.get(i));
                    }
                }

                if (desc.size() > 0) {
                    for (int i = 0; i < desc.size(); i++) {
                        TextView rowTextView = new TextView(CalendarView.this);

                        // set some properties of rowTextView or something
                        rowTextView.setText("Event:" + desc.get(i));
                        rowTextView.setTextColor(Color.BLACK);

                        // add the textview to the linearlayout
                        rLayout.addView(rowTextView);

                    }

                } 

                desc = null;

            }

        });
    }

    protected void setNextMonth() {
        if (month.get(GregorianCalendar.MONTH) == month
                .getActualMaximum(GregorianCalendar.MONTH)) {
            month.set((month.get(GregorianCalendar.YEAR) + 1),
                    month.getActualMinimum(GregorianCalendar.MONTH), 1);
        } else {
            month.set(GregorianCalendar.MONTH,
                    month.get(GregorianCalendar.MONTH) + 1);
        }

    }

    protected void setPreviousMonth() {
        if (month.get(GregorianCalendar.MONTH) == month
                .getActualMinimum(GregorianCalendar.MONTH)) {
            month.set((month.get(GregorianCalendar.YEAR) - 1),
                    month.getActualMaximum(GregorianCalendar.MONTH), 1);
        } else {
            month.set(GregorianCalendar.MONTH,
                    month.get(GregorianCalendar.MONTH) - 1);
        }

    }

    protected void showToast(String string) {
        Toast.makeText(this, string, Toast.LENGTH_SHORT).show();

    }

    public void refreshCalendar() {
        TextView title = (TextView) findViewById(R.id.title);

        adapter.refreshDays();
        adapter.notifyDataSetChanged();
        handler.post(calendarUpdater); // generate some calendar items

        title.setText(android.text.format.DateFormat.format("MMMM yyyy", month));
    }

    public Runnable calendarUpdater = new Runnable() {

        @Override
        public void run() {
            items.clear();

            // Print dates of the current week
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
            String itemvalue;
            /// = Utility.readCalendarEvent(CalendarView.this);
            //Log.d("=====Event====", event.toString());
            Log.d("=====Date ARRAY====", Utility.startDates.toString());

            /*for (int i = 0; i < Utility.startDates.size(); i++) {
                itemvalue = df.format(itemmonth.getTime());
                itemmonth.add(GregorianCalendar.DATE, 1);
                items.add(Utility.startDates.get(i).toString());
            }*/
            adapter.setItems(items);
            adapter.notifyDataSetChanged();
        }
    };
}

ManagerDashboard.java

   public class ManagerDashboard extends Activity {
        Button create_project, view_project;
        Button take_attendance, view_attendance;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.manager_dashboard);
            init();
            setListener();
        }

        void init() {
            take_attendance = (Button) findViewById(R.id.manager_dashboard_button1);
            view_attendance = (Button) findViewById(R.id.manager_dashboard_button2);
            create_project = (Button) findViewById(R.id.manager_dashboard_button3);
            view_project = (Button) findViewById(R.id.manager_dashboard_button4);
        }

        void setListener() {
            take_attendance.setOnClickListener(new OnClickListener() {
                @SuppressLint("NewApi")
                public void onClick(View v) {
;
                    Intent intent=new Intent(getApplicationContext(),CalendarView.class);      //app crashes from here
                    startActivity(intent);
                }
            });
            view_attendance.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(ManagerDashboard.this,ViewAttendance.class);
                    startActivity(intent);
                }
            });
            create_project.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(ManagerDashboard.this,CreateProject.class);
                    startActivity(intent);
                }
            });

            view_project.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(ManagerDashboard.this,ViewProject.class);
                    startActivity(intent);
                }
            });
        }
    }

4 个答案:

答案 0 :(得分:1)

尝试使用ManagerDashboard.this代替getApplicationContext()

答案 1 :(得分:1)

ManagerDashboard 活动添加到 AndroidManifest 文件....

如果您遗失了,还要将 CalendarView 添加到 AndroidManifest 文件中,然后在该文件中添加<...>

改变这个:

  take_attendance.setOnClickListener(new OnClickListener() {
            @SuppressLint("NewApi")
            public void onClick(View v) {

                 Intent intent=new Intent(ManagerDashboard.this,CalendarView.class);      
                 startActivity(intent);
            }
        });

答案 2 :(得分:0)

Logcat错误清楚地表明,您忘记在清单文件中声明您的活动。请再次检查您的清单。唯一的问题是这个,没有别的。

答案 3 :(得分:0)

试试这种方式

take_attendance.setOnClickListener(new OnClickListener() {
                    @SuppressLint("NewApi")
                    public void onClick(View v) {
    ;
                        Intent intent=new Intent(ManagerDashboard.this,CalendarView.class);      //app crashes from here
                        startActivity(intent);
                    }
                });