通过按钮打开新活动导致Android应用崩溃

时间:2018-12-13 03:52:28

标签: android fatal-error

此处是发生致命异常的日志记录器。单击日期和时间按钮后,第二个按钮应该导致新活动。

2018-12-12 19:41:43.911 1385-1422/? W/audio_hw_generic: Not supplying enough         
data to HAL, expected position 2007618 , only wrote 2007360
2018-12-12 19:41:44.098 4585-4585/com.example.vanessamontiel.eventplanner     
D/AndroidRuntime: Shutting down VM
2018-12-12 19:41:44.099 4585-4585/com.example.vanessamontiel.eventplanner 
E/AndroidRuntime: FATAL EXCEPTION: main

这是活动的代码:第一个按钮有效,第二个按钮无效。第一次我们对View v遇到问题,我们将View v1放到了无法使用的位置,但是这次我们删除了它,现在仍然无法使用。我们不知道可能是什么问题。这是针对我们的Android App Studio类的项目。

此外,当您单击不起作用的按钮时,它将转到“列表活动”。也许与错误有关。

public class MainActivity extends AppCompatActivity {
private TextView reservation;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //displays icon on top bar
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setLogo(R.mipmap.ic_launcher);
    getSupportActionBar().setDisplayUseLogoEnabled(true);

    reservation = (TextView)findViewById(R.id.txtReservation);
    Button button = (Button)findViewById(R.id.btnDate);
    Button button1 = (Button)findViewById(R.id.btnType);

    // define setOnClickListener for button
    button.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            new DatePickerDialog(MainActivity.this, d, c.get(Calendar.YEAR), 
c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH)).show();
        } // end onClick
    }); // end setOnClickListener

    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(MainActivity.this, 
TypeActivity.class));
        }
    });

} // end onCreate
Calendar c = Calendar.getInstance();
DateFormat fmtDate = DateFormat.getDateInstance();

DatePickerDialog.OnDateSetListener d = new 
DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int month, int 
dayOfMonth) {
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month);
        c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        new TimePickerDialog(MainActivity.this, t, 
c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), false).show();
        //reservation.setText("Your event date is set for " + 
fmtDate.format(c.getTime()));
    }
}; // end DatePickerDialog

TimePickerDialog.OnTimeSetListener t = new 
 TimePickerDialog.OnTimeSetListener() {
    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        c.set(Calendar.HOUR_OF_DAY, hourOfDay);
        c.set(Calendar.MINUTE, minute);
        String amPm;
        if(hourOfDay >= 12){
            amPm = "PM";
        }
        else {
            amPm = "AM";
        }
        reservation.setText("Your event date is set for " +     
fmtDate.format(c.getTime()) + " at " + hourOfDay + ":" + minute + " " + 
amPm);
    }
};
} // end MainActivity class

编辑:logcat中的红线代码:

2018-12-12 19:41:44.099 4585-4585 / com.example.vanessamontiel.eventplanner E / Android运行时:致命异常:主要     流程:com.example.vanessamontiel.eventplanner,PID:4585     java.lang.RuntimeException:无法在项目布局中找到ID为com.example.vanessamontiel.eventplanner:id / balloons的视图         在android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)         在android.widget.ArrayAdapter.getView(ArrayAdapter.java:371)         在android.widget.AbsListView.obtainView(AbsListView.java:2363)         在android.widget.ListView.makeAndAddView(ListView.java:1970)         在android.widget.ListView.fillDown(ListView.java:704)         在android.widget.ListView.fillFromTop(ListView.java:765)         在android.widget.ListView.layoutChildren(ListView.java:1744)         在android.widget.AbsListView.onLayout(AbsListView.java:2162)         在android.view.View.layout(View.java:17637)         在android.view.ViewGroup.layout(ViewGroup.java:5575)         在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)         在android.widget.FrameLayout.onLayout(FrameLayout.java:261)         在android.view.View.layout(View.java:17637)         在android.view.ViewGroup.layout(ViewGroup.java:5575)         在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)         在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)         在android.widget.LinearLayout.onLayout(LinearLayout.java:1494)         在android.view.View.layout(View.java:17637)         在android.view.ViewGroup.layout(ViewGroup.java:5575)         在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)         在android.widget.FrameLayout.onLayout(FrameLayout.java:261)         在com.android.internal.policy.DecorView.onLayout(DecorView.java:726)         在android.view.View.layout(View.java:17637)         在android.view.ViewGroup.layout(ViewGroup.java:5575)         在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2346)         在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2068)         在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)         在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6337)         在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:874)         在android.view.Choreographer.doCallbacks(Choreographer.java:686)         在android.view.Choreographer.doFrame(Choreographer.java:621)         在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:860)         在android.os.Handler.handleCallback(Handler.java:751)         在android.os.Handler.dispatchMessage(Handler.java:95)         在android.os.Looper.loop(Looper.java:154)         在android.app.ActivityThread.main(ActivityThread.java:6119)         在java.lang.reflect.Method.invoke(本机方法)         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

0 个答案:

没有答案