当前位置: 首页 > 知识库问答 >
问题:

“不幸的是‘应用程序’已经停止”错误[重复]

秦安怡
2023-03-14

这个想法是在警报开始时启动一个游戏。我在下面展示了manifest.xml。如果我犯了错误,请纠正我。因为我刚接触Android系统,所以我无法理解它。PS:我已经搜索了其他类似的问题,并纠正了许多其他错误,但问题仍然存在。

这是我从教程(AndroidTimeActivity)中获得的警报代码:

public class AndroidTimeActivity extends Activity {

    TimePicker myTimePicker;
    Button buttonstartSetDialog;
    TextView textAlarmPrompt;

    TimePickerDialog timePickerDialog;

    final static int RQS_1 = 1;

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    textAlarmPrompt = (TextView)findViewById(R.id.alarmprompt);

    buttonstartSetDialog = (Button)findViewById(R.id.startSetDialog);
    buttonstartSetDialog.setOnClickListener(new OnClickListener(){

                        public void onClick(View v) {
                textAlarmPrompt.setText("");
                openTimePickerDialog(false);

            }});

}


    private void openTimePickerDialog(boolean is24r){
        Calendar calendar = Calendar.getInstance();

        timePickerDialog = new TimePickerDialog(
                AndroidTimeActivity.this, 
                onTimeSetListener, 
                calendar.get(Calendar.HOUR_OF_DAY), 
                calendar.get(Calendar.MINUTE), 
                is24r);
        timePickerDialog.setTitle("Set Alarm Time");  

        timePickerDialog.show();

    }

OnTimeSetListener onTimeSetListener= new OnTimeSetListener(){


        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

            Calendar calNow = Calendar.getInstance();
            Calendar calSet = (Calendar) calNow.clone();

            calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
            calSet.set(Calendar.MINUTE, minute);
            calSet.set(Calendar.SECOND, 0);
            calSet.set(Calendar.MILLISECOND, 0);

            if(calSet.compareTo(calNow) <= 0){
                //Today Set time passed, count to tomorrow
                calSet.add(Calendar.DATE, 1);
            }

            setAlarm(calSet);
        }};

    private void setAlarm(Calendar targetCal){

        textAlarmPrompt.setText(
                "\n\n***\n"
                + "Alarm is set@ " + targetCal.getTime() + "\n"
                + "***\n");

Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(),RQS_1,                  
intent, 0);     
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(),                                                            
pendingIntent);

    }


}

这是AlarmReceiver类:

 public class AlarmReceiver extends BroadcastReceiver {

@Override
    public void onReceive(Context context, Intent arg1) {
         Intent activityIntent = new Intent(context, Manager.class);                    
        activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(activityIntent);

}

 }
       public class Manager extends Activity {
       private static int ROW_COUNT = -1;
    private static int COL_COUNT = -1;
    private Context context;
    private Drawable backImage;
    private int [] [] cards;
    private List<Drawable> images;
    private Card firstCard;
    private Card seconedCard;
    private ButtonListener buttonListener;

    private static Object lock = new Object();

    int turns;
    private TableLayout mainTable;
    private UpdateCardsHandler handler;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);



        handler = new UpdateCardsHandler();
        loadImages();
        setContentView(R.layout.main);


       backImage =  getResources().getDrawable(R.drawable.icon);

 /*
       ((Button)findViewById(R.id.ButtonNew)).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            newGame();

        }


    });*/

       buttonListener = new ButtonListener();

       mainTable = (TableLayout)findViewById(R.id.TableLayout03);


       context  = mainTable.getContext();

        Spinner s = (Spinner) findViewById(R.id.Spinner01);
     ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.type,  
     android.R.layout.simple_spinner_item);                     

     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            s.setAdapter(adapter);

            s.setOnItemSelectedListener(new OnItemSelectedListener(){

              public void onItemSelected(
                      android.widget.AdapterView<?> arg0, 
                      View arg1, int pos, long arg3){

                  ((Spinner) findViewById(R.id.Spinner01)).setSelection(0);

                int x,y;

                switch (pos) {
                case 1:
                    x=4;y=4;
                    break;
                case 2:
                    x=4;y=5;
                    break;
                case 3:
                    x=4;y=6;
                    break;
                case 4:
                    x=5;y=6;
                    break;
                case 5:
                    x=6;y=6;
                    break;
                default:
                    return;
                }
                newGame(x,y);

            }



            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }

        });
    }

    private void newGame(int c, int r) {
        ROW_COUNT = r;
        COL_COUNT = c;

        cards = new int [COL_COUNT] [ROW_COUNT];


        mainTable.removeView(findViewById(R.id.TableRow01));
        mainTable.removeView(findViewById(R.id.TableRow02));

        TableRow tr = ((TableRow)findViewById(R.id.TableRow03));
        tr.removeAllViews();

        mainTable = new TableLayout(context);
        tr.addView(mainTable);

         for (int y = 0; y < ROW_COUNT; y++) {
             mainTable.addView(createRow(y));
          }

         firstCard=null;
         loadCards();

         turns=0;
         ((TextView)findViewById(R.id.tv1)).setText("Tries: "+turns);


    }

    private void loadImages() {
        images = new ArrayList<Drawable>();

        images.add(getResources().getDrawable(R.drawable.card1));
        images.add(getResources().getDrawable(R.drawable.card2));
        images.add(getResources().getDrawable(R.drawable.card3));
        images.add(getResources().getDrawable(R.drawable.card4));
        images.add(getResources().getDrawable(R.drawable.card5));
        images.add(getResources().getDrawable(R.drawable.card6));
        images.add(getResources().getDrawable(R.drawable.card7));
        images.add(getResources().getDrawable(R.drawable.card8));
        images.add(getResources().getDrawable(R.drawable.card9));
        images.add(getResources().getDrawable(R.drawable.card10));
        images.add(getResources().getDrawable(R.drawable.card11));
        images.add(getResources().getDrawable(R.drawable.card12));
        images.add(getResources().getDrawable(R.drawable.card13));
        images.add(getResources().getDrawable(R.drawable.card14));
        images.add(getResources().getDrawable(R.drawable.card15));
        images.add(getResources().getDrawable(R.drawable.card16));
        images.add(getResources().getDrawable(R.drawable.card17));
        images.add(getResources().getDrawable(R.drawable.card18));
        images.add(getResources().getDrawable(R.drawable.card19));
        images.add(getResources().getDrawable(R.drawable.card20));
        images.add(getResources().getDrawable(R.drawable.card21));

    }

    private void loadCards(){
        try{
            int size = ROW_COUNT*COL_COUNT;

            Log.i("loadCards()","size=" + size);

            ArrayList<Integer> list = new ArrayList<Integer>();

            for(int i=0;i<size;i++){
                list.add(new Integer(i));
            }


            Random r = new Random();

            for(int i=size-1;i>=0;i--){
                int t=0;

                if(i>0){
                    t = r.nextInt(i);
                }

                t=list.remove(t).intValue();
                cards[i%COL_COUNT][i/COL_COUNT]=t%(size/2);

                   Log.i("loadCards()", "card["+(i%COL_COUNT)+"]["+(i/COL_COUNT)+"]="+  
                   cards[i%COL_COUNT][i/COL_COUNT]);  
            }
        }
        catch (Exception e) {
            Log.e("loadCards()", e+"");
        }

    }

    private TableRow createRow(int y){
         TableRow row = new TableRow(context);
         row.setHorizontalGravity(Gravity.CENTER);

         for (int x = 0; x < COL_COUNT; x++) {
                 row.addView(createImageButton(x,y));
         }
         return row;
    }

    private View createImageButton(int x, int y){
        Button button = new Button(context);
        button.setBackgroundDrawable(backImage);
        button.setId(100*x+y);
        button.setOnClickListener(buttonListener);
        return button;
    }

    class ButtonListener implements OnClickListener {


        public void onClick(View v) {

            synchronized (lock) {
                if(firstCard!=null && seconedCard != null){
                    return;
                }
                int id = v.getId();
                int x = id/100;
                int y = id%100;
                turnCard((Button)v,x,y);
            }

        }

        private void turnCard(Button button,int x, int y) {
            button.setBackgroundDrawable(images.get(cards[x][y]));

            if(firstCard==null){
                firstCard = new Card(button,x,y);
            }
            else{ 

                if(firstCard.x == x && firstCard.y == y){
                    return; //the user pressed the same card
                }

                seconedCard = new Card(button,x,y);

                turns++;
            ((TextView)findViewById(R.id.tv1)).setText("Tries: "+turns);


                TimerTask tt = new TimerTask() {

                    @Override
                    public void run() {
                        try{
                            synchronized (lock) {
                              handler.sendEmptyMessage(0);
                            }
                        }
                        catch (Exception e) {
                            Log.e("E1", e.getMessage());
                        }
                    }
                };

                  Timer t = new Timer(false);
                    t.schedule(tt, 1300);
            }


           }

        }

    class UpdateCardsHandler extends Handler{

        @Override
        public void handleMessage(Message msg) {
            synchronized (lock) {
                checkCards();
            }
        }
     public void checkCards(){
        if(cards[seconedCard.x][seconedCard.y] == cards[firstCard.x][firstCard.y]){
                    firstCard.button.setVisibility(View.INVISIBLE);
                    seconedCard.button.setVisibility(View.INVISIBLE);
                }
                else {
                     seconedCard.button.setBackgroundDrawable(backImage);
                    firstCard.button.setBackgroundDrawable(backImage);
                }

                firstCard=null;
                seconedCard=null;
            }
    }




    }
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exercise.AndroidTime"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="10" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name="com.exercise.AndroidTime.AndroidTimeActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name=".AlarmReceiver" android:process=":remote" />
    <activity android:name="com.exercise.AndroidTime.Manager"></activity>
</application>

</manifest>

共有1个答案

王辉
2023-03-14
public class AlarmReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent arg1) {
  Intent activityIntent = new Intent(context,
                            Manager.class);
            activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(activityIntent);

    }

 }
 类似资料:
  • 我想在通知上打开我的MapsActive单击通知正常工作,但当我单击通知时,它会让我停止应用程序 通知活动。班 地图civity.class activity_map LogCat ethod.invokeActivityTcom.android.internal.os.LaunchActive(ActivityTaller.run)atnit.java:726ActivityTcom.andro

  • 我在Mac上的Eclipse中创建了一个新的Android应用程序项目。我设置了它Android4.4.2 sdk。 当我在物理Nexus 7平板电脑中安装此示例项目时,它会抛出以下启动错误。我尚未在此项目中添加任何内容,它只是一个按原样创建的示例项目。 不幸的是已经停止了 代码: Android智能.xml: 对于,值/styles.xml和值-v11/styles.xml和值-v14/styl

  • 这个想法是在警报开始时启动一个游戏。我已经在下面展示了manifest.xml。如果我犯了错误,请纠正我。因为我是Android的新手,所以我不太懂。PS:我已经搜索了其他类似的问题,并更正了很多其他错误,但问题依然存在。 这是我从一个教程(AndroidTimeActivity)得到的警报代码: 这是AlarmReceiver类: 这里是Manager类,当警报接收器接收到意图时,我要调用它:

  • 代码 和logcat 06-03 16:59:26.570 972 0-9757/com.example.abhijitroy.superhero E/AndroidRuntime:致命异常:AsyncTask#1进程:com.example.abhijitroy.superhero,PID:9720 java.lang.RuntimeException:在Android.os.AsyncTask

  • 我对Android编程完全陌生。我正在制作的第一个应用程序遇到了一些问题。每次我运行应用程序时,我的手机(我正在使用它来测试和调试我的应用程序)上会弹出一条消息说“不幸的是,MyNewApp3已停止”。这个错误甚至无法打开我的应用程序。我如何解决这个问题? 这是我的代码: 我的LogCat输出(这是我在看到别人建议别人放置LogCat输出后发布的;我不知道它是用来做什么的): 01-12 11:4

  • 我在Android Studio 6.0上的模拟器出现了上述崩溃,但在我的设备6.0.1上没有崩溃。模拟器有时会启动相机,但它大多会崩溃,在logcat中没有任何东西来给我指明正确的方向。有人知道这里可能发生了什么吗? 此外,这里有一个图像,当它确实通过。image_capture_camera logcat: