How can dynamically setContentView Android - android

I've to dynamically set an xml into a ContentView?
This is my code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
if (extras != null) {
value = extras.getString("ID_position");
Log.e("TESTVALUE", value);
}
if (value.equals("0")){
setContentView(R.layout.list_mobile);
}
if (value.equals("1")){
setContentView(R.layout.custom_dialog1);
}
}
It crash at activity open... pls help me ^^
PS: the value is correct ;)
EDIT: i've done a big mistake in extend class... damned copy and paste... i'm sorry again ;) now it work well

Looking at your code, the possible reason is:
Value at the Bundle with id = ID_position has type other than String => value will be null and you'll get NPE at value.equals(). Are you sure you didn't put int instead?

Related

Crashes in Android 6.0.1 after restart because of Bundle extras = getIntent().getExtras()

I am trying retrieve variables with this code
Here's my MainActivity
public class MainActivity extends AppCompatActivity {
String Variable1 = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle extras = getIntent().getExtras();
if (extras != null) {
Variable1 = extras.getString("Variable1");
EditText1_ET.setText(Variable1);
}
}
}
I am creating Variable1 in another Activity
Intent i = new Intent(getApplicationContext(), MainActivity.class);
i.putExtra("Variable1", textview.getText().toString());
startActivity(i);
Its working great until I reboot OS (Android 6.0.1) and start my app, I get error that my app stopped working.
I tried it on Android 5.1 and its working without error.
Can anyone help me please?
You should do this, and check for nullability :
if (extras != null) {
String variable1 = extras.getString("Variable1");
if (variable1 != null) {
EditText EditText1_ET = (EditText) findViewById(R.id.xxxx);
if (EditText1_ET != null)
EditText1_ET.setText(variable1);
}
}
Also init your EdiText variable before setting text !
Well it is better to change the following:
SearchQueryTerm = extras.getString("Variable1");
EditText1_ET.setText(Variable1);
to:
Variable1 = extras.getString("Variable1");
if(EditText1_ET != null) {EditText1_ET.setText(Variable1);}
in your code you just assign null to your EditText every time.
Looks like you've solved your problem. Yet, I though I need to put my suggestion here.
From the Activity2 where you're passing the parameter value, do you really need to get the text from a TextView? Because, you already have set some String value in your TextView somewhere. Just use that String variable to pass the value to another Activity.
Intent i = new Intent(getApplicationContext(), MainActivity.class);
i.putExtra("Variable1", myString);
startActivity(i);
Now in the MainActivity, check if the Variable1 is null and initialize your EditText first.
public class MainActivity extends AppCompatActivity {
String Variable1 = null;
private EditText EditText1_ET; // Declare an EditText first
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize your EditText here
EditText1_ET = (EditText) findViewById(R.id.your_edit_text_id);
Bundle extras = getIntent().getExtras();
if (extras != null) {
Variable1 = extras.getString("Variable1");
// Here check if Variable1 is null
if(Variable1 != null) EditText1_ET.setText(Variable1);
}
}
}
Had the same problem, it was still crashing even after checking for null.
I used a try-catch block and that solved the problem for me.

Android .putExtra (not responding)

I need to pass a String created from 'Activity A' to 'Activity B' so that I can display it in a TextView.
The problem is that the code causes Android to not respond, its identical to the other tutorials online.
Thanks for any feedback.
Activity A.onCreate()
check_button = (Button) findViewById(R.id.check_button);
check_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Intent i = new Intent(AddActivity.this, DetailActivity.class);
String hash = text_hash.toString();
i.putExtra("hash" , hash);
startActivity(i);
}
});
Activity B.onCreate()
Bundle extras = getIntent().getExtras();
if (extras != null)
{
passedHash = (String) getIntent().getExtras().getString("hash");
hash.setText(passedHash);
}
stack trace:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
According to your log, it seems that you didn't initialize your TextView in Activity B. Do this before setting the text to your TextView:
TextView hash = (TextView)findViewById(R.id.hash_textview);
Ok, so I needed to use X.getText().toString();
By reading the stack trace I figured I also needed to findViewByID on the TextView BEFORE trying to set text.
Thanks for reading.
If text_hash is TextView or EditText then use use
String hash = text_hash.getText().toString();
In ActivityB onCreate() use this:
String newString;
if (savedInstanceState != null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
newString= null;
} else {
newString= extras.getString("hash");
}
}
EDIT
From your error log, You didn't initialize TextView in your ActivityB. First you have to initialize that TextView then set Text.
your textView object is nullreferenced, you need to initialize the hash variable in the oncreate before you begin to call any method of that TextView...
Example:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView hash = (TextView)findViewById(R.id.hash_textview);
after that you can call the settext method
hash.setText(passedHash);

NullPointerException when i tried to get Extras in FragmentActivity

i have a problem to pass extras between an Activity and a FragmentActivity.
This next code create the Intent to start the new Activity:
Intent fichaSerie = new Intent(getActivity(),ActividadSerie.class);
NotificacionSerie serie = (NotificacionSerie) adaptador.getItem(position);
fichaSerie.putExtra("serie", serie.getID());
getActivity().startActivity(fichaSerie);
When I tried to get the Extras in the Fragment Activity, i get a NullPointerException:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.serie);
String extra = getIntent().getExtras().getString("serie") //NullPointerException
}
Anyone know what mistake(s) i'm made in my code?
Thank you very much to all.
Thanks all your help guys, i solved my problem using this call:
getIntent().getStringExtra("serie")
I mark this question as solved.
Thank you again guys.
First of all Java is case sensitive so .getID() is not the same as .getId().
If you are trying to get an ID of an objet, use .getId(). That usually returns an Integer value
Pay attention on the type you are putting to extra as Michal said (String or Integer or anything else)
Intent fichaSerie = new Intent(getApplicationContext(), ActividadSerie.class);
NotificacionSerie serie = (NotificacionSerie) adaptador.getItem(position);
fichaSerie.putExtra("serie", serie.getId()); //presuming this id is an Integer or long int ... You could change this to string if it is still usable then (serie.getId().toString())... but you have to get an intent with myIntent.getStringExtra(...), but I think you will not be doing this.
startActivityForResult(fichaSerie, 0);
Getting an extra from ActividadSerie.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.serie);
Intent myIntent = getIntent();
String extra = myIntent.getStringExtra("serie"); //try diferent gets... getIntExtra(), getStringExtra()..
}
You are put an Integer value and get it into the String value so that it gets a NullPointerException.

Retrieving the intent values from another class

My question is a bit basic. I've been learning to code on JAVA and android. Here I am a bit confused on how to call the values that I have sent via an intent.
In my first activity this is the intent that I am using.
Intent intent = new Intent(MainActivity.this, Secondactivity.class);
String regName1 = regName;
intent.putExtra(regName1, regNameSplit[0]);
startActivity(intent);
Here regName1 will contain three values. SessionID,URL,Name split by "-".
In my SecondActivity
public class Secondactivity extends Activity {
public final String TAG = "###---Secondactivity---###";
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.secondactivity);
Log.i(TAG,"before if statement");
if (getIntent().getStringExtra("regName1") != null){
getIntent().getStringExtra("regName1").split("-");
String[] str = "regName";
Log.i(TAG, ""+str[0]+str[1]+str[2])
}
}
}
The value if regName1 always comes as null.
This line
intent.putExtra(regName1, regNameSplit[0]);
Needs to be like this instead
intent.putExtra("regName1", regNameSplit[0]); // note the quotes
BUT you are using regName1 as a variable... how do you expect the second class to know that variable?
Use a string resource instead.
and you are sure that the content of the variable regName is actually "regName"?
cause you set the value using
intent.putExtra(regName, ... )
and you get the value using
intent.getStringExtra("regName")
use firstactivity
dont use
String regname1=regname;
just:
intent.putExtra("regName1", regNameSplit[0]);
in second Activity
if (getIntent().getStringExtra("regName1") != null){
//
}

Using Bundle to move a value from activity to activity but can't get them from on create method?

My java skills are not strong. Only been programming in it for a month or 2 so forgive my stupidness.
I'm trying to pass values between methods in a bundle to allow me to save and load some game settings but although I think my values are transferring, I can't get a value out of the 'on create method' to use in the rest of my programme.
I'm loading and bundling up my boolean value here (I've snipped out lots or hopefully irrelevant stuff) :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
vV = new VortexTouch(this);
CONTEXT = this;
// LOAD DATA
SharedPreferences settings = getSharedPreferences("GAME_DATA",MODE_PRIVATE);
_dPad = settings.getBoolean("GamePad", true);
// PASS DATA
intent = new Intent();
intent.setClass(this,VortexRenderer.class);
intent.putExtra("my_data", true); // should be _dPad but put 'true' in there for now.
startActivity(intent);
// PASS DATA END
setContentView(vV);
}
The boolean value is then received in my VortexRenderer class:
public class VortexRenderer extends Activity implements GLSurfaceView.Renderer {
private static final String LOG_TAG = VortexRenderer.class.getSimpleName();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getIntent().getExtras();
_dPad = bundle.getBoolean("my_data");
_dPad = true; // testing to see if value carries, it doesn't :-(
finish();
}
public boolean _dPad;
public void SomeGameAction(){
//try to do something with _dPad but it has not taken on a value of true. why?
}
so i think the value of _dPad is getting from one activity to the other but it's not getting out of the VortexRenderer 'onCrate' method.. Clearly I'm not understanding something.. can anyone help? Thanks.
My game was built around this excellent tutorial if that helps (not that there's much left of the original now):
http://www.droidnova.com/android-3d-game-tutorial-part-i,312.html
Less helpful but if you're interested this is what I'm trying to add the code to:
https://market.android.com/details?id=com.clockworkrobot.spacewarz
In the first activity, instead of
intent.putExtra("my_data", true);
use
Bundle bundle = new Bundle();
bundle.putBoolean("my_data", true);
intent.putExtra("android.intent.extra.INTENT", bundle);
Then in the second activity, instead of
Bundle bundle = getIntent().getExtras();
use
Bundle bundle = getIntent().getBundleExtra("android.intent.extra.INTENT");
I have to be honest... I'm not sure what you're truly trying to do here.
Calling finish in your onCreate will end your activity as soon as you start it.
You do appear to be passing/receiving the boolean with the intent properly, but then hardcoding _dPad to true I hope is just for debug because it certainly makes it unnecessary to pass it with the intent.
What is the overall purpose of your VortexRenderer activity? I imagine there will be a better way to accomplish your goal without creating a new activity.
I also recommend using the Log.v(tag, message); utility and logcat to help yourself debug challenges.
Code indentation will also most certainly help code readability.
The best way that works for me every time is that you use Global Static class to hold your temporary data. Use setters and getters it will be much more easy and understandable.

Categories

Resources