- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我收到以下错误:
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/
我正在将我的模板代码移植到 XTend。在某些时候,我在测试用例中有这种类型的条件处理: @Test def xtendIfTest() { val obj = new FD if (
我是新来的 kotlin , 当我开始 Null Safety 时,我对下面的情况感到困惑. There's some data inconsistency with regard to initia
我的应用程序一直在各种Android版本中保持良好状态。我有用户在Android 4.3、5.0、5.1和6.0上正常运行。但是,具有S7 Edge的用户刚刚更新了Android 7.0,将文本粘贴到
我使用的是最新版本的 LWUIT (1.5)。我在资源编辑器中设计了我的表单,然后将代码生成到 netbeans。问题是如果我想访问除表单之外的任何对象,我会收到此错误: java.lang.Null
更新: 我在 Fedora 21 上运行它。 SonarQube - 5.0。 SonarQube Runner - 2.4 更新 2:Findbugs v3.1,Java 插件 v2.8 更新3:
RecupData 我的类仅在 web 中返回 NullPointerException。我连接到 pgsql db 8.3.7 - 该脚本在“控制台”syso 中运行良好 - 但引发了测试 Web
我在 mac 上使用 Processing 2.08。我正在尝试使用文档中给出的 createShape 函数创建 PShape。 PShape s; void setup(){ size(500
我在 mac 上使用 Processing 2.08。我正在尝试使用文档中给出的 createShape 函数创建 PShape。 PShape s; void setup(){ size(500
每次运行此 jsp 时,都会收到以下错误异常: org.apache.jasper.JasperException: java.lang.NullPointerException root cause
Kotlin 在编译时有一个出色的 null 检查,使用分离到“可空?”和“不可为空”的对象。它有一个 KAnnotator 来帮助确定来自 Java 的对象是否可以为空。但是,如果 not-null
我有一个布局将显示一个TextView,用于显示一个滴答时间。我遵循了此链接中的代码 How to Display current time that changes dynamically for
Elasticsearch 1.4.1版(“lucene_version”:“4.10.2”) 我有一个像这样的文件: $ curl 'http://localhost:9200/blog/artic
这是我从另一个类调用函数的方法Selenium 设置已定义。 public void Transfer() throws Exception { System.out.println("\nTrans
我试图在主类中使用我在此类中创建的函数,但它崩溃并显示“警告:无法在根 0 处打开/创建首选项根节点 Software\JavaSoft\Prefsx80000002。 Windows RegCrea
这个问题已经有答案了: What is a NullPointerException, and how do I fix it? (12 个回答) 已关闭 3 年前。 我有一个 Java 代码,它将
我声明了两张牌: Card card1 = new Card('3', Card.Suit.clubs); Card card2 = new Card('T', Card.Suit.diamonds)
我编写了一段代码来解码 Base64 图像并在 javafx 中表示该图像。在我的 url base64 代码中不断变化。这就是我在 javafx 代码中使用任务的原因。但我收到错误:java.lan
我正在尝试使用 arrayList 的 arrayList 在 Java 中实现图形。 每当调用 addEdge 函数时,我都会收到 NullPointerException 。我似乎无法弄清楚为什么
我是 Java/android 的新手,所以很多这些术语都是外国的,但我愿意学习。我不打算详细介绍该应用程序,因为我认为它不相关。我目前的问题是,我使用了博客中的教程和代码 fragment ,并使我
我正在开发一个 Android 应用程序来在 Android developer guide 的帮助下录制视频.我程序上的所有代码都与此页面相同。 我在 之外定义了权限标签。 当应
我是一名优秀的程序员,十分优秀!