我收到以下错误:
java.lang.RuntimeException:无法启动活动 ComponentInfo {com.weslange.Term_Scheduling / com.weslange.Term_Scheduling.Term_ChangingDetails_Activity}: java.lang.NullPointerException:尝试调用虚方法' int com.weslange.Term_Scheduling.TermData.getTermID()'在null对象上 参考
以下似乎会导致此问题:$ git commit -m "$(git branch | sed -n 's/\* //p'): message"
我知道" getSupportActionBar().setDisplayHomeAsUpEnabled( true )
"确实有效,因为以下工作正常:getSupportActionBar()
MainActivity是根父级。 " getSupportActionBar().setTitle( "Selecting Courses" )
"是该活动的孩子。 " Term_ChangingDetails_Activity.java
"是" Term_CourseSelection_Activity.java
"的孩子。前两个与" Term_ChangingDetails_Activity.java
"。
我认为setDisplayHomeAsUpEnabled( true )
指的是我的" NullPointerException
"中的Parcelable。文件。 " Term_ChangingDetails_Activity.java
"包含异常的来源,当" Term_CourseSelection_Activity.java
"存在于该文件中。
我假设" setDisplayHomeAsUpEnabled( true )
"是正确的,因为如果我打电话给#34; AndroidManifest.xml
"在" finish()
"。
感谢您的时间。
编辑01:(为清楚起见,添加了MainActivity.java。)
MainActivity.java:
Term_ChangingDetails_Activity.java
Term_ChangingDetails_Activity.java:
public class MainActivity extends AppCompatActivity implements Term_RecyclerView_Adapter.ItemClickListener {
RecyclerView recyclerView;
Term_RecyclerView_Adapter adapter;
TermData test_TermData_Value;
Database_Handling databaseHandling;
List< TermData > termsArrayList;
List< CourseData > coursesArrayList;
List< MentorData > mentorsArrayList;
List< AssessmentData > assessmentsArrayList;
FloatingActionButton floatingActionButton;
@Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//This section ensures that each of the listings for the "Creation" activities begins with an example.
databaseHandling = new Database_Handling( this );
termsArrayList = databaseHandling.getAllTerms();
coursesArrayList = databaseHandling.getAllCourses();
mentorsArrayList = databaseHandling.getAllMentors();
assessmentsArrayList = databaseHandling.getAllAssessments();
if ( termsArrayList.isEmpty() ) {
TermData termDataBeginning = new TermData();
databaseHandling.addTermData( termDataBeginning );
//This line is necessary for this class, since the "recyclerView" listing needs populating.
termsArrayList = databaseHandling.getAllTerms();
}
if ( coursesArrayList.isEmpty() ) {
CourseData courseDataBeginning = new CourseData();
databaseHandling.addCourseData( courseDataBeginning );
}
if ( mentorsArrayList.isEmpty() ) {
MentorData mentorDataBeginning = new MentorData();
databaseHandling.addMentorData( mentorDataBeginning );
}
if ( assessmentsArrayList.isEmpty() ) {
AssessmentData assessmentDataThing = new AssessmentData();
databaseHandling.addAssessmentData( assessmentDataThing );
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
recyclerView = findViewById( R.id.recyclerView_Array_in_XML );
LinearLayoutManager linearLayoutManager = new LinearLayoutManager( this );
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(
recyclerView.getContext(), linearLayoutManager.getOrientation() );
recyclerView.addItemDecoration( dividerItemDecoration );
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
recyclerView.setLayoutManager( linearLayoutManager );
adapter = new Term_RecyclerView_Adapter( this, termsArrayList);
adapter.setClickListener( this );
recyclerView.setAdapter( adapter );
floatingActionButton = findViewById( R.id.floatingActionButton_Thing );
floatingActionButton.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick( View view ) {
int newPosition = ( databaseHandling.getTermDataCount() + 1 );
TermData termDataThing = new TermData();
termDataThing.setTermID( newPosition );
databaseHandling.addTermData( termDataThing );
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Using this way to re-launch the current activity seems visually better than
// "recreate()".
Intent intent = getIntent();
finish();
startActivity(intent);
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
}
});
}
@Override
public boolean onCreateOptionsMenu( Menu menu ) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate( R.menu.menu_main, menu );
return true;
}
//==============================================================================================
@Override
public boolean onOptionsItemSelected( MenuItem item ) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
if ( id == R.id.course_creation ) {
Intent intent = new Intent( MainActivity.this, CourseCreation_Activity.class );
startActivity( intent );
return true;
}
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
if ( id == R.id.mentor_creation ) {
Intent intent = new Intent( MainActivity.this, MentorCreation_Activity.class );
startActivity( intent );
return true;
}
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
if ( id == R.id.assessment_creation ) {
Intent intent = new Intent( MainActivity.this, AssessmentCreation_Activity.class );
startActivity( intent );
return true;
}
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
return super.onOptionsItemSelected( item );
}
//==============================================================================================
@Override
public void onItemClick( View view, int position ) {
//This prepares the "TermData" thing for entering info through "Term_ChangingDetails_Activity".
Intent intent = new Intent( MainActivity.this, Term_ChangingDetails_Activity.class );
test_TermData_Value = adapter.getItem( position );
intent.putExtra( "termData_Parcelable", test_TermData_Value );
//This ships the info to "Term_ChangingDetails_Activity".
startActivity( intent );
}
@Override
public void onResume() {
super.onResume();
termsArrayList = databaseHandling.getAllTerms();
//**************************************************************************
adapter = new Term_RecyclerView_Adapter( this, termsArrayList);
adapter.setClickListener( this );
recyclerView.setAdapter( adapter );
//**************************************************************************
adapter.notifyDataSetChanged();
}
}
Term_CourseSelection_Activity.java:
public class Term_ChangingDetails_Activity extends AppCompatActivity {
private Button saveButton;
int termID;
EditText termName_EditText;
Button startingDate_Button;
Button endingDate_Button;
Button courseSelection_Button;
Bundle dataArgument_Bundle_Example;
DialogFragment startingDateFragment;
DialogFragment endingDateFragment;
TermData termData_TransferInfo;
Database_Handling databaseHandling;
@Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
//The below line is necessary for showing info at all.
setContentView( R.layout.activity_term_changingdetails);
databaseHandling = new Database_Handling( this );
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
Intent intent = getIntent();
final TermData termData_Values = intent.getParcelableExtra( "termData_Parcelable" );
termID = termData_Values.getTermID();
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
//This displays a back button on the ToolBar at the top of the Activity.
getSupportActionBar().setDisplayHomeAsUpEnabled( true );
//This just changes the title of the ToolBar.
getSupportActionBar().setTitle( "Changing Term Info" );
//*****************************************************************
termName_EditText = findViewById( R.id.termNameXML );
termName_EditText.setText( termData_Values.getTermName() );
startingDate_Button = findViewById( R.id.startingDateButtonXML );
startingDate_Button.setText( termData_Values.getStartingDate() );
endingDate_Button = findViewById( R.id.endingDateButtonXML );
endingDate_Button.setText( termData_Values.getEndingDate() );
//*****************************************************************
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
saveButton = findViewById( R.id.saveButtonXML );
saveButton.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick( View view ) {
termData_TransferInfo = new TermData( termID,
termName_EditText.getText().toString(),
startingDate_Button.getText().toString(),
endingDate_Button.getText().toString() );
databaseHandling.updateTermData( termData_TransferInfo );
finish();
}
});
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//TODO START AN ACTIVITY FOR CHOOSING FROM A LIST OF COURSES.
courseSelection_Button = findViewById( R.id.courseSelection_ButtonXML );
courseSelection_Button.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick( View view ) {
Intent intent = new Intent( Term_ChangingDetails_Activity.this, Term_CourseSelection_Activity.class );
startActivity( intent );
}
});
}
public void reveal_StartingDate_DatePicker( View v ) {
startingDateFragment = new DatePickerFragment_TERM_StartingDate();
startingDateFragment.setArguments( dataArgument_Bundle_Example );
startingDateFragment.show( getFragmentManager(), "starting date picker" );
}
public void setStartingDate_in_StartingDate_Button( String startingDate_for_Button ) {
startingDate_Button.setText( startingDate_for_Button );
}
public void reveal_EndingDate_DatePicker( View view ) {
endingDateFragment = new DatePickerFragment_TERM_EndingDate();
endingDateFragment.setArguments( dataArgument_Bundle_Example );
endingDateFragment.show( getFragmentManager(), "ending date picker" );
}
public void setEndingDate_in_EndingDate_Button( String endingDate_for_Button ) {
endingDate_Button.setText( endingDate_for_Button );
}
}
activity_main.xml中
public class Term_CourseSelection_Activity extends AppCompatActivity {
private Button saveButton;
@Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_term_courseselection );
//This just changes the title of the ToolBar.
getSupportActionBar().setTitle( "Selecting Courses" );
//TODO THE SOFTWARE WORKS FINE WHEN THIS PART IS REMOVED. OTHERWISE, I GET A 'NullPointerException'.
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
//This displays the "back" button.
getSupportActionBar().setDisplayHomeAsUpEnabled( true );
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
saveButton = findViewById( R.id.saveButtonXML );
saveButton.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick( View view ) {
finish();
}
});
}
}
activity_term_changingdetails.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.weslange.Term_Scheduling.MainActivity"
android:background="?android:attr/selectableItemBackground"
android:focusable="true"
android:clickable="true"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView_Array_in_XML"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/selectableItemBackground"
android:focusable="true"
android:clickable="true"
/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/floatingActionButton_Thing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
app:srcCompat="@drawable/ic_add_term"
/>
</android.support.design.widget.CoordinatorLayout>
activity_term_courseselection.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbars="vertical"
android:showDividers="none"
tools:context="com.weslange.Term_Scheduling.Term_ChangingDetails_Activity"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="\nPlease Adjust as Necessary"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold"
/>
<EditText
android:id="@+id/termNameXML"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName|textCapWords"
/>
<Button
android:id="@+id/startingDateButtonXML"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:onClick="reveal_StartingDate_DatePicker"
/>
<Button
android:id="@+id/endingDateButtonXML"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:onClick="reveal_EndingDate_DatePicker"
/>
<Button
android:id="@+id/saveButtonXML"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save Changes and Return to the Main List"
/>
<Button
android:id="@+id/courseSelection_ButtonXML"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choose Courses for This Term"
/>
</LinearLayout>
再次感谢。
答案 0 :(得分:0)
Mike M.在问题的评论中提供了答案,所以他应该得到信任。如果您觉得这很有用,请向他投票。
缺少“finish()
”导致“ Term_ChangingDetails_Activity.java ”崩溃。应该看起来像这样:
courseSelection_Button = findViewById( R.id.courseSelection_ButtonXML );
courseSelection_Button.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick( View view ) {
Intent intent = new Intent( Term_ChangingDetails_Activity.this, Term_CourseSelection_Activity.class );
startActivity( intent );
finish();
}
});
虽然停止了崩溃,但我不确定为什么“后退”按钮失败了。将“getSupportActionBar().setDisplayHomeAsUpEnabled( true )
”添加到“ Term_CourseSelection_Activity.java ”的“onCreate()
”方法中,可以通过点击“后退”按钮来实现崩溃。
由于我的记忆,我在最后一部分道歉。我不小心找到了“返回”按钮直接返回MainActivity的方法,即使Activity的父级不存在为MainActivity也是如此。我不记得细节了。这样做也避免了NullPointerException。