gpt4 book ai didi

java - NullPointerException 为 "getSupportActionBar().setDisplayHomeAsUpEnabled( true )"?

转载 作者:太空宇宙 更新时间:2023-11-04 10:40:44 26 4
gpt4 key购买 nike

我收到以下错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.weslange.Term_Scheduling/com.weslange.Term_Scheduling.Term_ChangingDetails_Activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.weslange.Term_Scheduling.TermData.getTermID()' on a null object reference

以下似乎导致了该问题:getSupportActionBar().setDisplayHomeAsUpEnabled( true )

我知道“getSupportActionBar()”确实有效,因为以下方法工作得很好:getSupportActionBar().setTitle( "Selecting Courses")

MainActivity 是根父级。 “Term_ChangingDetails_Activity.java”是该 Activity 的子项。 “Term_CourseSelection_Activity.java”是“Term_ChangingDetails_Activity.java”的子级。前两个可以与“setDisplayHomeAsUpEnabled( true )”配合使用。

我认为 NullPointerException 引用了我的“Term_ChangingDetails_Activity.java”文件中的 Parcelable。 “Term_CourseSelection_Activity.java”包含异常源,当该文件中存在“setDisplayHomeAsUpEnabled( true )”时会发生异常。

我认为“AndroidManifest.xml”是正确的,因为如果我在“Term_ChangingDetails_Activity.java”中调用“finish()”,一切都会正常工作。

感谢您的宝贵时间。

编辑 01:(为了清晰起见,添加 MainActivity.java。)

MainActivity.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_ChangingDetails_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 );

}
}

Term_CourseSelection_Activity.java:

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_main.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_changedetails.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>

activity_term_courseselection.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context="com.weslange.Term_Scheduling.Term_CourseSelection_Activity"
>


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="\nChoose the Courses for This Term"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold"
/>


<Button
android:id="@+id/saveButtonXML"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save Changes and Return to the Term Details"
/>


</LinearLayout>

再次感谢。

最佳答案

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。

关于java - NullPointerException 为 "getSupportActionBar().setDisplayHomeAsUpEnabled( true )"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48999386/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com