I have recently gotten back to developing the app i was working on and encountered a problem when trying to run it. The app has worked fine when run before so I am a little puzzled about this.
Log cat:
07-30 12:07:53.119: E/AndroidRuntime(8070): FATAL EXCEPTION: main
07-30 12:07:53.119: E/AndroidRuntime(8070): java.lang.RuntimeException: Unable to start activity ComponentInfo{simcas.fartberegneren/simcas.fartberegneren.MainActivity}: java.lang.NullPointerException
07-30 12:07:53.119: E/AndroidRuntime(8070): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
07-30 12:07:53.119: E/AndroidRuntime(8070): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
07-30 12:07:53.119: E/AndroidRuntime(8070): at android.app.ActivityThread.access$700(ActivityThread.java:159)
07-30 12:07:53.119: E/AndroidRuntime(8070): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
07-30 12:07:53.119: E/AndroidRuntime(8070): at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 12:07:53.119: E/AndroidRuntime(8070): at android.os.Looper.loop(Looper.java:137)
07-30 12:07:53.119: E/AndroidRuntime(8070): at android.app.ActivityThread.main(ActivityThread.java:5419)
07-30 12:07:53.119: E/AndroidRuntime(8070): at java.lang.reflect.Method.invokeNative(Native Method)
07-30 12:07:53.119: E/AndroidRuntime(8070): at java.lang.reflect.Method.invoke(Method.java:525)
07-30 12:07:53.119: E/AndroidRuntime(8070): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
07-30 12:07:53.119: E/AndroidRuntime(8070): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
07-30 12:07:53.119: E/AndroidRuntime(8070): at dalvik.system.NativeStart.main(Native Method)
07-30 12:07:53.119: E/AndroidRuntime(8070): Caused by: java.lang.NullPointerException
07-30 12:07:53.119: E/AndroidRuntime(8070): at simcas.fartberegneren.MyAdapter.<init>(MyAdapter.java:38)
07-30 12:07:53.119: E/AndroidRuntime(8070): at simcas.fartberegneren.MainActivity.onCreate(MainActivity.java:32)
07-30 12:07:53.119: E/AndroidRuntime(8070): at android.app.Activity.performCreate(Activity.java:5372)
07-30 12:07:53.119: E/AndroidRuntime(8070): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
07-30 12:07:53.119: E/AndroidRuntime(8070): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
07-30 12:07:53.119: E/AndroidRuntime(8070): ... 11 more
view_pager.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/view_pager" />
</LinearLayout>
MyAdapter.java:
public class MyAdapter extends FragmentStatePagerAdapter implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
private final Context mContext;
private final ActionBar mActionBar;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
static final class TabInfo {
private final Class<?> clss;
private final Bundle args;
TabInfo(Class<?> _class, Bundle _args) {
clss = _class;
args = _args;
}
}
public MyAdapter(FragmentActivity activity, ViewPager pager) {
super(activity.getSupportFragmentManager());
mContext = activity;
mActionBar = activity.getActionBar();
mViewPager = pager;
mViewPager.setAdapter(this); // This is line 38 causing the nullpointer
mViewPager.setOnPageChangeListener(this);
MainActivity.java:
public class MainActivity extends FragmentActivity implements TabListener {
static Context context;
private MyAdapter myAdapter;
private ViewPager myViewPager;
private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_pager);
myViewPager = new ViewPager(this);
myViewPager = (ViewPager) findViewById(R.id.view_pager);
final ActionBar actionbar = getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
myAdapter = new MyAdapter(this, myViewPager); // This is line 32
myAdapter.addTab(actionbar.newTab().setText(R.string.menu_speed), SpeedZonesFragment.class, null);
myAdapter.addTab(actionbar.newTab().setText(R.string.menu_dist), DistanceFragment.class, null);
myAdapter.addTab(actionbar.newTab().setText(R.string.menu_fuel), FuelConsumptionFragment.class, null);
What am I missing here?
It should be noted that the code havent changed since i successfully ran it on my device half a year ago.
EDIT:
It seems, that even though I add some code, the nullpointer is still from the same two lines, despite the line has changed! What is going on?
The constructor public MyAdapter(FragmentActivity activity, ViewPager pager) is being called with a null pager.
Make a break point here:
myViewPager = (ViewPager) findViewById(R.id.view_pager);
Check if it is returning your pager object and check the xml view_pager.xml for the pager ID make sure it is view_pager.
EDIT: I saw your posted XML, please make sure of it's name view_pager.xml because you didn't post that.
try this:- Put try catch block where you are setting the Adapter. And If possible take a static variable in Main Activity.
public static int selectedTab;
and in the
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
selectedTab = tab.getPosition();
Log.d("Selected Tab is", ">" + selectedTab);
}
and the place where you are setting adapter, put this type of code. In my case I have 7 tabs and each has view pager. So on the 4th tab I put this code which ignore the Null pointer exception. Actually It happens because, if on the first tab the data is not come, and we move on the 4th or 5th tab and after that if data will come on first tab. Then we get the NullPointer Exception. Sorry for my english, I hope you will understand.
if (MainActivity.selectedTab == 0 || MainActivity.selectedTab == 1|| MainActivity.selectedTab == 2
|| MainActivity.selectedTab == 6) {
} else {
try {
adapter = new GalleryAdapter(getActivity(),
R.layout.row_gallery, mGridArray);
mGridView.setAdapter(adapter);
} catch (NullPointerException e) {
e.printStackTrace();
}
}
Related
I am trying to open a fragment from FragmentActivity. I have implmented NavigationDrawer. From Main Activity I opened FragmentActivity (LINK_New.java) and from there I want to open a Fragment (LINK_Create.java).
The main Navigation DrawList has this:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="#+id/left_drawer"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/list_background"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/drawer_list_selector" />
</android.support.v4.widget.DrawerLayout>
FRAME_CONTAINER IS ONLY AVAILABLE IN THE ABOVE XML.
XML for LINK_New & LINK_Create: it is just Linear Layout with TextBox and Image stuff in it.
I am trying this:
fragment = new LINK_Create();
FragmentManager fragmentManager2 = getSupportFragmentManager();
fragmentManager2.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
For which I am getting below error:
04-07 13:36:19.142: E/AndroidRuntime(20620): FATAL EXCEPTION: main
04-07 13:36:19.142: E/AndroidRuntime(20620): Process: com.ylg.LINK, PID: 20620
04-07 13:36:19.142: E/AndroidRuntime(20620): java.lang.IllegalArgumentException: No view found for id 0x7f0b006b (com.ylg.LINK:id/frame_container) for fragment LINK_Create{3093cc8e #0 id=0x7f0b006b}
04-07 13:36:19.142: E/AndroidRuntime(20620): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:934)
04-07 13:36:19.142: E/AndroidRuntime(20620): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
04-07 13:36:19.142: E/AndroidRuntime(20620): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-07 13:36:19.142: E/AndroidRuntime(20620): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
04-07 13:36:19.142: E/AndroidRuntime(20620): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:450)
04-07 13:36:19.142: E/AndroidRuntime(20620): at android.os.Handler.handleCallback(Handler.java:739)
04-07 13:36:19.142: E/AndroidRuntime(20620): at android.os.Handler.dispatchMessage(Handler.java:95)
04-07 13:36:19.142: E/AndroidRuntime(20620): at android.os.Looper.loop(Looper.java:135)
04-07 13:36:19.142: E/AndroidRuntime(20620): at android.app.ActivityThread.main(ActivityThread.java:5254)
04-07 13:36:19.142: E/AndroidRuntime(20620): at java.lang.reflect.Method.invoke(Native Method)
04-07 13:36:19.142: E/AndroidRuntime(20620): at java.lang.reflect.Method.invoke(Method.java:372)
04-07 13:36:19.142: E/AndroidRuntime(20620): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
04-07 13:36:19.142: E/AndroidRuntime(20620): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
My LINK_New FRAGMENTACTIVITY has the following:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(actionBar.getDisplayOptions() | ActionBar.DISPLAY_SHOW_CUSTOM);
ImageView imageView = new ImageView(actionBar.getThemedContext());
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setImageResource(R.drawable.logoactionbar);
ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT, Gravity.END | Gravity.CENTER_VERTICAL);
layoutParams.rightMargin = 20;
imageView.setLayoutParams(layoutParams);
actionBar.setCustomView(imageView);
actionBar.setTitle(getResources().getString(R.string.account_anew));
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setIcon(android.R.color.transparent);
getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.backgroundcolor)));
setContentView(R.layout.link_new)
}
On BackPressed inside LINK_New.java I am calling the fragment:
#Override
public void onBackPressed()
{
super.onBackPressed();
fragment = new LINK_Create();
FragmentManager fragmentManager2 = getSupportFragmentManager();
fragmentManager2.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
}
I tried all the solutions that are available on SO I am not able to figure out a solution for the above issue.
Please help!
Thanks!
You are using the fragment's fragment manager, instead of the Activity's. So, use the Activity's instead.
FragmentActivity:
public void replaceWith(Fragment fragment) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.frame_container, fragment)
.commit();
}
Fragment:
#Override
public void onBackPressed()
{
((YourFragmentActivity)getActivity()).replaceWith(new LINK_Create());
}
This is the quick solution to the problem, anyways.
Normally I'd use Otto and send an event
FragmentActivity:
#Subscribe
public void onLinkFragmentBackPressed(LINK_New.BackPressedEvent e) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.frame_container, new LINK_Create())
.commit();
}
Fragment:
#Override
public void onBackPressed() {
SingletonBus.INSTANCE.post(new BackPressedEvent());
}
I have two framelayout in my main.xml file. I add framelayouts to the class that extends Fragment. my main class extends FragmentActivity and this is Oncreate method of it:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FragmentManager fm =getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
Fragment f=new Freg1();
Fragment f2=new Freg1();
ft.add(R.id.frame1, f);
ft.add(R.id.frame2, f2);
ft.commit();
tf=Typeface.createFromAsset(this.getAssets(),"font/Byekan.ttf" );
tv1=(TextView) findViewById(R.id.textView1);
tv1.setTypeface(tf);
Log.i(TAG,"1");
lv1=(ListView) findViewById(R.id.listView1);
lv2=(ListView) findViewById(R.id.listView2);
Log.i(TAG,"2");
List<String> stringList = new ArrayList<String>(Arrays.asList(s1));
Log.i(TAG,"3");
ListAdapter listAdapter = new CustomListAdapter(MainActivity.this , R.layout.custom_list ,stringList);
Log.i(TAG,"4");
lv1.setAdapter(listAdapter);
Log.i(TAG,"5");
lv2.setAdapter(listAdapter);
Log.i(TAG,"6");
}
when i run the codes, it crashed after LOG no4. that mean setAdapter() method do not work. how can i resolve this problem?
this is my logcat resource:
Shutting down VM
threadid=1: thread exiting with uncaught exception (group=0x40a13300)
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.taxitabriz/com.example.taxitabriz.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.taxitabriz.MainActivity.onCreate(MainActivity.java:55)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
... 11 more
thank for you that help me to resolve problem.
Your ListView lv1 is null as you can see here:
java.lang.RuntimeException: Unable to start activity ComponentInfo{...}: java.lang.NullPointerException
....
com.example.taxitabriz.MainActivity.onCreate(MainActivity.java:55)
The line 55 of MainActivity should be this call: lv1.setAdapter(listAdapter);
Make sure that your listView1 is included within the layout and initialized correctly prior to trying to set an Adapter to it.
so Im trying to make an app that when you click on a button, it displays a lot of images. The problem is that when I click the button, the application crashes.
Here is a part of my XML file. It has 36 different images:
<ImageView
android:id="#+id/imageView31"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/veh1" />
<ImageView
android:id="#+id/imageView32"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/veh2" />
<ImageView
android:id="#+id/imageView33"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/veh3" />
...
And here is the LogCat:
02-18 22:47:00.263: E/AndroidRuntime(366): FATAL EXCEPTION: main
02-18 22:47:00.263: E/AndroidRuntime(366): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.guzi.samphelptools/com.guzi.samphelptools.Vehicles}: android.view.InflateException: Binary XML file line #81: Error inflating class <unknown>
02-18 22:47:00.263: E/AndroidRuntime(366): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.os.Looper.loop(Looper.java:130)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-18 22:47:00.263: E/AndroidRuntime(366): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 22:47:00.263: E/AndroidRuntime(366): at java.lang.reflect.Method.invoke(Method.java:507)
02-18 22:47:00.263: E/AndroidRuntime(366): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-18 22:47:00.263: E/AndroidRuntime(366): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-18 22:47:00.263: E/AndroidRuntime(366): at dalvik.system.NativeStart.main(Native Method)
02-18 22:47:00.263: E/AndroidRuntime(366): Caused by: android.view.InflateException: Binary XML file line #81: Error inflating class <unknown>
02-18 22:47:00.263: E/AndroidRuntime(366): at android.view.LayoutInflater.createView(LayoutInflater.java:518)
02-18 22:47:00.263: E/AndroidRuntime(366): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
02-18 22:47:00.263: E/AndroidRuntime(366): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.app.Activity.setContentView(Activity.java:1657)
02-18 22:47:00.263: E/AndroidRuntime(366): at com.guzi.samphelptools.Vehicles.onCreate(Vehicles.java:14)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-18 22:47:00.263: E/AndroidRuntime(366): ... 11 more
02-18 22:47:00.263: E/AndroidRuntime(366): Caused by: java.lang.reflect.InvocationTargetException
02-18 22:47:00.263: E/AndroidRuntime(366): at java.lang.reflect.Constructor.constructNative(Native Method)
02-18 22:47:00.263: E/AndroidRuntime(366): at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.view.LayoutInflater.createView(LayoutInflater.java:505)
02-18 22:47:00.263: E/AndroidRuntime(366): ... 24 more
02-18 22:47:00.263: E/AndroidRuntime(366): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
02-18 22:47:00.263: E/AndroidRuntime(366): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.content.res.Res`enter code here`ources.loadDrawable(Resources.java:1709)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.widget.ImageView.<init>(ImageView.java:118)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.widget.ImageView.<init>(ImageView.java:108)
02-18 22:47:00.263: E/AndroidRuntime(366): ... 27 more
Okay, so I found this simple listview thing, but i cant get it running.
import android.os.Bundle;
import android.app.Activity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
// Array of integers points to images stored in /res/drawable-hdpi/
int[] vehs = new int[]{
R.drawable.veh1,
R.drawable.veh2,
R.drawable.veh3,
R.drawable.veh4,
R.drawable.veh5,
R.drawable.veh6,
R.drawable.veh7,
R.drawable.veh8,
R.drawable.veh9,
R.drawable.veh10,
R.drawable.veh11,
R.drawable.veh12,
R.drawable.veh13,
R.drawable.veh14,
R.drawable.veh15,
R.drawable.veh16,
R.drawable.veh17,
R.drawable.veh18,
R.drawable.veh19,
R.drawable.veh20,
R.drawable.veh21,
R.drawable.veh22,
R.drawable.veh23,
R.drawable.veh24,
R.drawable.veh25,
R.drawable.veh26,
R.drawable.veh27,
R.drawable.veh28,
R.drawable.veh29,
R.drawable.veh30,
R.drawable.veh31,
R.drawable.veh32,
R.drawable.veh33,
R.drawable.veh34,
R.drawable.veh35,
R.drawable.veh36,
};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Each row in the list stores country name, currency and flag
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<10;i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("", Integer.toString(vehs[i]) );
aList.add(hm);
}
// Keys used in Hashmap
String[] from = { "flag","txt","cur" };
// Ids of views in listview_layout
int[] to = { R.id.vehs};
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.listview_layout, from, to);
// Getting a reference to listview of main.xml layout file
ListView listView = ( ListView ) findViewById(R.id.listview);
// Setting the adapter to the listView
listView.setAdapter(adapter);
}
}
If you could help me with this, that would be great! :D
The problem you have here is you are loading everything at the same time, I guess. You should try using a ListView and inflate each row! The way this would work is that your app will load an ImageView or two at once and the rest will be left on the side, until the user scrolls close to it. The tutorial I found for this is (watch #71 to about #87 - you can get all his videos at slidenerd.com):
http://www.youtube.com/watch?v=uic3TVp_j3M#t=0
You should also resize your bitmaps so they aren't loaded in full resolution. You don't need a 1920x1080 image for a 540*420 screen, for example. The following link shows you how to take care of this.
http://developer.android.com/training/displaying-bitmaps/index.html
So, in the end, you will have only 1 ListView, 1 ImageView and way less code and way less confusion :P
EDIT: QUICK GUIDE:
I'll walk you through the ListView method only, because I think the scaling down of an image example by Google is easy to follow.
So, first of all, you will have the following:
myActivity.xml (the main Activity where you need these images)
Single row (the layout of what a single row looks like)
MyActivity.java (The java class for your activity)
myActivity.xml will contain a ListView:
<ListView
android:id="#+id/lvList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
singleRow.xml will contain just an ImageView - you will use the same ImageView's LayoutParamaters and mirror them 36 times:
<ImageView
android:id="#+id/ivImage"
android:layout_width="50dp"
android:layout_height="85dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_launcher" />
MyActivity.java - where all the magic happens!
public class MyActivity extends Activity implements OnItemClickListener {
ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
lv = (ListView) findViewById(R.id.lvList);
MyAdapter adapter = new MyAdapter(this);
lv.setAdapter(adapter);
lv.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
MyView holder = (MyView) view.getTag(); // This will get the tag of the item click - that is set up below
}
//Create a holder for the image that will be in an ImageView
class SingleRowData {
int img;
SingleRowData(int img) {
this.img = img;
}
}
//A reference to the ImageView that needs to be copied
private class MyView {
ImageView ivImg;
MyView(View v) {
ivImg = (ImageView) v.findViewById(R.id.ivHomePageRow);
}
}
class MyAdapter extends BaseAdapter{
ArrayList<SingleHPSRow> list;
Context context;
public MyAdapter(Context context) {
this.context = context;
list = new ArrayList<SingleRowData>();
...
int[] imgId = ...; // let's say you are getting all the images from your resources .. you will have to set this up, I'm guessing you know how to.
for(int item = 0; item < imgUrl.length; i++) {
list.add(imgId[item]);
}
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
view row = convertView; //This will try to see if the row has already been loaded b4
MyView holder = null; initialize our custome View for the row
if(row == null) { // new row
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); //Get the LayoutInflater
row = inflater.inflate(R.layout.singleRow, parent, false); //Reference to the row layout
holder = new MyView(row);
row.setTag(holder); // Give the row a tag - the holder that contains the item
} else { // old row
holder = (MyView) row.getTag(); //Get the tag that was assigned
}
SingleRowData item = list.get(position); //Get the item at the current position
int image = item.img; //Get the imgId assigned to the current item
holder.ivImg.setImageResource(image); //Load image for the current position
return row; // Return the current row
}
}
}
Hopefull this is well explained, I copied and shortened one of my codes. Let me know if there's something you don't understand
I know a bunch of people have asked this same thing but I just don't know what's going on. I'm trying to make a calculator in Eclipse, but I keep getting a list of errors.
There are no errors in the file that the program notices, although there is an error in the layout.xml but it hasn't caused a problem before so that shouldn't cause a problem.
07-30 08:19:50.470: D/AndroidRuntime(2071): Shutting down VM
07-30 08:19:50.470: W/dalvikvm(2071): threadid=1: thread exiting with uncaught
exception (group=0x40a421f8)
07-30 08:19:50.480: E/AndroidRuntime(2071): FATAL EXCEPTION: main
07-30 08:19:50.480: E/AndroidRuntime(2071): java.lang.RuntimeException: Unable to start
activity
ComponentInfo{com.example.se.miun.chris.calculator/com.example.se.miun.chris.
calculator.MainActivity}: java.lang.NullPointerException
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.app.ActivityThread.access$600(ActivityThread.java:123)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.os.Handler.dispatchMessage(Handler.java:99)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.os.Looper.loop(Looper.java:137)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.app.ActivityThread.main(ActivityThread.java:4424)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
java.lang.reflect.Method.invokeNative(Native Method)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
java.lang.reflect.Method.invoke(Method.java:511)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
dalvik.system.NativeStart.main(Native Method)
07-30 08:19:50.480: E/AndroidRuntime(2071): Caused by: java.lang.NullPointerException
07-30 08:19:50.480: E/AndroidRuntime(2071): at
com.example.se.miun.chris.calculator.MainActivity.onCreate(MainActivity.java:60)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.app.Activity.performCreate(Activity.java:4465)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-30 08:19:50.480: E/AndroidRuntime(2071): ... 11 more
This is my coding. It doesn't really do anything yet, but I wanted to just run it to see if it encountered any errors. This is the mainActivity.java file.
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
Button Seven;
Button Eight;
Button Nine;
Button Four;
Button Five;
Button Six;
Button One;
Button Two;
Button Three;
Button Zero;
Button Point;
Button Negative;
TextView TextBox;
int x;
int y;
String z;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Seven = (Button)findViewById(R.id.NumberSeven);
Seven.setOnClickListener(this);
Eight = (Button)findViewById(R.id.NumberEight);
Eight.setOnClickListener(this);
Nine = (Button)findViewById(R.id.NumberNine);
Nine.setOnClickListener(this);
Four = (Button)findViewById(R.id.NumberFour);
Four.setOnClickListener(this);
Five = (Button)findViewById(R.id.NumberFive);
Five.setOnClickListener(this);
Six = (Button)findViewById(R.id.NumberSix);
Six.setOnClickListener(this);
One = (Button)findViewById(R.id.NumberOne);
One.setOnClickListener(this);
Two = (Button)findViewById(R.id.NumberTwo);
Two.setOnClickListener(this);
Three = (Button)findViewById(R.id.NumberThree);
Three.setOnClickListener(this);
Zero = (Button)findViewById(R.id.NumberZero);
Zero.setOnClickListener(this);
Point = (Button)findViewById(R.id.Point);
Point.setOnClickListener(this);
Negative = (Button)findViewById(R.id.NNegative);
Negative.setOnClickListener(this);
TextBox = (TextView)findViewById(R.id.Screen);
x = (Integer) null;
y = (Integer) null;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void onClick(View One) {
if(z == null){
x = 1;
TextBox.setText(x);
TextBox.setText("diggity");
}
else if(z != null) {
y = 1;
TextBox.setText(x);
TextBox.setText(z);
TextBox.setText(y);
}
}
}
line 60: x = (Integer) null;
This line will compile to this bytecode (disassembled by javap):
aconst_null
checkcast #2; //class java/lang/Integer
invokevirtual #3; //Method java/lang/Integer.intValue:()I
Third line will cause a NullPointerException becouse Integer object is actually your null constant :)
Primitive data types (int, long etc.) is the only non-object types in Java. null is used to show that the current variable (Object variable) is not backed by the actual object (no memory was allocated). For primitive types memory allocates immediately so they cant have this null state.
So you should check for "if(x == 0)" or define it as Integer.
P.S. And don't cast null to anything :)
It's like this
you're not getting much errors because the application can't launch. it cannot launch because it's onCreate() cannot finish.
onCreate() cannot finish because of a nullPointerException.
you cast null into integer twice, instead of simply instantiating a new integer which will default to 0. once you get rid of that, it should work.
see?
E/AndroidRuntime(2071): Caused by: java.lang.NullPointerException
E/AndroidRuntime(2071): at
MainActivity.onCreate(MainActivity.java:60)
and i bet that this is line 60
x = (Integer) null;
so change it to
x = new Integer();
So whenever I click the button on the startup page, it gives me a force close error. Here's the class for the main.xml layout file:
public class ForeverAloneActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnstrt = (Button) findViewById(R.id.toq1);
btnstrt.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Intent frstq = new Intent(v.getContext(), QuestionOne.class);
startActivityForResult(frstq, 0);
And this is what I believe is producing the error. This class is related to the page that that when pressing the button on the startup page, you are taken to:
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView (R.layout.frstq);
Button startQ2 = (Button) findViewById(R.id.toq2);
startQ2.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Intent toQ2 = new Intent(v.getContext(), QuestionTwo.class);
final EditText number = (EditText) findViewById(R.id.editText1);
final Toast error = Toast.makeText(QuestionOne.this, "Please insert a value", Toast.LENGTH_SHORT);
if (number.getText().toString().equals("")) {error.show();
}else{
startActivityForResult(toQ2, 0);}
That if statement is there as on the next page, there is an EditText box. I tried to make it so that if there is nothing in the EditText box, it displays a toast message saying "Please insert a value". Until an integer is put into the EditText box, then the button will not work.
If someone can help, it will be much appreciated.
Logcat:
04-07 19:33:58.199: W/dalvikvm(362): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-07 19:33:58.221: E/AndroidRuntime(362): FATAL EXCEPTION: main
04-07 19:33:58.221: E/AndroidRuntime(362): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.kenning.foreveralone/com.kenning.foreveralone.QuestionOne}; have you declared this activity in your AndroidManifest.xml?
04-07 19:33:58.221: E/AndroidRuntime(362): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
04-07 19:33:58.221: E/AndroidRuntime(362): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
04-07 19:33:58.221: E/AndroidRuntime(362): at android.app.Activity.startActivityForResult(Activity.java:2817)
04-07 19:33:58.221: E/AndroidRuntime(362): at com.kenning.foreveralone.ForeverAloneActivity$1.onClick(ForeverAloneActivity.java:22)
04-07 19:33:58.221: E/AndroidRuntime(362): at android.view.View.performClick(View.java:2408)
04-07 19:33:58.221: E/AndroidRuntime(362): at android.view.View$PerformClick.run(View.java:8816)
04-07 19:33:58.221: E/AndroidRuntime(362): at android.os.Handler.handleCallback(Handler.java:587)
04-07 19:33:58.221: E/AndroidRuntime(362): at android.os.Handler.dispatchMessage(Handler.java:92)
04-07 19:33:58.221: E/AndroidRuntime(362): at android.os.Looper.loop(Looper.java:123)
04-07 19:33:58.221: E/AndroidRuntime(362): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-07 19:33:58.221: E/AndroidRuntime(362): at java.lang.reflect.Method.invokeNative(Native Method)
04-07 19:33:58.221: E/AndroidRuntime(362): at java.lang.reflect.Method.invoke(Method.java:521)
04-07 19:33:58.221: E/AndroidRuntime(362): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-07 19:33:58.221: E/AndroidRuntime(362): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-07 19:33:58.221: E/AndroidRuntime(362): at dalvik.system.NativeStart.main(Native Method)
are you adding QuestionTwo in manifiest file ?
check your error - have you declared this activity in your AndroidManifest.xml?
Declare your activity in manifeast file. It seems you forgetted that
<activity android:name="QuestionTwo" />
also dont forget to include every activity in your manifeast file
Hey have you added the QuestionTwo class as an activity in menifest file. If after adding the
Intent toQ2 = new Intent(YourCurrentActivity.this,QuestionTwo.class);
then you must have not added the activity in menifest.B'coz it clearly shows in the log that Activity is not found. You have to declare the activity in menifest file.
Hope you'll get run your app.