I have 2 Activities which are ( MainActivity
, LoadIntentActivity
). I know how to putExtra()
from MainActivity
and receive the value in LoadIntentActivity
with getIntent().getExtra()
. However, I want to start LoadIntentActivity
with 2 method: 1 is with the putExtra()
and the other is without putExtra()
. But the LoadIntentActivity
already have a getIntent().getExtra()
that will result NullPointerException
if i trying to start LoadIntentActivity
without putExtra()
.
This is my code:
activity_main.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:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btnGotoIA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Go to IntentActivity"/>
<ListView
android:id="@+id/lvList"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
**MainActivity.java**
:
public class MainActivity extends AppCompatActivity {
Button btnGotoIA;
ListView lvList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnGotoIA = findViewById(R.id.btnGotoIA);
lvList = findViewById(R.id.lvList);
String[] items = {"1", "4", "53", "3", "99"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
lvList.setAdapter(adapter);
btnGotoIA.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, LoadIntentActivity.class);
startActivity(intent);
}
});
lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(MainActivity.this, LoadIntentActivity.class);
intent.putExtra("text", adapter.getItem(i));
startActivity(intent);
}
});
}
}
**activity_load_intent.xml**
:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".LoadIntentActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
LoadIntentActivity.java
:
public class LoadIntentActivity extends AppCompatActivity {
TextView textView;
Bundle bundle = getIntent().getExtras();
String text = bundle.getString("text");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_load_intent);
textView = findViewById(R.id.textView);
// if i start this Activity without putExtra the TextView should be empty
textView.setText(text);
}
}
If you want to handle the case of a missing bundle, just check if it (and the string extracted from it) is null before using it.
public class LoadIntentActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_load_intent);
textView = findViewById(R.id.textView);
Bundle bundle = getIntent().getExtras();
if( bundle != null ) {
String bText = bundle.getString("text");
if( bText != null ) {
textView.setText(bText);
}
}
}
}
Alternately, if you need to save the text as a class member still you could do something like this:
public class LoadIntentActivity extends AppCompatActivity {
private TextView textView;
private String text = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_load_intent);
textView = findViewById(R.id.textView);
Bundle bundle = getIntent().getExtras();
if( bundle != null ) {
String bText = bundle.getString("text");
if( bText != null ) {
text = bText;
}
}
textView.setText(text);
}
}
You could also save a similar class member boolean to indicate whether the activity had been started with a bundle or not (in case it affects the actions taken later in the activity).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.