Android programming crash null pointer exception
hey everyone at the moment im trying to display all the files in a certain folder on the android devices sd card in a
ListView
and so far im having bad luck,It crashes with a nullpointer exception so i was wondering if anyone here could shine some lite on my proble ill post my activity and logcat below and thanks.
Activity
package com.mkyong.android; import java.io.File; import java.util.ArrayList; import android.app.ListActivity; import android.os.Bundle; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; import android.view.View; public class ListMobileActivity extends ListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.res); } public ArrayList<String> GetFiles(String DirectoryPath) { ArrayList<String> MyFiles = new ArrayList<String>(); File f = new File(DirectoryPath); f.mkdirs(); File[] files = f.listFiles(); if (files.length == 0) return null; else { for (int i=0; i<files.length; i++) MyFiles.add(files[i].getName()); } return MyFiles; } { ListView lv; ArrayList<String> FilesInFolder = GetFiles("/sdcard/RootBox"); lv = (ListView)findViewById(R.id.listView1); lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, FilesInFolder)); lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { } }); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { //get selected items String selectedValue = (String) getListAdapter().getItem(position); Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show(); } }
and logcat
05-24 06:42:28.992: E/AndroidRuntime(796): FATAL EXCEPTION: main
05-24 06:42:28.992: E/AndroidRuntime(796): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mkyong.android/com.mkyong.android.ListMobileActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is ‘android.R.id.list’
05-24 06:42:28.992: E/AndroidRuntime(796): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-24 06:42:28.992: E/AndroidRuntime(796): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-24 06:42:28.992: E/AndroidRuntime(796): at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-24 06:42:28.992: E/AndroidRuntime(796): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-24 06:42:28.992: E/AndroidRuntime(796): at android.os.Handler.dispatchMessage(Handler.java:99)
Answer
If you have asked for permission in android manifest for reading storage, then this might be the problem.
It seems from the code that you are querying a instance of list view in the instance block.
This might be called before the setContentView is called.
You should instantiate a UI component in XML after the layout is inflated or else you will run into exception.
Try this.
public class ListMobileActivity extends ListActivity { ListView lv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.res); ArrayList<String> FilesInFolder = GetFiles("/sdcard/RootBox"); lv = (ListView) findViewById(R.id.listView1); lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, FilesInFolder)); lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { } }); } public ArrayList<String> GetFiles(String DirectoryPath) { ArrayList<String> MyFiles = new ArrayList<String>(); File f = new File(DirectoryPath); f.mkdirs(); File[] files = f.listFiles(); if (files.length == 0) return null; else { for (int i = 0; i < files.length; i++) MyFiles.add(files[i].getName()); } return MyFiles; } }
It should work.
Filed under: Android Coding FAQ
Tags:
how to get the files of assest subfolder in android?
How can i get the files of assets sub folder.in my Assets folder i have another sub folder(sample).I want to get all files of sample folder and store it into sdcard.Please can any one help me?
Thanking in Advance.
Answer
You can do it this way..
AssetManager assetManager = getAssets(); String[] files = assetManager.list("sample"); for(String filename : files) { ..... }
Filed under: Android Coding FAQ
Tags:
How to change the background button of image dynamically
I have a
Button
name is
Start
actually
Start
is a image which i have placed in my button as a background
android:background=”@drawable/btn_stop
what i want to do is By pressing the
Start
button it should replace the
Start
image with
Stop
I am able to do it in Java Code by using
btnStartStop.setBackgroundResource(R.drawable.btn_stop);
but i want to do it
XML
by using
selector
i have used this code
<Button android:id="@+id/button_start_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dip" android:layout_marginTop="10dip" android:background="@drawable/btn_selector"/>
Here btn_selector is
XML
file which i have created inside the
drawable
folder
which contain
btn_selector.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@drawable/btn_stop" android:state_pressed="true" /> <item android:drawable="@drawable/btn_start"/> </selector>
but its only creating a hovering effect for ex:it only showing
Stop
when i am pressing the
Start
for a second and then after its showing
Start
.
Note:I Don’t want to use Toggle Button
Answer
Your selector shoul be like,
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/switch_on" /> <item android:state_selected="true" android:drawable="@drawable/switch_on" /> <item android:drawable="@drawable/switch_off" /> <!-- default --> </selector>
And your listener should be like,
button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub if(button.isSelected()) { button.setSelected(false); } else { button.setSelected(true); } } });
Filed under: Android Coding FAQ
Tags:
How can I align text of the spinner to the centre?
Here is my XML of the spinner but it did not work.
<Spinner android:id="@+id/frequency" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/spinner_voices" android:gravity="center|center_horizontal" />
Answer
You need to set your own layout for spinner item.
ArrayAdapter adap = new ArrayAdapter<String>(this, R.layout.spinner_item, new String[]{"ABC", "DEF", "GHI"}); spriner.setAdapter(adap);
Where R.layout.spinner_item is a layout with content:
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:layout_width="fill_parent" android:gravity="center"/>
Filed under: Android Coding FAQ
Tags:
Auto cut background image edges
I would like to improve my androids app design with background image but its resizing/spreading it on screens and make the app uglier as you can imagine,
I googled the issue and the only thing I found is putting many images for smartphone, tablet etc… to get always the best sized image but I m finding a way to put a huge image cut edges when screen is too small without spreading all the image because I don’t need to see the whole image
Someone know is it possible and how?
Answer
What you need is to place an image view to your layout.Set height and width as match parent.Then set scaleType as centreCrop.And place all other views above this image view.By doing so, Android will crop the image according to screen size of devices.
Filed under: Android Coding FAQ
Tags: