I have an Activity the implements the following functions:
public class SettingsActivity extends Activity {
public void setText(EditText txtBox,String strText){
txtBox.setText(strText);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
EditText txtEdit = (EditText)findViewById(R.id.txtEdit);
setText(txtEdit,"String");
...
}
So I am trying to pass an EditText Object to the setText Function and then calling the txtBox.setText() function. But this throws the following exception:
android.content.res.Resources$NotFoundException: String resource ID #0x0
However, when calling the txtEdit.setText() function within onCreate() works perfectly fine.
Edit: The function setText was a simplification, I was actually passing an object a of custom class a then called txtBox.setText(object.value), which ( by mistake) was actually an Integer, not a String. Passing a String fixed the error. I am sorry for the inconvenience.
I just realized that, in fact I did not pass a String Parameter. Like #Lubos Horacek described the exception is thrown when you are passing an int to setText. Converting the parameter fixed the error.
May be your id doesn't exist in layout file
Related
I have one activity and I am testing one method of it using Roboelectic Framework but I am getting null pointer exception.
following is test code
#Test
public void processTest() throws Exception {
MyActivity activity = Robolectric.setupActivity(MyActivity.class);
activity.setValue();
tView = (TextView) activity.findViewById(R.id.text);
assertEquals(“1”,tView.getText().toString());
}
following is setValue method of activity
public class MyActivity extends AppCompatActivity {
TextView tView;
#Inject Student s;
#Override
public void onCreate() {
tView = (TextView) activity.findViewById(R.id.text);
}
public void setValue() {
String id = s.getId();
tView.setText(S);
}
I am getting null pointer error on String id = s.getId(); line when I run test
I know s is null. but my question how to make it available in Test using Roboelectric without using Mokito ?
Are you sure you pasted your code exactly as it run? onCreate should have parameter of Bundle, and the activity has not called setConentView before you do findViewById, and some other problems....
In general, The activity constructed by Robolectric.setupActivity should be ready for use in the rest of the test case, and onCreate(Bundle) should have been executed before return.
I'm trying to get the the text written by the user in an edittext view and store it in a String value then set this String value in a class that I created that contains two String Objects but when I run the code it gives me an error with a null pointer exception because the "WrittenSubj" and "WrittenDeta" are equal to null, how can i solve this problem?
public class TaskDetails extends AppCompatActivity {
EditText WrittenSubj;
EditText WrittenDeta;
Button SaveBut;
TheTask theTask;
private Database database;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_task_details);
WrittenSubj=(EditText)findViewById(R.id.thesubject);
WrittenDeta=(EditText)findViewById(R.id.theDetails);
SaveBut=(Button)findViewById(R.id.SaveButton);
String subj=WrittenSubj.getText().toString();
String deta=WrittenDeta.getText().toString();
theTask.setSubject(subj);
theTask.setDetails(deta);
}
I think TheTask is your Beam class, And you have not initialize it.
Initialize this.
theTask = new TheTask();
And use
theTask.setSubject(subj);
theTask.setDetails(deta);
You have to initialize the TheTask class before using that like below
TheTask theTask = new TheTask();
And after that use object of theTask like below
theTask.setSubject(subj);
theTask.setDetails(deta);
Problem is arrising because you are initializing edittexts and getting its text getText() in method onCreate() and OnCreate() is the method called when activity is created So at the time of creation of activity your edit texts will obviously be having null/empty value.
So you must call edittext.getText() on any event click or button click after actually the text have entered.
public class Excuses extends ActionBarActivity {
// openings
private String[] openings = {
getString(R.string.opening_1),
getString(R.string.opening_2),
getString(R.string.opening_3),
getString(R.string.opening_4),
getString(R.string.opening_5),
getString(R.string.opening_6),
getString(R.string.opening_7),
getString(R.string.opening_8),
getString(R.string.opening_9),
getString(R.string.opening_10),
getString(R.string.opening_11),
getString(R.string.opening_12),
getString(R.string.opening_13),
getString(R.string.opening_14),
getString(R.string.opening_15),
getString(R.string.opening_16),
getString(R.string.opening_17)
};
When ran, this error pops up:
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:79)
at android.content.Context.getString(Context.java:352)
at com.aczylsapp.com.excusegenerator.Excuses.<init>(Excuses.java:12)
This line gives me the NullPointerException:
private String[] openings = {
and, I have no idea why :/
I have already looked at other posts, but they do not help me.
If someone could help me out, I would be greatful.
The problem is that the Activity was not fully constructed, based on Activity Lifecycle and you cannot access the Resources properly.
You must move this assignment to inside onCreate method. Like:
public class Excuses extends ActionBarActivity {
// openings, will be initialize in onCreate method
private String[] openings;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initializeOpenings();
// Your initialization code
}
public void initializeOpenings() {
openings = new String[] {
getString(R.string.opening_1),
getString(R.string.opening_2),
getString(R.string.opening_3),
getString(R.string.opening_4),
getString(R.string.opening_5),
getString(R.string.opening_6),
getString(R.string.opening_7),
getString(R.string.opening_8),
getString(R.string.opening_9),
getString(R.string.opening_10),
getString(R.string.opening_11),
getString(R.string.opening_12),
getString(R.string.opening_13),
getString(R.string.opening_14),
getString(R.string.opening_15),
getString(R.string.opening_16),
getString(R.string.opening_17)
};
}
}
But there is a better way to do this!
Create an String Array in your resource, like:
<string-array name="openings">
<item>#string/opening_1</item>
<!-- Declare all your items here -->
<item>#string/opening_17</item>
</string-array>
And access then with getStringArray(int resId):
public class Excuses extends ActionBarActivity {
// openings, will be initialize in onCreate method
private String[] openings;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initializeOpenings();
// Your initialization code
}
public void initializeOpenings() {
openings = getResources().getStringArray(R.array.openings);
}
}
Null Pointer exception means that the objects you are trying to put into your array are empty or dont exist.
In your case, this likely means that getString(R.string.opening_1) is returning null.
I suspect that the issue lies with the parameter you are putting in your getString() method. If I were you, I'd take a good look at your getString() and check that getString(R.string.opening_1) is returning the String you want it to.
Based on the Android Documentation for getString(), you need to make sure that the "Resource id for the string" can be found.
I've successfully passed a value from a static class to a non-static one, but I got an error, null value, when I put that value to an EditText.
public class HelloBubblesActivity extends SherlockFragmentActivity {
public EditText editText1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_discuss);
editText1 = (EditText) findViewById(R.id.editText1);
}
public static class MyDialogFragment extends SherlockDialogFragment {
//i fill variable emotx is "test string"
public void emot(String emotx){
HelloBubblesActivity hb=new HelloBubblesActivity();
hb.smiley(emotx); //send value to smiley method..
}
}
public void smiley(String name){
Log.d("test", name); //result value is "test string" (success)
editText1.setText(name); //here is error
}
}
I'm not sure why I'm getting this issue. Can anyone see why this is not doing what is expected?
HelloBubblesActivity hb=new HelloBubblesActivity();
hb.smiley(emotx); //send value to smiley method..
Creates a new HelloBubblesActivity object but doesn't call onCreate and hence does not assign anything to editText1. Fields that have never been assigned are null in Java.
(Of course, to say the code is not doing what is "expected" would be wrong -- it's doing exactly what one would expect from the scenario.)
(Your mistake is most likely in thinking that setting a value in ANY HelloBubblesActivity object makes the value appear in ALL HelloBubblesActivity objects. Individual objects do not share instance fields, and what you set in one object does not magically appear in another. You can't just create an object of some class and expect it to have some sort of paranormal communications with others of it's class.)
(But this is a common mistake made by those who are dumped into OOP without a good background in assembler programming, et al.)
Groping in the dark... This time I am receiving the following error in Eclipse:
The method call(Activity) in the type
IntentsUtils is not applicable for the
arguments (new
View.OnClickListener(){})
This error refers to the call() line in a callback hooked up to a button, in a class that extends Activity:
public class UnderstandingIntents extends Activity {
...
...
...
// A call-back for when the user presses the testintents button.
OnClickListener mTestIntentsListener = new OnClickListener() {
public void onClick(View v) {
IntentsUtils.call(this);
}
};
}
IntentsUtils is a class copied verbatim from listing 3-33 here.
What does this error mean?
The problem here is that you are trying to reference the Activity Class (UnderstandingIntents) in an Anonymous inner class, hence when you say "this" it refers to View.OnClickListener(){}
to correct this do the following code:
IntentsUtils.call(UnderstandingIntents.this);
This way, your Activity class gets referenced.
The this parameter passed into IntentsUtils.call() refers to the object within which it is being used, in this case an instance of OnClickListener. Try replacing the this parameter with UnderstandingIntents.this:
IntentsUtils.call(UnderstandingIntents.this);
try this
IntentsUtils.call(UnderstandingIntents.this);