NullPointerException Error in getFilesDir() Method - android

I try to save some data in internal storage on android but I keep getting a NullPointerException I think it has to do with the getFilesDir() I'm using but I'm not sure. Can Some Please help clarify if that is that case and help me write this file to the device. Here the Error message im am getting
01-20 22:11:59.020: E/AndroidRuntime(329): FATAL EXCEPTION: main
01-20 22:11:59.020: E/AndroidRuntime(329): java.lang.NullPointerException
01-20 22:11:59.020: E/AndroidRuntime(329): at android.content.ContextWrapper.getFilesDir(ContextWrapper.java:178)
01-20 22:11:59.020: E/AndroidRuntime(329): at yantz.imageapp4.main.writeElement(main.java:89)
01-20 22:11:59.020: E/AndroidRuntime(329): at yantz.imageapp4.Panel.onTouchEvent(Panel.java:102)
Here is the oncreate of the main class
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout sv = new FrameLayout(this);
LinearLayout ll = new LinearLayout(this);
test = new Panel(this);
test.settest(1) ;
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(test);
sv.addView(ll);
setContentView(sv);}
Here my OnTouch method in the panel class
public boolean onTouchEvent(MotionEvent event) {
mainactivity=new main();
mainactivity.writeElement(new Element(getResources(),(int) event.getX(),(int) event.getY()));
Log.v("Gesture", "is 1 ");
return super.onTouchEvent(event);
}
Here is my writeObject method inside my main class
public void writeElement(Element obj){
Log.v("main", "made it to method writeElement" );
File f = new File(getFilesDir()+FILENAME);
try {
fos = new FileOutputStream(f);
ObjectOutputStream objectwrite = new ObjectOutputStream(fos);
objectwrite.writeObject(obj);
fos.close();
Log.v("main", "file was made File ");
}catch (FileNotFoundException e){
e.printStackTrace();
Log.v("main", "file was not made File not found ");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.v("main", "file was not made File IOException ");
}
}
manifest
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name="yantz.imageapp4.main" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".charactors" android:label="#string/app_name"
android:theme="#android:style/Theme.Black">
<intent-filter>
<action android:name="yantz.imageapp4.charactors" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".main2" android:label="#string/app_name"
android:theme="#android:style/Theme.Black">
<intent-filter>
<action android:name="yantz.imageapp4.main2" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>

I know what's wrong. You can't just write
mainactivity=new main();
getFilesDir should get the correct context instance but it doesn't now.
Try something like that:
public boolean onTouchEvent(MotionEvent event) {
main.writeElement(new Element(...), this.getContext);
return super.onTouchEvent(event);
}
public static void writeElement(Element obj, Context context){
...
File f = new File(context.getFilesDir(), FILENAME);
...
}

Please try,
Open your AndroidManifest.xml and write following lines after the closing of tag.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>

Related

FileNotFoundException - Android - Creating and saving a pdf file to external storage

I have a problem in my android app while trying to create a pdf file from view of app.
W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Documents/MyList/MyList.pdf: open failed: ENOENT (No such file or directory)
I had an error such like above. What am I missing there?
How can I handle it? Can you help me? Thanks.
Here is the Manifest file of my project.
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.furkan.tercihrehberi">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme.NoActionBar">
<!--activities...-->
<activity
android:name=".ListeMyo"
android:label="#string/app_name">
</activity>
<activity
android:name=".PDFGoster"
android:label="#string/app_name">
<intent-filter >
<action android:name="android.intent.action.OPEN_DOCUMENT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".PDFGosterTeog"
android:label="#string/app_name">
<intent-filter >
<action android:name="android.intent.action.OPEN_DOCUMENT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
ImageButton save = (ImageButton) findViewById(R.id.save);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String state = Environment.getExternalStorageState();
if (!Environment.MEDIA_MOUNTED.equals(state)) {
Toast.makeText(PDFGoster.this,"You cannot do that.",Toast.LENGTH_LONG).show();
}
File pdfDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS),
"MyList");
if (!pdfDir.exists()){
pdfDir.mkdir();
}
LayoutInflater inflater = (LayoutInflater) PDFGoster.this.getSystemService(LAYOUT_INFLATER_SERVICE);
RelativeLayout root = (RelativeLayout) inflater.inflate(R.layout.pdf, null);
root.setDrawingCacheEnabled(true);
Bitmap screen= getBitmapFromView(PDFGoster.this.getWindow().findViewById(R.id.tabla_cuerpo));
File pdfFile = new File(pdfDir, "MyList.pdf");
try {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(pdfFile));
document.open();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
screen.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
addImage(document,byteArray);
document.close();
}
catch (Exception e){
e.printStackTrace();
}
Intent intent = new Intent("android.intent.action.OPEN_DOCUMENT");
Uri uri = Uri.fromFile(new File(pdfDir, "MyList"));
intent.setDataAndType(uri, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Toast.makeText(PDFGoster.this, "List successfully created", Toast.LENGTH_LONG).show();
startActivity(intent);
}
});

How to write text file via emulator?

Yesterday i post this question and some one replied me by peace of codes but the problem is permission denied. i do not know why happen this error because i have allowed permission in my manifest.xml. below is peace of code.
This is java code
public void WriteText() {
EditText txt= (EditText) findViewById(R.id.txtwrite);
try {
BufferedWriter fos = new BufferedWriter(new FileWriter(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+"File.txt"));
fos.write(txt.getText().toString().trim());
fos.close();
Toast.makeText(this, "Saved", Toast.LENGTH_LONG);
} catch (Exception e) {
Toast.makeText(this,e.getMessage(),Toast.LENGTH_LONG);
}
}
This is manifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Please any one help me i was so Straggled about it.

My android app can download but doesnt appear in my downloaded apps

My new android app can download from the playstore but doesn't appear in my downloaded apps. When i download it from the play store usually there is an option to 'open' the app aswell as 'uninstall', only the uninstall button is visible. (http://i.imgur.com/jNTvJq2.png notice the missing open button)
here's the my manifest, there were no errors during all the tests i ran.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jackattackapps.bigl"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#drawable/iconimage"
android:label="#string/app_name"
android:theme="#style/Theme.Sherlock" >
<activity
android:name="com.jackattackapps.bigl.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.jackattackapps.bigl.Splashscreen"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.SPLASHSCREEN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
</application>
</manifest>
And this is the activity that is supposed to load on the start of the application.
package com.jackattackapps.bigl;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
public class Splashscreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
Thread timer = new Thread(){
public void run(){
try{
MediaPlayer ourSong = MediaPlayer.create(Splashscreen.this, R.raw.splashsound);
ourSong.start();
sleep(2300);
}
catch (InterruptedException e){
e.printStackTrace();
} finally {
Intent openMainActivity = new Intent("android.intent.action.MAINACTIVITY");
startActivity(openMainActivity);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
if more information is needed just comment, help would be appreciated greatly.
Your Launcher activity should have this Intent Filter
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>

Android: No Activity found to handle Intent error?

Im getting the error in the title when just messing around with android programming:
Code :
package co.uk.mypchealth.whatami;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread logoTimer = new Thread(){
public void run(){
try {
sleep(3000);
Intent menuIntent = new Intent("co.uk.mypchealth.whatami.JAVAMENU");
startActivity(menuIntent);
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
finish();
}
}
};
logoTimer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
and the manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="co.uk.mypchealth.whatami"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="co.uk.mypchealth.whatami.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".JavaMenu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="co.uk.mypchealth.whatami.JAVAMENU" />
<category android:name="android.intent.category.DEFUALT" />
</intent-filter>
</activity>
</application>
</manifest>
Im Hoping its something simple i mean jeeze the app does nothing yet lol.... i have the intent declared and and all the names i have checked and double checked ... just cant see the wood from the trees. Any help would be great.
Try,
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Intent menuIntent = new Intent(MainActivity.this, JavaMenu.class);
startActivity(menuIntent);
}
}, 3000);
try this :
<activity
android:name=".JavaMenu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="co.uk.mypchealth.whatami.JAVAMENU" />
<category android:name="android.intent.category.DEFUALT" />
</intent-filter>
</activity>
you have defined the app name in both activities try to change this activity declaration to :
<activity android:name="co.uk.mypchealth.whatami.JavaMenu" > </activity>

IntentService is not being called

I am trying to send an Intent from the onCreate in an Activity to start an IntentService. However the IntentService's onHandleIntent is never being received. I have tried changing around the Manifest with Intent-filters but nothing seems to be working. No exceptions are being thrown, but the IntentService is simply not called.
Here is the onCreate
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
db = new TwitterDB(this);
String[] columns = new String[1];
columns[0] = TwitterDB.TEXT;
tAdapter = new SimpleCursorAdapter(this, 0, null, columns, null, 0);
tweets = (ListView)findViewById(R.id.listtwitter);
tweets.setAdapter(tAdapter);
Intent intent = new Intent(this, SyncService.class);
intent.putExtra("action", SyncService.TWITTER_SYNC);
this.startService(intent);
}
Here is the creator and onHandleIntent of the IntentService class, I know it is not being called because logcat never shows "Retrieved intent". The constructor is not called either (There was a log call in there, but I removed it.
public SyncService(){
super("SyncService");
twitterURI = Uri.parse(TWITTER_URI);
Log.i("SyncService", "Constructed");
}
#Override
public void onHandleIntent(Intent i){
int action = i.getIntExtra("action", -1);
Log.i("SyncService", "Retrieved intent");
switch (action){
case TWITTER_SYNC:
try{
url = new URL(twitterString);
conn = (HttpURLConnection)url.openConnection();
syncTwitterDB();
conn.disconnect();
}catch(MalformedURLException e){
Log.w("SyncService", "Twitter URL is no longer valid.");
e.printStackTrace();
}catch(IOException e){
Log.w("SyncService", "Twitter connection could not be established.");
e.printStackTrace();
}
break;
default:;
// invalid request
}
}
And here is the Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.twitter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<application android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TwitterTwoActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<service android:name="SyncService"/>
</activity>
</application>
</manifest>
Move your service declaration outside of the scope of the TwitterTwoActivity in the XML file, as I have done here:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.twitter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<application android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TwitterTwoActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="SyncService"/>
</application>
</manifest>
You need to define the path to the service in your manifest, simply putting the name is not sufficient.
Change
<service android:name="SyncService"/>
to
<service android:name=".SyncService"/>
if SyncService is in the root of your package, add respective folder before .SyncService if needed.

Categories

Resources