Open a GridView Activity from a Button on another activity

kamil5

New Member
I'm having an issue when trying to open a GridView from a button. When I do this and add the corresponding java and xml files the app either doesn't open properly or crashes when I try to run the app. If anyone could assist that would be great.\[code\] import java.util.ArrayList; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView; import android.widget.Toast; public class GridViewExampleActivity extends Activity { /** Called when the activity is first created. */ private GridviewAdapter mAdapter; private ArrayList<String> listCountry; private ArrayList<Integer> listFlag; private GridView gridView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); prepareList(); Intent i = getIntent(); // prepared arraylist and passed it to the Adapter class mAdapter = new GridviewAdapter(this, listCountry, listFlag); // Set custom adapter to gridview gridView = (GridView) findViewById(R.id.gridView1); gridView.setAdapter(mAdapter); // Implement On Item click listener gridView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { Toast.makeText(GridViewExampleActivity.this, mAdapter.getItem(position), Toast.LENGTH_SHORT).show(); } }); } public void prepareList() { listCountry = new ArrayList<String>(); listCountry.add("india"); listCountry.add("Brazil"); listCountry.add("Canada"); listCountry.add("China"); listCountry.add("France"); listCountry.add("Germany"); listCountry.add("Iran"); listCountry.add("Italy"); listCountry.add("Japan"); listCountry.add("Korea"); listCountry.add("Mexico"); listCountry.add("Netherlands"); listCountry.add("Portugal"); listCountry.add("Russia"); listCountry.add("Saudi Arabia"); listCountry.add("Spain"); listCountry.add("Turkey"); listCountry.add("United Kingdom"); listCountry.add("United States"); listFlag = new ArrayList<Integer>(); listFlag.add(R.drawable.ic_launcher); listFlag.add(R.drawable.ic_launcher); listFlag.add(R.drawable.ic_launcher); listFlag.add(R.drawable.ic_launcher); listFlag.add(R.drawable.ic_launcher); listFlag.add(R.drawable.ic_launcher); listFlag.add(R.drawable.ic_launcher); listFlag.add(R.drawable.ic_launcher); listFlag.add(R.drawable.ic_launcher); listFlag.add(R.drawable.ic_launcher); listFlag.add(R.drawable.ic_launcher); listFlag.add(R.drawable.ic_launcher); listFlag.add(R.drawable.ic_launcher); listFlag.add(R.drawable.ic_launcher); listFlag.add(R.drawable.ic_launcher); listFlag.add(R.drawable.ic_launcher); listFlag.add(R.drawable.ic_launcher); listFlag.add(R.drawable.ic_launcher); listFlag.add(R.drawable.ic_launcher); } } import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class WelcomeActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); Button btnNextScreen = (Button) findViewById(R.id.button1); btnNextScreen.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent nextScreen = new Intent(getApplicationContext(), GridViewExampleActivity.class); startActivity(nextScreen); } }); } } import java.util.ArrayList; import android.app.Activity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; public class GridviewAdapter extends BaseAdapter { private ArrayList<String> listCountry; private ArrayList<Integer> listFlag; private Activity activity; public GridviewAdapter(Activity activity,ArrayList<String> listCountry, A ArrayList<Integer> listFlag) { super(); this.listCountry = listCountry; this.listFlag = listFlag; this.activity = activity; } @Override public int getCount() { // TODO Auto-generated method stub return listCountry.size(); } @Override public String getItem(int position) { // TODO Auto-generated method stub return listCountry.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } public static class ViewHolder { public ImageView imgViewFlag; public TextView txtViewTitle; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder view; LayoutInflater inflator = activity.getLayoutInflater(); if(convertView==null) { view = new ViewHolder(); convertView = inflator.inflate(R.layout.gridview_row, null); view.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1); view.imgViewFlag = (ImageView) convertView.findViewById(R.id.imageView1); convertView.setTag(view); } else { view = (ViewHolder) convertView.getTag(); } view.txtViewTitle.setText(listCountry.get(position)); view.imgViewFlag.setImageResource(listFlag.get(position)); return convertView; } } \[/code\]Manifest:\[code\] <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.easyandroidtutgridviewwithtext" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:theme="@style/AppTheme" > <activity android:name="com.example.easyandroidtutgridviewwithtext.WelcomeActivity" > > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".GridViewExampleActivity" ></activity> <activity android:name=".GridviewAdapter" > </activity> </application> </manifest>\[/code\]LogCat:\[code\] 03-17 04:37:23.244: I/PackageManager(60): Removing non-system package:com.example.easyandroidtutgridviewwithtext 03-17 04:37:23.244: I/ActivityManager(60): Force stopping package com.example.easyandroidtutgridviewwithtext uid=10062 03-17 04:37:23.382: D/PackageManager(60): Scanning package com.example.easyandroidtutgridviewwithtext 03-17 04:37:23.382: I/PackageManager(60): Package com.example.easyandroidtutgridviewwithtext codePath changed from /data/app/com.example.easyandroidtutgridviewwithtext-1.apk to /data/app/com.example.easyandroidtutgridviewwithtext-2.apk; Retaining data and using new 03-17 04:37:23.382: I/PackageManager(60): Unpacking native libraries for /data/app/com.example.easyandroidtutgridviewwithtext-2.apk 03-17 04:37:23.421: D/installd(34): DexInv: --- BEGIN '/data/app/com.example.easyandroidtutgridviewwithtext-2.apk' --- 03-17 04:37:24.511: D/dalvikvm(941): DexOpt: load 168ms, verify+opt 638ms 03-17 04:37:24.581: D/installd(34): DexInv: --- END '/data/app/com.example.easyandroidtutgridviewwithtext-2.apk' (success) --- 03-17 04:37:24.591: W/PackageManager(60): Code path for pkg : com.example.easyandroidtutgridviewwithtext changing from /data/app/com.example.easyandroidtutgridviewwithtext-1.apk to /data/app/com.example.easyandroidtutgridviewwithtext-2.apk 03-17 04:37:24.591: W/PackageManager(60): Resource path for pkg : com.example.easyandroidtutgridviewwithtext changing from /data/app/com.example.easyandroidtutgridviewwithtext-1.apk to /data/app/com.example.easyandroidtutgridviewwithtext-2.apk 03-17 04:37:24.591: D/PackageManager(60): Activities: com.example.easyandroidtutgridviewwithtext.WelcomeActivity com.example.easyandroidtutgridviewwithtext.GridViewExampleActivity com.example.easyandroidtutgridviewwithtext.GridviewAdapter 03-17 04:37:24.631: I/ActivityManager(60): Force stopping package com.example.easyandroidtutgridviewwithtext uid=10062 03-17 04:37:24.901: D/dalvikvm(60): GC_CONCURRENT freed 1177K, 47% free 4611K/8647K, external 3511K/3903K, paused 6ms+8ms 03-17 04:37:25.081: I/installd(34): move /data/dalvik- cache/data@[email protected]@classes.dex -> / data/dalvik-cache/data@[email protected] [email protected] 03-17 04:37:25.081: D/PackageManager(60): New package installed in /data/app/com.example.easyandroidtutgridviewwithtext-2.apk 03-17 04:37:25.261: I/ActivityManager(60): Force stopping package com.example.easyandroidtutgridviewwithtext uid=10062 03-17 04:37:25.382: D/dalvikvm(124): GC_EXPLICIT freed 52K, 48% free 3119K/5959K, external 5470K/6279K, paused 116ms 03-17 04:37:25.672: W/RecognitionManagerService(60): no available voice recognition services found 03-17 04:37:25.991: D/dalvikvm(60): GC_EXPLICIT freed 470K, 49% free 4489K/8647K, external 3511K/3903K, paused 116ms 03-17 04:37:26.047: I/installd(34): unlink /data/dalvik- cache/data@[email protected]@classes.dex 03-17 04:37:26.071: D/AndroidRuntime(932): Shutting down VM 03-17 04:37:26.091: D/dalvikvm(932): GC_CONCURRENT freed 101K, 72% free 295K/1024K, external 0K/0K, paused 1ms+1ms 03-17 04:37:26.091: D/jdwp(932): Got wake-up signal, bailing out of select 03-17 04:37:26.091: D/dalvikvm(932): Debugger has detached; object registry had 1 entries 03-17 04:37:26.115: I/AndroidRuntime(932): NOTE: attach of thread 'Binder Thread #3' failed 03-17 04:37:26.661: D/AndroidRuntime(946): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<< 03-17 04:37:26.661: D/AndroidRuntime(946): CheckJNI is ON 03-17 04:37:27.471: D/AndroidRuntime(946): Calling main entry com.android.commands.am.Am 03-17 04:37:27.521: I/ActivityManager(60): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.easyandroidtutgridviewwithtext/.WelcomeActivity } from pid 946 03-17 04:37:27.702: I/ActivityManager(60): Start proc com.example.easyandroidtutgridviewwithtext for activity com.example.easyandroidtutgridviewwithtext/.WelcomeActivity: pid=955 uid=10062 gids={} 03-17 04:37:27.767: D/AndroidRuntime(946): Shutting down VM 03-17 04:37:27.781: D/dalvikvm(946): GC_CONCURRENT freed 103K, 69% free 320K/1024K, external 0K/0K, paused 1ms+1ms 03-17 04:37:27.781: D/jdwp(946): Got wake-up signal, bailing out of select 03-17 04:37:27.781: D/dalvikvm(946): Debugger has detached; object registry had 1 entries 03-17 04:37:27.831: I/AndroidRuntime(946): NOTE: attach of thread 'Binder Thread #3' failed 03-17 04:37:28.772: D/AndroidRuntime(955): Shutting down VM 03-17 04:37:28.772: W/dalvikvm(955): threadid=1: thread exiting with uncaught exception (group=0x40015560) 03-17 04:37:28.786: E/AndroidRuntime(955): FATAL EXCEPTION: main 03-17 04:37:28.786: E/AndroidRuntime(955): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.easyandroidtutgridviewwithtext/com.example. easyandroidtutgridviewwithtext.WelcomeActivity}: java.lang.NullPointerException 03-17 04:37:28.786: E/AndroidRuntime(955): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 03-17 04:37:28.786: E/AndroidRuntime(955): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 03-17 04:37:28.786: E/AndroidRuntime(955): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 03-17 04:37:28.786: E/AndroidRuntime(955): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 03-17 04:37:28.786: E/AndroidRuntime(955): at android.os.Handler.dispatchMessage(Handler.java:99) 03-17 04:37:28.786: E/AndroidRuntime(955): at android.os.Looper.loop(Looper.java:123) 03-17 04:37:28.786: E/AndroidRuntime(955): at android.app.ActivityThread.main(ActivityThread.java:3683) 03-17 04:37:28.786: E/AndroidRuntime(955): at java.lang.reflect.Method.invokeNative(Native Method) 03-17 04:37:28.786: E/AndroidRuntime(955): at java.lang.reflect.Method.invoke(Method.java:507) 03-17 04:37:28.786: E/AndroidRuntime(955): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 03-17 04:37:28.786: E/AndroidRuntime(955): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 03-17 04:37:28.786: E/AndroidRuntime(955): at dalvik.system.NativeStart.main(Native Method) 03-17 04:37:28.786: E/AndroidRuntime(955): Caused by: java.lang.NullPointerException 03-17 04:37:28.786: E/AndroidRuntime(955): at com.example.easyandroidtutgridviewwithtext.WelcomeActivity. onCreate(WelcomeActivity.java:17) 03-17 04:37:28.786: E/AndroidRuntime(955): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 03-17 04:37:28.786: E/AndroidRuntime(955): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 03-17 04:37:28.786: E/AndroidRuntime(955): ... 11 more 03-17 04:37:28.792: W/ActivityManager(60): Force finishing activity com.example.easyandroidtutgridviewwithtext/.WelcomeActivity 03-17 04:37:29.313: W/ActivityManager(60): Activity pause timeout for HistoryRecord{40645288 com.example.easyandroidtutgridviewwithtext/.WelcomeActivity}\[/code\]
 
Back
Top