当我单击按钮时,我的应用程序停止。我创建了一个应用程序,当我单击NavigationView中的菜单时,它将显示一个BottomSheet,然后在ButtomSheet中,有一个按钮应该单击以打开DatePickerDialog。但在此之前,当加载配置文件(MainActivity)时,它将停止我的程序。
从MainActivity到NavigationView到Modal的一切工作……直到我在Profile.java(MainActivity)内将其编码(下面的代码)
btnDateTime = (Button) findViewById(R.id.btnDateTime);
txtDateTime =(TextView)findViewById(R.id.txtDateTime);
btnDateTime.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DAY_OF_MONTH);
int month =calendar.get(Calendar.MONTH);
int year =calendar.get(Calendar.YEAR);
datePickerDialog = new DatePickerDialog(Profile.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
txtDateTime.setText(dayOfMonth +"/" + (month +1)+"/"+year);
}
},year,month,day);
datePickerDialog.show();
}
});
但是当我删除上述代码行时,一切正常。请帮助我,我真的需要你们(大师)
这是我的个人资料(MainActivity.java)
package com.example.khalessi_jb.reminderapplication;
import android.app.DatePickerDialog;
import android.content.ClipData;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.util.Calendar;
public class Profile extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener{
private DrawerLayout mDrawerLlayout;
private ActionBarDrawerToggle mToggle;
TextView txtFullname;
TextView txtDateTime;
Button btnDateTime;
Calendar calendar;
DatePickerDialog datePickerDialog;
int [] images ={R.drawable.work,R.drawable.location,R.drawable.people};
String [] reminder = {"Finish SBTI Report","21st Anniversary","Managerial Meeting"};
String [] dateToFinish ={"November 1, 2018","December 8, 2018","November 2, 2018"};
View view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
btnDateTime = (Button) findViewById(R.id.btnDateTime);
txtDateTime =(TextView)findViewById(R.id.txtDateTime);
btnDateTime.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DAY_OF_MONTH);
int month =calendar.get(Calendar.MONTH);
int year =calendar.get(Calendar.YEAR);
datePickerDialog = new DatePickerDialog(Profile.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
txtDateTime.setText(dayOfMonth +"/" + (month +1)+"/"+year);
}
},year,month,day);
datePickerDialog.show();
}
});
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
mDrawerLlayout = (DrawerLayout)findViewById(R.id.drawerlayout);
mToggle = new ActionBarDrawerToggle(this,mDrawerLlayout,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close);
mDrawerLlayout.addDrawerListener(mToggle);
mToggle.syncState();
view = this.getWindow().getDecorView();
view.setBackgroundResource(R.color.colorLoginBackground);
ListView ListView =(ListView)findViewById(R.id.lvRemindersList);
CustomAdapter customAdapter = new CustomAdapter();
ListView.setAdapter(customAdapter);
}
@Override
public void onBackPressed(){
if(mDrawerLlayout.isDrawerOpen(GravityCompat.START)){
mDrawerLlayout.closeDrawer(GravityCompat.START);
}else{
super.onBackPressed();
}
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_Account:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new ModalEvent()).commit();
break;
case R.id.nav_eventreminder:
ModalEvent modalEvent = new ModalEvent();
modalEvent.show(getSupportFragmentManager(),"Showed");
break;
case R.id.nav_reminder:
break;
case R.id.nav_appointmentslots:
break;
case R.id.nav_logout:
break;
}
mDrawerLlayout.closeDrawer(GravityCompat.START);
return true;
}
class CustomAdapter extends BaseAdapter{
@Override
public int getCount() {
return images.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
view = getLayoutInflater().inflate(R.layout.customlistview,null);
ImageView imageView = (ImageView)view.findViewById(R.id.imageViewer);
TextView reminders = (TextView)view.findViewById(R.id.txtReminder);
TextView date = (TextView)view.findViewById(R.id.txtDate);
imageView.setImageResource(images[position]);
reminders.setText(reminder[position]);
date.setText(dateToFinish[position]);
return view;
}
}
}
activity_profile.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawerlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".Profile">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"></android.support.v7.widget.Toolbar>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
<ImageView
android:id="@+id/imPerson"
android:layout_width="66dp"
android:layout_height="62dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="10dp"
android:layout_marginTop="59dp"
app:srcCompat="@drawable/profile" />
<TextView
android:id="@+id/txtFullname"
android:layout_width="274dp"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imPerson"
android:layout_marginStart="6dp"
android:layout_marginTop="0dp"
android:layout_toEndOf="@+id/imPerson"
android:text="John Bryan Cantiveros"
android:textColor="@color/white"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="@+id/txtDesignation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtFullname"
android:layout_alignStart="@+id/txtFullname"
android:layout_marginStart="1dp"
android:layout_marginTop="1dp"
android:text="Programmer"
android:textColor="@color/white" />
<TextView
android:id="@+id/txtBranch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtDesignation"
android:layout_alignStart="@+id/txtFullname"
android:layout_marginStart="1dp"
android:layout_marginTop="0dp"
android:text="Lagao DJ Interprises"
android:textColor="@color/white" />
<View
android:layout_width="wrap_content"
android:layout_height="7dp"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="10dp"
android:layout_marginLeft="80dp"
android:layout_marginTop="130dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="80dp"
android:layout_marginBottom="80dp"
android:background="@color/Yellow" />
<ListView
android:id="@+id/lvRemindersList"
android:layout_width="wrap_content"
android:layout_height="422dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="10dp"
android:layout_marginTop="135dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:background="@color/white" />
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_gravity="start"
app:headerLayout="@layout/navigation_header"
app:menu="@menu/navigation_menu"/>
</android.support.v4.widget.DrawerLayout>
我的ModalEvent.java
package com.example.khalessi_jb.reminderapplication;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetDialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ModalEvent extends BottomSheetDialogFragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.modal_event,container,false);
return v;
}
}
我的modal_event.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/textView12"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Event" />
<EditText
android:id="@+id/txtEventName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView12"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="Event Title"
android:inputType="textLongMessage" />
<EditText
android:id="@+id/txtWhere"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView15"
android:layout_alignStart="@+id/textView13"
android:ems="10"
android:hint="Event Location"
android:inputType="textLongMessage" />
<TextView
android:id="@+id/textView13"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txtEventName"
android:layout_alignStart="@+id/txtEventName"
android:layout_marginTop="-1dp"
android:text="When" />
<EditText
android:id="@+id/txtDateTime"
android:layout_width="148dp"
android:layout_height="wrap_content"
android:layout_below="@+id/textView13"
android:layout_alignStart="@+id/textView12"
android:layout_marginStart="205dp"
android:layout_marginTop="0dp"
android:ems="10"
android:hint="Date/Time"
android:inputType="time"
android:textSize="15dp" />
<TextView
android:id="@+id/textView15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView10"
android:layout_alignStart="@+id/textView13"
android:layout_marginStart="4dp"
android:text="Where" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView12"
android:layout_alignParentEnd="true"
android:layout_marginTop="216dp"
android:layout_marginEnd="132dp"
android:text="Create" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button"
android:layout_alignParentEnd="true"
android:layout_marginTop="-1dp"
android:layout_marginEnd="15dp"
android:text="Close" />
<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/txtDateTime"
android:layout_alignParentStart="true"
android:layout_marginStart="0dp"
android:layout_marginEnd="21dp"
android:layout_toStartOf="@+id/txtDateTime"
android:text="at" />
<TextView
android:id="@+id/textView14"
android:layout_width="124dp"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/button"
android:layout_alignParentTop="true"
android:layout_marginStart="96dp"
android:layout_marginTop="378dp" />
<Button
android:id="@+id/btnDateTime"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_below="@+id/textView13"
android:layout_alignParentStart="true"
android:layout_marginStart="-4dp"
android:text="Choose Date/Time" />
</RelativeLayout>
logcat
2018-11-06 20:32:19.439 20721-
20721/com.example.khalessi_jb.reminderapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.khalessi_jb.reminderapplication, PID: 20721
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.khalessi_jb.reminderapplication/com.example.khalessi_jb.reminderapplication.Profile}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2724)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2789)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6259)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.khalessi_jb.reminderapplication.Profile.onCreate(Profile.java:55)
at android.app.Activity.performCreate(Activity.java:6666)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2677)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2789)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6259)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
答案 0 :(得分:0)
您需要从正在加载的XML中找到按钮
View v = inflater.inflate(R.layout.modal_event,container,false);
Button btnDateTime = (Button) findViewById(...);
btnDateTime.setOnClick...
return v;