Show GridView images from JSON Android - java

I have a problem when displaying images from JSON to GridView could help me .
GalleryActivity.java
public class GalleryActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery_layout);
Intent i = getIntent();
String tempid = i.getStringExtra("tempid");
GridView gridView = (GridView) findViewById(R.id.grid_view);
gridView.setAdapter(new ImageAdapter(this,tempid));
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
}
});
}
}
ImageAdapter.java
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public String[] mThumbIds;
private static String KEY_SUCCESS = "success";
private static String KEY_IMAGES = "images";
private static String KEY_IMAGE = "url_img";
// Constructor
public ImageAdapter(Context c,String tempID){
mContext = c;
final DealerFunctions dealerFunction = new DealerFunctions();
JSONObject json = dealerFunction.getImages(tempID);
try {
if (json.getString(KEY_SUCCESS) != null) {
String res = json.getString(KEY_SUCCESS);
if (Integer.parseInt(res) == 1) {
JSONArray imagesFields = json
.getJSONArray(KEY_IMAGES);
for (int i = 0; i < imagesFields.length(); i++) {
JSONObject x = imagesFields.getJSONObject(i);
mThumbIds[i] = x.getString(KEY_IMAGE);
}
} else {
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return mThumbIds[position];
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView==null){
imageView = new ImageView(mContext);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
}else{
imageView = (ImageView) convertView;
}
imageView.setBackgroundDrawable(Drawable.createFromPath(mThumbIds[position]));
return imageView;
}
}
Don't Work...
when I run the application, closes immediately assume that the problem comes in imageView.setBackgroundDrawable (Drawable.createFromPath (mThumbIds [position]));
I need your help guys
Logcat Errors as follows.
03-27 10:50:12.658: D/AndroidRuntime(1782): Shutting down VM
03-27 10:50:12.658: W/dalvikvm(1782): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
03-27 10:50:12.678: E/AndroidRuntime(1782): FATAL EXCEPTION: main
03-27 10:50:12.678: E/AndroidRuntime(1782): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.montalvo.dealer/com.montalvo.dealer.GalleryActivity}: java.lang.NullPointerException
03-27 10:50:12.678: E/AndroidRuntime(1782): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-27 10:50:12.678: E/AndroidRuntime(1782): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-27 10:50:12.678: E/AndroidRuntime(1782): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-27 10:50:12.678: E/AndroidRuntime(1782): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-27 10:50:12.678: E/AndroidRuntime(1782): at android.os.Handler.dispatchMessage(Handler.java:99)
03-27 10:50:12.678: E/AndroidRuntime(1782): at android.os.Looper.loop(Looper.java:123)
03-27 10:50:12.678: E/AndroidRuntime(1782): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-27 10:50:12.678: E/AndroidRuntime(1782): at java.lang.reflect.Method.invokeNative(Native Method)
03-27 10:50:12.678: E/AndroidRuntime(1782): at java.lang.reflect.Method.invoke(Method.java:521)
03-27 10:50:12.678: E/AndroidRuntime(1782): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-27 10:50:12.678: E/AndroidRuntime(1782): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-27 10:50:12.678: E/AndroidRuntime(1782): at dalvik.system.NativeStart.main(Native Method)
03-27 10:50:12.678: E/AndroidRuntime(1782): Caused by: java.lang.NullPointerException
03-27 10:50:12.678: E/AndroidRuntime(1782): at com.montalvo.dealer.ImageAdapter.<init>(ImageAdapter.java:57)
03-27 10:50:12.678: E/AndroidRuntime(1782): at com.montalvo.dealer.GalleryActivity.onCreate(GalleryActivity.java:25)
03-27 10:50:12.678: E/AndroidRuntime(1782): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-27 10:50:12.678: E/AndroidRuntime(1782): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-27 10:50:12.678: E/AndroidRuntime(1782): ... 11 more
03-27 10:50:15.501: I/Process(1782): Sending signal. PID: 1782 SIG: 9

You are mixing things up here, use this
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
imageView = new ImageView(mContext);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
imageView.setBackgroundDrawable(Drawable.createFromPath(mThumbIds[position]));
return imageView;
}
The pattern you have used is that of the ViewHolder but you are not creating any views. If you do want to use the ViewHolder model then,
first create an xml with an ImageView.
Create a ViewHolder class in your ImageAdapter with an ImageView.
In the getView() method, inflate the xml.
create the viewholder for the case of convertview == null
set the viewholder as the TAG of the convertview.
in the else condtion, assign the previously created viewholder to the convertview.

Related

Android CursorAdapter giving java.lang.NullPointerException error

I am having a few issues running my custom CursorAdapter in my application. According to my logcat, the error I get:
11-08 06:41:03.228 6109-6109/? W/System.err? java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.name/com.app.name.MyActivity}: java.lang.NullPointerException
11-08 06:41:03.229 6109-6109/? W/System.err? at com.app.name.MyActivity.onCreate(MyActivity.java:71)
11-08 06:41:03.231 6109-6109/? E/AndroidRuntime? FATAL EXCEPTION: main
occurs at line 71 which sets the adapter (obj.setAdapter(myAdapter)). Initially I thought my database data retrieve function (Cursor chatCursor = mydb.selectConversation(msgId);) did not return any data but after testing it with the chatCursor.getCount() function, I realized that was not the case. Kindly assist me in solving this issue. Below are the codes for my activity, adapter and logcat. Thanks in advance.
MyAdapter.java
public class MyAdapter extends CursorAdapter {
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public MyAdapter(Context context, Cursor cursor) {
super(context, cursor, 0);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.chat_left, parent, false);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView user = (TextView) view.findViewById(R.id.reply_user);
TextView msg = (TextView) view.findViewById(R.id.reply_msg);
String theTime = cursor.getString(cursor.getColumnIndexOrThrow("message_id"));
String theMessage = cursor.getString(cursor.getColumnIndexOrThrow("message"));
user.setText(theTime);
msg.setText(String.valueOf(theMessage));
}
}
MyActivity.java
public class MyActivity extends ListActivity {
//Utils Class
Utils util;
final Context context = this;
private ProgressBar dialog;
ListView obj;
DBHelper mydb;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
util = new Utils(this);
mydb = new DBHelper(this);
Bundle extras = getIntent().getExtras();
final Integer msgId = extras.getInt("id");
obj = (ListView) findViewById(R.id.list);
Cursor chatCursor = mydb.selectConversation(msgId);
MyAdapter myAdapter = new MyAdapter(this, chatCursor);
obj.setAdapter(myAdapter);
}
}
logcat
11-08 06:41:03.228 6109-6109/? W/System.err? java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.name/com.app.name.MyActivity}: java.lang.NullPointerException
11-08 06:41:03.229 6109-6109/? W/System.err? at com.app.name.MyActivity.onCreate(MyActivity.java:71)
11-08 06:41:03.231 6109-6109/? E/AndroidRuntime? FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.name/com.app.name.MyActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2351)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.access$600(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5370)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.app.name.MyActivity.onCreate(MyActivity.java:71)
at android.app.Activity.performCreate(Activity.java:5122)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1150)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2315)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.access$600(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5370)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
11-08 06:41:03.238 559-832/? W/ActivityManager? Force finishing activity com.app.name/.MyActivity
11-08 06:41:03.742 559-574/? W/ActivityManager? Activity pause timeout for ActivityRecord{42e71938 u0 com.app.name/.MyActivity}
Take a look to your activity_view.xml. Probably the listview ID isn't "list" as you state when you try to find it with findViewById(R.layout.list).

Android Outofmemoryerror?

I have these codes for my android activity. But it gave me java.lang.outofmemoryerror: java.lang.string[] of length 2131492864 ecxeeds the VM limit. I want to know if there is something wrong with my codes or is it my String.xml contains too much items? Or there's too many pictures? I have around 120 150x150 image that is around 20kb each. If possible I would like to know how can i solve this issue. Thanks in advance.
public class ChampionInfo extends FragmentActivity {
GridView gridtable;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_champion_info);
gridtable = (GridView) findViewById(R.id.gridtable);
gridtable.setAdapter(new GridAdapter(this));
}
public class GridAdapter extends BaseAdapter {
private Context mContext;
String[] list = new String[R.array.championlist];
int[] champImage = getImage();
public int[] getImage() {
int[] tempImage = new int[list.length];
for (int i = 0; i > list.length; i++) {
tempImage[i] = getResources().getIdentifier(list[i],
"drawable", getPackageName());
}
return tempImage;
}
// Constructor
public GridAdapter(Context c) {
mContext = c;
}
public int getCount() {
return champImage.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(champImage[position]);
return imageView;
}
// Keep all Images in array
}
}
These are the error that shows on the catlog:
09-26 06:09:38.869: E/AndroidRuntime(2653): FATAL EXCEPTION: main
09-26 06:09:38.869: E/AndroidRuntime(2653): Process: com.example.loldb, PID: 2653
09-26 06:09:38.869: E/AndroidRuntime(2653): java.lang.OutOfMemoryError: java.lang.String[] of length 2131492864 exceeds the VM limit
09-26 06:09:38.869: E/AndroidRuntime(2653): at com.example.loldb.ChampionInfo$GridAdapter.(ChampionInfo.java:50)
09-26 06:09:38.869: E/AndroidRuntime(2653): at com.example.loldb.ChampionInfo.onCreate(ChampionInfo.java:44)
09-26 06:09:38.869: E/AndroidRuntime(2653): at android.app.Activity.performCreate(Activity.java:5231)
09-26 06:09:38.869: E/AndroidRuntime(2653): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-26 06:09:38.869: E/AndroidRuntime(2653): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
09-26 06:09:38.869: E/AndroidRuntime(2653): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
09-26 06:09:38.869: E/AndroidRuntime(2653): at android.app.ActivityThread.access$800(ActivityThread.java:135)
09-26 06:09:38.869: E/AndroidRuntime(2653): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-26 06:09:38.869: E/AndroidRuntime(2653): at android.os.Handler.dispatchMessage(Handler.java:102)
09-26 06:09:38.869: E/AndroidRuntime(2653): at android.os.Looper.loop(Looper.java:136)
09-26 06:09:38.869: E/AndroidRuntime(2653): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-26 06:09:38.869: E/AndroidRuntime(2653): at java.lang.reflect.Method.invokeNative(Native Method)
09-26 06:09:38.869: E/AndroidRuntime(2653): at java.lang.reflect.Method.invoke(Method.java:515)
09-26 06:09:38.869: E/AndroidRuntime(2653): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-26 06:09:38.869: E/AndroidRuntime(2653): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-26 06:09:38.869: E/AndroidRuntime(2653): at dalvik.system.NativeStart.main(Native Method)
This line doesn't make much sense:
String[] list = new String[R.array.championlist];
R.array.championlist is an integer that points to the what I assume is a string array resource. You can load that string array with Resources.getStringArray(int).
String[] list = resources.getStringArray(R.array.championlist);

Update and remove GridView items at runtime

i am developing a project where i compare the images of two items,So if two items will have same image after clicking these items should be hide. my code is given below and this code encounter a problem. is this a logical error or any other issue? i try to solve the issue but did't resolve.. Please guide me... here is my main Activity.
MainActivity.java
public class MainActivity extends Activity {
Context ctx;
int imagesArray[];
ImageAdapter adapter;
List<Integer> pictures;
boolean flage = false;
GridView gridView;
int save1, save2;
int img1 = -1, img2 = -1;
public int OriginalArray[] = { R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_0,
R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3 };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ImageView myimage = new ImageView(ctx);
gridView = (GridView) findViewById(R.id.gv_memory);
gridView.setAdapter(adapter);
shuffleArray();
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
myimage.setImageResource(pictures.get(position));
if (flage == false) {
img1 = pictures.get(position);
flage = true;
} else if (flage == true) {
img2 = pictures.get(position);
checkResult();
flage = false;
}
}
private void checkResult() {
// TODO Auto-generated method stub
if (img1 == img2) {
adapter.pictureList.remove(img1);
adapter.pictureList.remove(img2);
adapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, "Congratualatin !!!!",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "Sorry!!!!",
Toast.LENGTH_LONG).show();
}
}
});
}
private void shuffleArray() {
// TODO Auto-generated method stub
pictures = new ArrayList<Integer>();
for (int index = 0; index < OriginalArray.length; index++) {
pictures.add(OriginalArray[index]);
}
Collections.shuffle(pictures);
}
}
ImageAdapter.java
public class ImageAdapter extends BaseAdapter {
private Context context;
List<Integer> pictureList = new ArrayList<Integer>();
public ImageAdapter(Context c) {
context = c;
for (int i = 0; i < 8; i++) {
pictureList.add(R.drawable.question);
}
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return (pictureList.size());
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return pictureList.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView myimage = new ImageView(context);
myimage.setImageResource(pictureList.get(position));
myimage.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
myimage.setLayoutParams(new GridView.LayoutParams(70, 70));
return myimage;
}
}
LogCat.
03-14 06:09:03.793: D/dalvikvm(2877): GC_FOR_ALLOC freed 54K, 8% free 2771K/2996K, paused 111ms, total 117ms
03-14 06:09:03.802: I/dalvikvm-heap(2877): Grow heap (frag case) to 3.943MB for 1127536-byte allocation
03-14 06:09:03.922: D/dalvikvm(2877): GC_FOR_ALLOC freed 2K, 6% free 3870K/4100K, paused 118ms, total 118ms
03-14 06:09:03.962: D/AndroidRuntime(2877): Shutting down VM
03-14 06:09:03.962: W/dalvikvm(2877): threadid=1: thread exiting with uncaught exception (group=0x41465700)
03-14 06:09:03.972: E/AndroidRuntime(2877): FATAL EXCEPTION: main
03-14 06:09:03.972: E/AndroidRuntime(2877): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.memory/com.example.memory.MainActivity}: java.lang.NullPointerException
03-14 06:09:03.972: E/AndroidRuntime(2877): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
03-14 06:09:03.972: E/AndroidRuntime(2877): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
03-14 06:09:03.972: E/AndroidRuntime(2877): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-14 06:09:03.972: E/AndroidRuntime(2877): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
03-14 06:09:03.972: E/AndroidRuntime(2877): at android.os.Handler.dispatchMessage(Handler.java:99)
03-14 06:09:03.972: E/AndroidRuntime(2877): at android.os.Looper.loop(Looper.java:137)
03-14 06:09:03.972: E/AndroidRuntime(2877): at android.app.ActivityThread.main(ActivityThread.java:5103)
03-14 06:09:03.972: E/AndroidRuntime(2877): at java.lang.reflect.Method.invokeNative(Native Method)
03-14 06:09:03.972: E/AndroidRuntime(2877): at java.lang.reflect.Method.invoke(Method.java:525)
03-14 06:09:03.972: E/AndroidRuntime(2877): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-14 06:09:03.972: E/AndroidRuntime(2877): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-14 06:09:03.972: E/AndroidRuntime(2877): at dalvik.system.NativeStart.main(Native Method)
03-14 06:09:03.972: E/AndroidRuntime(2877): Caused by: java.lang.NullPointerException
03-14 06:09:03.972: E/AndroidRuntime(2877): at android.view.ViewConfiguration.get(ViewConfiguration.java:318)
03-14 06:09:03.972: E/AndroidRuntime(2877): at android.view.View.<init>(View.java:3264)
03-14 06:09:03.972: E/AndroidRuntime(2877): at android.widget.ImageView.<init>(ImageView.java:112)
03-14 06:09:03.972: E/AndroidRuntime(2877): at com.example.memory.MainActivity.onCreate(MainActivity.java:33)
03-14 06:09:03.972: E/AndroidRuntime(2877): at android.app.Activity.performCreate(Activity.java:5133)
03-14 06:09:03.972: E/AndroidRuntime(2877): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-14 06:09:03.972: E/AndroidRuntime(2877): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
03-14 06:09:03.972: E/AndroidRuntime(2877): ... 11 more
03-14 06:14:04.503: I/Process(2877): Sending signal. PID: 2877 SIG: 9
You have
final ImageView myimage = new ImageView(ctx);
ctz is not initialized. Its only declared as Context ctx;
You have this
gridView.setAdapter(adapter);
But you need to intialize adapter before using the same
So change to
setContentView(R.layout.main);
final ImageView myimage = new ImageView(this); //this refers to Activity context
gridView = (GridView) findViewById(R.id.gv_memory);
adapter = new ImageAdapter(this)
gridView.setAdapter(adapter);

Android gallery view in dialog results in nullpointerexception

So I've used the below code elsewhere and it has worked fine but now I'd like to use it in an alert dialog. Problem is that whenever I set the adapter it results in a nullpointerexception. The code (minus the alertdialog) is pretty much all right from the dev tutorial here:
http://developer.android.com/resources/tutorials/views/hello-gallery.html
If I comment out the line:
gallery.setAdapter(new ImageAdapter(this));
the dialog opens fine but the moment I set adapter results in error. Any ideas?
Here is the code for my alertdialog:
private void statusbarCustom() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
View view = LayoutInflater.from(this).inflate(R.layout.custom_icon, null);
final EditText cTitle = (EditText)view.findViewById(R.id.search_term);
Gallery gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter(this));
builder.setView(view);
builder.setPositiveButton("Continue", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton("Cancel", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.setTitle("Statusbar");
alertDialog.show();
}
And here is the imageadapter code:
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private Integer[] mImageIds = {
R.drawable.attach,
R.drawable.bell,
R.drawable.book_addresses,
R.drawable.book,
R.drawable.cake,
R.drawable.calculator,
R.drawable.calendar,
R.drawable.camera,
R.drawable.car,
R.drawable.cart,
R.drawable.chart_curve,
R.drawable.chart_pie_edit,
R.drawable.clock_,
R.drawable.computer,
R.drawable.controller,
R.drawable.cup,
R.drawable.date,
R.drawable.emotion_evilgrin,
R.drawable.emotion_grin,
R.drawable.emotion_happy,
R.drawable.emotion_smile,
R.drawable.emotion_suprised,
R.drawable.emotion_tongue,
R.drawable.emotion_unhappy,
R.drawable.emotion_waii,
R.drawable.emotion_wink,
R.drawable.exclamation,
R.drawable.film,
R.drawable.folder,
R.drawable.group,
R.drawable.heart,
R.drawable.house,
R.drawable.key,
R.drawable.lightbulb,
R.drawable.lightning,
R.drawable.lock,
R.drawable.lorry,
R.drawable.map,
R.drawable.money_euro,
R.drawable.money_pound,
R.drawable.money_yen,
R.drawable.money,
R.drawable.shop,
R.drawable.compass,
R.drawable.sofa,
R.drawable.gift,
R.drawable.smartphone,
R.drawable.accept,
R.drawable.add,
R.drawable.sound_none,
R.drawable.newspaper,
R.drawable.painbrush,
R.drawable.rainbow,
R.drawable.report,
R.drawable.ruby,
R.drawable.shield,
R.drawable.sport_8ball,
R.drawable.sport_basketball,
R.drawable.sport_football,
R.drawable.sport_raquet,
R.drawable.sport_shuttlecock,
R.drawable.sport_soccer,
R.drawable.sport_tennis,
R.drawable.star,
R.drawable.stop,
R.drawable.table_icon,
R.drawable.telephone,
R.drawable.television,
R.drawable.facebook
};
public ImageAdapter(Context c) {
mContext = c;
TypedArray attr = mContext.obtainStyledAttributes(R.styleable.GalleryTheme);
mGalleryItemBackground = attr.getResourceId(
R.styleable.GalleryTheme_android_galleryItemBackground, 0);
attr.recycle();
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mImageIds[position]);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
}
And here is logcat output:
02-21 10:40:29.317: E/AndroidRuntime(3347): FATAL EXCEPTION: main
02-21 10:40:29.317: E/AndroidRuntime(3347): java.lang.NullPointerException
02-21 10:40:29.317: E/AndroidRuntime(3347): at com.flufflydelusions.app.enotesclassic.NoteEdit.statusbarCustom(NoteEdit.java:1954)
02-21 10:40:29.317: E/AndroidRuntime(3347): at com.flufflydelusions.app.enotesclassic.NoteEdit.access$67(NoteEdit.java:1949)
02-21 10:40:29.317: E/AndroidRuntime(3347): at com.flufflydelusions.app.enotesclassic.NoteEdit$25.onClick(NoteEdit.java:1835)
02-21 10:40:29.317: E/AndroidRuntime(3347): at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:935)
02-21 10:40:29.317: E/AndroidRuntime(3347): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
02-21 10:40:29.317: E/AndroidRuntime(3347): at android.widget.ListView.performItemClick(ListView.java:3746)
02-21 10:40:29.317: E/AndroidRuntime(3347): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1981)
02-21 10:40:29.317: E/AndroidRuntime(3347): at android.os.Handler.handleCallback(Handler.java:587)
02-21 10:40:29.317: E/AndroidRuntime(3347): at android.os.Handler.dispatchMessage(Handler.java:92)
02-21 10:40:29.317: E/AndroidRuntime(3347): at android.os.Looper.loop(Looper.java:130)
02-21 10:40:29.317: E/AndroidRuntime(3347): at android.app.ActivityThread.main(ActivityThread.java:3691)
02-21 10:40:29.317: E/AndroidRuntime(3347): at java.lang.reflect.Method.invokeNative(Native Method)
02-21 10:40:29.317: E/AndroidRuntime(3347): at java.lang.reflect.Method.invoke(Method.java:507)
02-21 10:40:29.317: E/AndroidRuntime(3347): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
02-21 10:40:29.317: E/AndroidRuntime(3347): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
02-21 10:40:29.317: E/AndroidRuntime(3347): at dalvik.system.NativeStart.main(Native Method)
Check if your gallery is null.
Was missing "view" in
view.findViewById for gallery
At this line:
Gallery gallery = (Gallery) findViewById(R.id.gallery);
The gallery has to be in the view set via setContentView. setContentView also has to come before the findViewByID. Otherwise you need to use a layout inflater and get the gallery view as such:
View view = activityContext.getLayoutInflater().inflate(R.layout.gallery_xml, null);
Gallery gallery = (Gallery)view.findViewById(R.id.gallery);

Custom list starting new activity?

I have built a customized list item to replace the android's simple list item by inflating each list item. The customized list is working and it consists of an image view and a text view.
My problem is, when I try to launch a new activity after a list item is clicked nothing happens even the app wont crash. So, is there a way to launch an activity using list view???
public class activityone extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activitylayout);
setListAdapter(new MyAdapter(this, android.R.layout.simple_list_item_1, R.id.textView1,
getResources().getStringArray(R.array.names)));
}
public void onListItemClick(ListView l, View v, int position,
long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Object o = this.getListAdapter().getItem(position);
if(getSelectedItemPosition() == 0){
Intent intent = new Intent(activityone.this,no1.class);
startActivity(intent);
}
.
.
.
.
}
private class MyAdapter extends ArrayAdapter<String>{
public MyAdapter(Context context, int resource, int textViewResourceId,
String[] strings) {
super(context, textViewResourceId, strings);
// TODO Auto-generated constructor stub
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.list_item, parent, false);
String[] items = getResources().getStringArray(R.array.names);
ImageView iv = (ImageView) row.findViewById(R.id.imageView1);
TextView tv = (TextView) row.findViewById(R.id.textView1);
tv.setText(items[position]);
if(items[position].equals("john")){
iv.setImageResource(R.drawable.john);
}
.
.
.
.
return row;
}
}
}
And here is the no1 class
public class no1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.name_profile);
CharSequence t1 = "Name: john";
CharSequence t2 = "Age: 31";
CharSequence t3 = "Nationality: Saudi";
CharSequence t4 = "Number: 765646454";
ImageView iv = (ImageView) findViewById(R.drawable.imageView1);
TextView tv1 = (TextView) findViewById(R.id.textView1);
TextView tv2 = (TextView) findViewById(R.id.textView2);
TextView tv3 = (TextView) findViewById(R.id.textView3);
TextView tv4 = (TextView) findViewById(R.id.textView4);
iv.setImageResource(R.drawable.john);
tv1.setText(t1);
tv2.setText(t2);
tv3.setText(t3);
tv4.setText(t4);
}
}
And yes my activities are added to the manifest xml file.
Thanks for your help.
#Vineet Shukla I don't know how to do this i placed a break point but I couldn't debug i am new to eclipse environment could you explain more please
#Divyesh
I edited the code and the app now crashes when i press a list item here is the Logcat trace
09-17 10:10:57.686: ERROR/AndroidRuntime(365): FATAL EXCEPTION: main
09-17 10:10:57.686: ERROR/AndroidRuntime(365): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abc.act/com.abc.act.no1}: java.lang.NullPointerException
09-17 10:10:57.686: ERROR/AndroidRuntime(365): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-17 10:10:57.686: ERROR/AndroidRuntime(365): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-17 10:10:57.686: ERROR/AndroidRuntime(365): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-17 10:10:57.686: ERROR/AndroidRuntime(365): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-17 10:10:57.686: ERROR/AndroidRuntime(365): at android.os.Handler.dispatchMessage(Handler.java:99)
09-17 10:10:57.686: ERROR/AndroidRuntime(365): at android.os.Looper.loop(Looper.java:123)
09-17 10:10:57.686: ERROR/AndroidRuntime(365): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-17 10:10:57.686: ERROR/AndroidRuntime(365): at java.lang.reflect.Method.invokeNative(Native Method)
09-17 10:10:57.686: ERROR/AndroidRuntime(365): at java.lang.reflect.Method.invoke(Method.java:521)
09-17 10:10:57.686: ERROR/AndroidRuntime(365): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-17 10:10:57.686: ERROR/AndroidRuntime(365): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-17 10:10:57.686: ERROR/AndroidRuntime(365): at dalvik.system.NativeStart.main(Native Method)
09-17 10:10:57.686: ERROR/AndroidRuntime(365): Caused by: java.lang.NullPointerException
09-17 10:10:57.686: ERROR/AndroidRuntime(365): at com.abc.act.no1.onCreate(no1.java:31)
09-17 10:10:57.686: ERROR/AndroidRuntime(365): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-17 10:10:57.686: ERROR/AndroidRuntime(365): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-17 10:10:57.686: ERROR/AndroidRuntime(365): ... 11 more
You're not effectively using this API, you should switch to SimpleAdapter because handles more complex List Item layouts and data bindings. Instead of creating intractable branch statements you need to use the underlying data of the list effectively.

Categories

Resources