Reputation: 44325
Within my MainActivity.java
I am trying to open a file according to the code given here. But when running the code
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button) findViewById(R.id.btn1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getBaseContext(), "YOUR TEXT HERE", Toast.LENGTH_LONG).show();
// Perform action on click
FileInputStream fis = getBaseContext().openFileInput("hello.txt", Context.MODE_PRIVATE);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader bufferedReader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
}
});
}
}
I get the following error:
Error:(27, 55) error: method openFileInput in class Context cannot be applied to given types;
required: String
found: String,int
reason: actual and formal argument lists differ in length
I am not even able to understand what the error means. What 'argument list' is the message referring to?
Upvotes: 1
Views: 2146
Reputation: 668
As @LunarWatcher said the openFileInput takes 1 parameter.So,
Instead of this
FileInputStream fis = getBaseContext().openFileInput("hello.txt",Context.MODE_PRIVATE);
use this
FileInputStream fis = openFileInput("hello.txt");
Upvotes: 0
Reputation: 28238
As mentioned by @MuhammadKashifArif:
Instead of this
FileInputStream fis = getBaseContext().openFileInput("hello.txt",Context.MODE_PRIVATE);
use this
FileInputStream fis = openFileInput("hello.txt", MODE_PRIVATE);
It doesn't matter.
Context is context, and getBase context is still the same context as everything else, calling getBaseContext doesn't change the type of context compared to using this
in an activity. There is no difference between accessing MODE_PRIVATE
statically and using import static
I am not sure when exactly this changed (and I am not going to look through every version of android to check either), but it is openFileOutput
that takes two arguments, a String an an Integer.
openFileInput
now only takes one argument. So:
FileInputStream fis = /*Some context here if you are outside an activity.*/openFileInput("hello.txt");
And to load:
FileOutputStream fos = /*some context if outside activity.*/openFileOutput("hello.txt", Context.MODE_PRIVATE);
And I would once again like to clear up the fact that it doesn't make a difference whether or not you call getBaseContext, getApplicationContext, or call this
/SomeActivity.this
inside an activity. The context class itself doesn't change, the fields are the same. And writing MODE_PRIVATE
without Context.
in front of it is called a static import. The only difference between Context.MODE_PRIVATE
and the static import MODE_PRIVATE
is only you don't have to type those characters. The method doesn't exist no matter how you call it
As for the compile error, it basically says you are supplying more arguments than the method requires. You should also see a red warning over the call to the method as well, but what you see is the compile error as you tried to compile the build while there is a call to an unknown method.
Upvotes: 1