|
|
|
Android สอบถามการดึงรูปจาก SDCard มาแสดง นำ Codeไปจากที่นี้และติด Error ครับ |
|
|
|
|
|
|
|
มันขึ้นประมาณนี้ครับ
Code
01-31 22:58:29.820: E/AndroidRuntime(12278): FATAL EXCEPTION: main
01-31 22:58:29.820: E/AndroidRuntime(12278): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.application1/com.application1.Main}: java.lang.NullPointerException
01-31 22:58:29.820: E/AndroidRuntime(12278): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
01-31 22:58:29.820: E/AndroidRuntime(12278): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
01-31 22:58:29.820: E/AndroidRuntime(12278): at android.app.ActivityThread.access$600(ActivityThread.java:128)
01-31 22:58:29.820: E/AndroidRuntime(12278): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
01-31 22:58:29.820: E/AndroidRuntime(12278): at android.os.Handler.dispatchMessage(Handler.java:99)
01-31 22:58:29.820: E/AndroidRuntime(12278): at android.os.Looper.loop(Looper.java:137)
01-31 22:58:29.820: E/AndroidRuntime(12278): at android.app.ActivityThread.main(ActivityThread.java:4514)
01-31 22:58:29.820: E/AndroidRuntime(12278): at java.lang.reflect.Method.invokeNative(Native Method)
01-31 22:58:29.820: E/AndroidRuntime(12278): at java.lang.reflect.Method.invoke(Method.java:511)
01-31 22:58:29.820: E/AndroidRuntime(12278): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
01-31 22:58:29.820: E/AndroidRuntime(12278): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
01-31 22:58:29.820: E/AndroidRuntime(12278): at dalvik.system.NativeStart.main(Native Method)
01-31 22:58:29.820: E/AndroidRuntime(12278): Caused by: java.lang.NullPointerException
01-31 22:58:29.820: E/AndroidRuntime(12278): at com.application1.Main.getSD(Main.java:49)
01-31 22:58:29.820: E/AndroidRuntime(12278): at com.application1.Main.onCreate(Main.java:33)
01-31 22:58:29.820: E/AndroidRuntime(12278): at android.app.Activity.performCreate(Activity.java:4465)
01-31 22:58:29.820: E/AndroidRuntime(12278): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
01-31 22:58:29.820: E/AndroidRuntime(12278): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
01-31 22:58:29.820: E/AndroidRuntime(12278): ... 11 more
ตัวCode
Code (Android-Java)
package com.application1;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class Main extends Activity {
List <String> ImageList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form2);
/*** Get Images from SDCard ***/
ImageList = getSD();
// gridView1
final GridView gView1 = (GridView)findViewById(R.id.gridView1);
gView1.setAdapter(new ImageAdapter(this,ImageList));
// OnClick
}
private List <String> getSD()
{
List <String> it = new ArrayList <String>();
File f = new File ("/mnt/sdcard/picture");
File[] files = f.listFiles ();
for (int i = 0; i <files.length; i++)
{
File file = files[i];
Log.d("Count",file.getPath());
it.add (file.getPath());
}
return it;
}
public class ImageAdapter extends BaseAdapter
{
private Context context;
private List <String> lis;
public ImageAdapter(Context c, List <String> li)
{
// TODO Auto-generated method stub
context = c;
lis = li;
}
public int getCount() {
// TODO Auto-generated method stub
return lis.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.showimage, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.textView1);
String strPath = lis.get(position).toString();
// Get File Name
String fileName = strPath.substring( strPath.lastIndexOf('/')+1, strPath.length() );
textView.setText(fileName);
// Image Resource
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);
Bitmap bm = BitmapFactory.decodeFile(strPath);
imageView.setImageBitmap(bm);
return convertView;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Tag : Mobile, Android, Mobile
|
|
|
|
|
|
Date :
2013-01-31 23:17:10 |
By :
MaliNo |
View :
1324 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|