Android Java, getString from the Main Activity to a Fragment Issue - java

Hi could someone advise me on whats wrong with the following code. The logcat says its in the onCreate method. all seems to be correct in the mainactivity as i have passed the bundle with the string.
#SuppressLint("NewApi")
public class CurrentBookingFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.current_booking_fragment, container, false);
Bundle bundle = this.getArguments();
String strtext = this.getArguments().getString("GetStringPUFirstLine");
TextView CurrentBookingFragmentTV = (TextView)view.findViewById(R.id.CurrentBookingFragmentTV);
CurrentBookingFragmentTV.setText(strtext);
return view;
}
}
Here is the log:
03-30 21:59:30.385: E/AndroidRuntime(30843): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
03-30 21:59:30.385: E/AndroidRuntime(30843): at dalvik.system.NativeStart.main(Native Method)
03-30 21:59:30.385: E/AndroidRuntime(30843): Caused by: android.view.InflateException: Binary XML file line #45: Error inflating class fragment
03-30 21:59:30.385: E/AndroidRuntime(30843): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
03-30 21:59:30.385: E/AndroidRuntime(30843): at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
03-30 21:59:30.385: E/AndroidRuntime(30843): at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
03-30 21:59:30.385: E/AndroidRuntime(30843): at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
03-30 21:59:30.385: E/AndroidRuntime(30843): at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
03-30 21:59:30.385: E/AndroidRuntime(30843): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:361)
03-30 21:59:30.385: E/AndroidRuntime(30843): at android.app.Activity.setContentView(Activity.java:1956)
03-30 21:59:30.385: E/AndroidRuntime(30843): at com.example.prototype.MainActivity.onCreate(MainActivity.java:88)
03-30 21:59:30.385: E/AndroidRuntime(30843): at android.app.Activity.performCreate(Activity.java:5372)
03-30 21:59:30.385: E/AndroidRuntime(30843): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
03-30 21:59:30.385: E/AndroidRuntime(30843): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
03-30 21:59:30.385: E/AndroidRuntime(30843): ... 11 more
03-30 21:59:30.385: E/AndroidRuntime(30843): Caused by: java.lang.NullPointerException
03-30 21:59:30.385: E/AndroidRuntime(30843): at com.example.prototype.CurrentBookingFragment.onCreateView(CurrentBookingFragment.java:49)
03-30 21:59:30.385: E/AndroidRuntime(30843): at android.app.Fragment.performCreateView(Fragment.java:1699)
03-30 21:59:30.385: E/AndroidRuntime(30843): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:879)
03-30 21:59:30.385: E/AndroidRuntime(30843): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1053)
03-30 21:59:30.385: E/AndroidRuntime(30843): at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1155)
03-30 21:59:30.385: E/AndroidRuntime(30843): at android.app.Activity.onCreateView(Activity.java:4966)
03-30 21:59:30.385: E/AndroidRuntime(30843): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:695)
03-30 21:59:30.385: E/AndroidRuntime(30843): ... 21 more
Creating the fragment:
Fragment CurrentBookingFragment;
CurrentBookingFragment = new CurrentBookingFragment();
FragmentManager fm = getFragmentManager();
Bundle bundle=new Bundle();
bundle.putString("GetStringPUFirstLine", "GetStringPUFirstLine");
CurrentBookingFragment.setArguments(bundle);
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.BookingFragment, CurrentBookingFragment);
fragmentTransaction.commit();

Try setting the text in onActivityCreated(){}.
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
TextView CurrentBookingFragmentTV = (TextView)view.findViewById(R.id.CurrentBookingFragmentTV);
CurrentBookingFragmentTV.setText(strtext);
}

Related

Getting a wierd error i dont understand when creating a button

everything is mainly explained in the title ill go in as much detail as i can. I am making an android game app i created a unique id for a circle that is drawn in canvas in the DrawingView class since this cannot be referenced in xml i had to create the id as an int in java so that java could find the id and use it as the button. Im sure you are all very confused by now i will continue will putting in my best effort to explain. below is the code for the Main file that gets called
public class Main extends Activity {
DrawingView v;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
LinearLayout layout1 = new LinearLayout (this);
FrameLayout game = new FrameLayout(this);
DrawingView v = new DrawingView (this);
TextView myText = new TextView(this);
int w = getResources().getInteger(DrawingView.redColor);
Button redCircle = (Button) findViewById(w);
redCircle.setWidth(300);
redCircle.setText("Start Game");
layout1.addView(myText);
layout1.addView(redCircle);
//redCircle.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
game.addView(v);
game.addView(layout1);
setContentView(game);
redCircle.setOnClickListener((OnClickListener) this);
}
public void onClick(View v) {
Intent intent = new Intent(this, Main.class);
startActivity(intent);
// re-starts this activity from game-view. add this.finish(); to remove from stack
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
and not really neccessary but to help you understand what i am trying to accomplish here is the DrawingView class that creates random circles either green or red below.
public class DrawingView extends View {
public DrawingView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
RectF rectf = new RectF(0, 0, 200, 0);
private static final int w = 100;
public static int lastColor = Color.BLACK;
private final Random random = new Random();
private final Paint paint = new Paint();
private final int radius = 230;
private final Handler handler = new Handler();
public static int redColor = Color.RED;
public static int greenColor = Color.GREEN;
private final Runnable updateCircle = new Runnable() {
#Override
public void run() {
lastColor = random.nextInt(2) == 1 ? redColor : greenColor;
paint.setColor(lastColor);
invalidate();
handler.postDelayed(this, 1000);
}
};
#Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
handler.post(updateCircle);
}
#Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
handler.removeCallbacks(updateCircle);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// your other stuff here
canvas.drawCircle(random.nextInt(canvas.getWidth()-radius/2) + radius/2f, random.nextInt(canvas.getHeight()-radius/2) + radius/2f, radius, paint);
}
//Bitmap.Config conf = Bitmap.Config.ARGB_8888; // see other conf types
//private int h = 100;
//#SuppressLint("DrawAllocation")
//Bitmap bmp = Bitmap.createBitmap(w, h, conf); // this creates a MUTABLE bitmap
//Canvas canvas = new Canvas(bmp);
/*
private Object canvas(Bitmap bmp) {
// TODO Auto-generated method stub
return null;
}
*/
}
and more importantly the log cat errors that display after the app crashes
03-30 02:27:27.903: E/AndroidRuntime(3104): FATAL EXCEPTION: main
03-30 02:27:27.903: E/AndroidRuntime(3104): Process: com.Tripps.thesimplegame, PID: 3104
03-30 02:27:27.903: E/AndroidRuntime(3104): java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
03-30 02:27:27.903: E/AndroidRuntime(3104): at com.Tripps.thesimplegame.DrawingView.onDraw(DrawingView.java:67)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.view.View.draw(View.java:15114)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.view.View.updateDisplayListIfDirty(View.java:14048)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.view.View.getDisplayList(View.java:14071)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.view.View.draw(View.java:14838)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.view.ViewGroup.drawChild(ViewGroup.java:3404)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.view.View.updateDisplayListIfDirty(View.java:14043)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.view.View.getDisplayList(View.java:14071)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.view.View.draw(View.java:14838)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.view.ViewGroup.drawChild(ViewGroup.java:3404)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.view.View.updateDisplayListIfDirty(View.java:14043)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.view.View.getDisplayList(View.java:14071)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.view.View.draw(View.java:14838)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.view.ViewGroup.drawChild(ViewGroup.java:3404)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.view.View.updateDisplayListIfDirty(View.java:14043)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.view.View.getDisplayList(View.java:14071)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.view.View.draw(View.java:14838)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.view.ViewGroup.drawChild(ViewGroup.java:3404)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.view.View.draw(View.java:15117)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.widget.FrameLayout.draw(FrameLayout.java:592)
03-30 02:27:27.903: E/AndroidRuntime(3104): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2595)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.view.View.updateDisplayListIfDirty(View.java:14048)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.view.View.getDisplayList(View.java:14071)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:266)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:272)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:311)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.view.ViewRootImpl.draw(ViewRootImpl.java:2492)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2337)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1968)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.view.Choreographer.doCallbacks(Choreographer.java:580)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.view.Choreographer.doFrame(Choreographer.java:550)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.os.Handler.handleCallback(Handler.java:739)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.os.Handler.dispatchMessage(Handler.java:95)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.os.Looper.loop(Looper.java:135)
03-30 02:27:27.903: E/AndroidRuntime(3104): at android.app.ActivityThread.main(ActivityThread.java:5221)
03-30 02:27:27.903: E/AndroidRuntime(3104): at java.lang.reflect.Method.invoke(Native Method)
03-30 02:27:27.903: E/AndroidRuntime(3104): at java.lang.reflect.Method.invoke(Method.java:372)
03-30 02:27:27.903: E/AndroidRuntime(3104): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
03-30 02:27:27.903: E/AndroidRuntime(3104): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
03-30 02:38:46.269: D/OpenGLRenderer(3167): Render dirty regions requested: true
03-30 02:38:46.276: D/(3167): HostConnection::get() New Host Connection established 0xae0add60, tid 3167
03-30 02:38:46.287: D/Atlas(3167): Validating map...
03-30 02:38:46.379: D/(3167): HostConnection::get() New Host Connection established 0xa6c52140, tid 3182
03-30 02:38:46.427: I/OpenGLRenderer(3167): Initialized EGL, version 1.4
03-30 02:38:46.473: D/OpenGLRenderer(3167): Enabling debug mode 0
03-30 02:38:46.550: W/EGL_emulation(3167): eglSurfaceAttrib not implemented
03-30 02:38:46.550: W/OpenGLRenderer(3167): Failed to set EGL_SWAP_BEHAVIOR on surface 0xae0ed4a0, error=EGL_SUCCESS
03-30 03:01:54.523: W/ResourceType(3228): No known package when getting value for resource number 0xffff0000
03-30 03:01:54.523: D/AndroidRuntime(3228): Shutting down VM
03-30 03:01:54.524: E/AndroidRuntime(3228): FATAL EXCEPTION: main
03-30 03:01:54.524: E/AndroidRuntime(3228): Process: com.Tripps.thesimplegame, PID: 3228
03-30 03:01:54.524: E/AndroidRuntime(3228): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Tripps.thesimplegame/com.Tripps.thesimplegame.Main}: android.content.res.Resources$NotFoundException: Resource ID #0xffff0000
03-30 03:01:54.524: E/AndroidRuntime(3228): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
03-30 03:01:54.524: E/AndroidRuntime(3228): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
03-30 03:01:54.524: E/AndroidRuntime(3228): at android.app.ActivityThread.access$800(ActivityThread.java:144)
03-30 03:01:54.524: E/AndroidRuntime(3228): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
03-30 03:01:54.524: E/AndroidRuntime(3228): at android.os.Handler.dispatchMessage(Handler.java:102)
03-30 03:01:54.524: E/AndroidRuntime(3228): at android.os.Looper.loop(Looper.java:135)
03-30 03:01:54.524: E/AndroidRuntime(3228): at android.app.ActivityThread.main(ActivityThread.java:5221)
03-30 03:01:54.524: E/AndroidRuntime(3228): at java.lang.reflect.Method.invoke(Native Method)
03-30 03:01:54.524: E/AndroidRuntime(3228): at java.lang.reflect.Method.invoke(Method.java:372)
03-30 03:01:54.524: E/AndroidRuntime(3228): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
03-30 03:01:54.524: E/AndroidRuntime(3228): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
03-30 03:01:54.524: E/AndroidRuntime(3228): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0xffff0000
03-30 03:01:54.524: E/AndroidRuntime(3228): at android.content.res.Resources.getValue(Resources.java:1233)
03-30 03:01:54.524: E/AndroidRuntime(3228): at android.content.res.Resources.getInteger(Resources.java:989)
03-30 03:01:54.524: E/AndroidRuntime(3228): at com.Tripps.thesimplegame.Main.onCreate(Main.java:34)
03-30 03:01:54.524: E/AndroidRuntime(3228): at android.app.Activity.performCreate(Activity.java:5933)
03-30 03:01:54.524: E/AndroidRuntime(3228): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
03-30 03:01:54.524: E/AndroidRuntime(3228): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
03-30 03:01:54.524: E/AndroidRuntime(3228): ... 10 more
03-30 03:03:04.088: W/ResourceType(3288): No known package when getting value for resource number 0xffff0000
03-30 03:03:04.089: D/AndroidRuntime(3288): Shutting down VM
03-30 03:03:04.089: E/AndroidRuntime(3288): FATAL EXCEPTION: main
03-30 03:03:04.089: E/AndroidRuntime(3288): Process: com.Tripps.thesimplegame, PID: 3288
03-30 03:03:04.089: E/AndroidRuntime(3288): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Tripps.thesimplegame/com.Tripps.thesimplegame.Main}: android.content.res.Resources$NotFoundException: Resource ID #0xffff0000
03-30 03:03:04.089: E/AndroidRuntime(3288): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
03-30 03:03:04.089: E/AndroidRuntime(3288): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
03-30 03:03:04.089: E/AndroidRuntime(3288): at android.app.ActivityThread.access$800(ActivityThread.java:144)
03-30 03:03:04.089: E/AndroidRuntime(3288): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
03-30 03:03:04.089: E/AndroidRuntime(3288): at android.os.Handler.dispatchMessage(Handler.java:102)
03-30 03:03:04.089: E/AndroidRuntime(3288): at android.os.Looper.loop(Looper.java:135)
03-30 03:03:04.089: E/AndroidRuntime(3288): at android.app.ActivityThread.main(ActivityThread.java:5221)
03-30 03:03:04.089: E/AndroidRuntime(3288): at java.lang.reflect.Method.invoke(Native Method)
03-30 03:03:04.089: E/AndroidRuntime(3288): at java.lang.reflect.Method.invoke(Method.java:372)
03-30 03:03:04.089: E/AndroidRuntime(3288): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
03-30 03:03:04.089: E/AndroidRuntime(3288): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
03-30 03:03:04.089: E/AndroidRuntime(3288): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0xffff0000
03-30 03:03:04.089: E/AndroidRuntime(3288): at android.content.res.Resources.getValue(Resources.java:1233)
03-30 03:03:04.089: E/AndroidRuntime(3288): at android.content.res.Resources.getInteger(Resources.java:989)
03-30 03:03:04.089: E/AndroidRuntime(3288): at com.Tripps.thesimplegame.Main.onCreate(Main.java:34)
03-30 03:03:04.089: E/AndroidRuntime(3288): at android.app.Activity.performCreate(Activity.java:5933)
03-30 03:03:04.089: E/AndroidRuntime(3288): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
03-30 03:03:04.089: E/AndroidRuntime(3288): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
03-30 03:03:04.089: E/AndroidRuntime(3288): ... 10 more
03-30 03:03:34.502: I/Process(3288): Sending signal. PID: 3288 SIG: 9
You are getting error at :
canvas.drawCircle(random.nextInt(canvas.getWidth()-radius/2) + radius/2f, random.nextInt(canvas.getHeight()-radius/2) + radius/2f, radius, paint);
Because the Object canvas do not have width or height (it is null)
Reason :
Your code adds views like :
LinearLayout layout1 = new LinearLayout (this);
FrameLayout game = new FrameLayout(this);
DrawingView v = new DrawingView (this);
TextView myText = new TextView(this);
int w = getResources().getInteger(DrawingView.redColor);
Button redCircle = (Button) findViewById(w);
redCircle.setWidth(300);
redCircle.setText("Start Game");
layout1.addView(myText);
layout1.addView(redCircle);
//redCircle.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
game.addView(v);
game.addView(layout1);
But at the time of
game.addView(v);
v do not have any views referenced so the null object view is added in
game
So pls add some view i.e. Text or Images in v first and then call
game.addView(v);
This is solution for your error

using Bundle object to parse JSONobject Fragment to Fragment

i am using Bundle object to parse jsonobject one fragment to another fragment.but when running App it crash..can any one help me....
Fragment 1
after getting my jsonobject using Async task using button i open the my second Fragment
public void onClick(View v) {
Log.d(TAG,"More button clicked");
Fragment_account_details fragment = new Fragment_account_details();
Bundle bundle = new Bundle();
String accountDetails = name_value_list.toString();
bundle.putString("accountDetails", accountDetails);
fragment.setArguments(bundle);
getFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, fragment.newInstance(),
fragment.TAG).commit();
};
Fragment 2
public class Fragment_account_details extends Fragment {
TextView name, officePhone,fax,email;
public static Fragment_account_details newInstance() {
return new Fragment_account_details();
}
public final static String TAG = Fragment_account_details.class
.getSimpleName();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_account_details, container,false);
name = (TextView) rootView.findViewById(R.id.text_name);
fax = (TextView) rootView.findViewById(R.id.text_fax);
email = (TextView) rootView.findViewById(R.id.text_email);
String accountDetails = getArguments().getString("accountDetails");
return rootView;
}
}
Logcat
12-09 11:18:22.291: D/Fragment_Entry_Account(1486): More button clicked
12-09 11:18:22.307: D/AndroidRuntime(1486): Shutting down VM
12-09 11:18:22.307: W/dalvikvm(1486): threadid=1: thread exiting with uncaught exception (group=0xa4cacb20)
12-09 11:18:22.311: E/AndroidRuntime(1486): FATAL EXCEPTION: main
12-09 11:18:22.311: E/AndroidRuntime(1486): Process: com.wakensys.sugercrm_wakensys, PID: 1486
12-09 11:18:22.311: E/AndroidRuntime(1486): java.lang.NullPointerException
12-09 11:18:22.311: E/AndroidRuntime(1486): at com.wakensys.sugercrm_wakensys.fragments.Fragment_account_details.onCreateView(Fragment_account_details.java:41)
12-09 11:18:22.311: E/AndroidRuntime(1486): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
12-09 11:18:22.311: E/AndroidRuntime(1486): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
12-09 11:18:22.311: E/AndroidRuntime(1486): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
12-09 11:18:22.311: E/AndroidRuntime(1486): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
12-09 11:18:22.311: E/AndroidRuntime(1486): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
12-09 11:18:22.311: E/AndroidRuntime(1486): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
12-09 11:18:22.311: E/AndroidRuntime(1486): at android.os.Handler.handleCallback(Handler.java:733)
12-09 11:18:22.311: E/AndroidRuntime(1486): at android.os.Handler.dispatchMessage(Handler.java:95)
12-09 11:18:22.311: E/AndroidRuntime(1486): at android.os.Looper.loop(Looper.java:136)
12-09 11:18:22.311: E/AndroidRuntime(1486): at android.app.ActivityThread.main(ActivityThread.java:5001)
12-09 11:18:22.311: E/AndroidRuntime(1486): at java.lang.reflect.Method.invokeNative(Native Method)
12-09 11:18:22.311: E/AndroidRuntime(1486): at java.lang.reflect.Method.invoke(Method.java:515)
12-09 11:18:22.311: E/AndroidRuntime(1486): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
12-09 11:18:22.311: E/AndroidRuntime(1486): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
12-09 11:18:22.311: E/AndroidRuntime(1486): at dalvik.system.NativeStart.main(Native Method)

ListView Item OnItemClick crashing in ListView Adapter Class

I've been trying to get my list view to work for quite some time now, I just changed my listener from onClick to onItemClick to handle listview items better but the app crashes once again, my code below, any help would be appreciated.
Class:
public class ListViewAdapterProduct extends BaseAdapter {
Context mContext;
ListView listView = (ListView) findViewById(R.id.listview1);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id){
Resources res = mContext.getResources();
productTitleArray = res.getStringArray(R.array.titles_product);
for (String productTitle : productTitleArray)
{
if(productTitle.equals("productOne"))
{
Log.d("Msg", "Found");
}
else
{
Log.d("Msg", "Not Found");
}
}
}
});
My previous post:
Compare XML String From Java Class When Item In ListView Is Clicked
Logcat:
03-30 16:40:39.027: E/AndroidRuntime(811): FATAL EXCEPTION: main
03-30 16:40:39.027: E/AndroidRuntime(811): java.lang.NullPointerException
03-30 16:40:39.027: E/AndroidRuntime(811): at com.twostarii.test.ListViewAdapterproduct.getView(ListViewAdapterproduct.java:91)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.widget.AbsListView.obtainView(AbsListView.java:2159)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.widget.ListView.makeAndAddView(ListView.java:1831)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.widget.ListView.fillDown(ListView.java:674)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.widget.ListView.fillFromTop(ListView.java:735)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.widget.ListView.layoutChildren(ListView.java:1652)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.widget.AbsListView.onLayout(AbsListView.java:1994)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.view.View.layout(View.java:14008)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.view.ViewGroup.layout(ViewGroup.java:4373)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1021)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.view.View.layout(View.java:14008)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.view.ViewGroup.layout(ViewGroup.java:4373)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.view.View.layout(View.java:14008)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.view.ViewGroup.layout(ViewGroup.java:4373)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.view.View.layout(View.java:14008)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.view.ViewGroup.layout(ViewGroup.java:4373)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.view.View.layout(View.java:14008)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.view.ViewGroup.layout(ViewGroup.java:4373)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.view.Choreographer.doFrame(Choreographer.java:532)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.os.Handler.handleCallback(Handler.java:725)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.os.Handler.dispatchMessage(Handler.java:92)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.os.Looper.loop(Looper.java:137)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-30 16:40:39.027: E/AndroidRuntime(811): at java.lang.reflect.Method.invokeNative(Native Method)
03-30 16:40:39.027: E/AndroidRuntime(811): at java.lang.reflect.Method.invoke(Method.java:511)
03-30 16:40:39.027: E/AndroidRuntime(811): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-30 16:40:39.027: E/AndroidRuntime(811): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-30 16:40:39.027: E/AndroidRuntime(811): at dalvik.system.NativeStart.main(Native Method)
Activity:
public class MainActivityproduct extends MainActivity {
ListView list;
ListViewAdapterproduct adapter;
EditText editsearch;
String[] position;
String[] productTitles;
String[] productDescriptions;
int[] images={
R.drawable.list_product_one,
R.drawable.list_product_two,
R.drawable.list_product_three,
};
ArrayList<ClassproductList> arraylist = new ArrayList<ClassproductList>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
position = new String[] { "1", "2", "3"};
Resources res = getResources();
productTitles = res.getStringArray(R.array.titles_product);
productDescriptions = res.getStringArray(R.array.descriptions_product);
list = (ListView) findViewById(R.id.listview1);
list.setAdapter(adapter);
for (int i = 0; i < position.length; i++)
{
ClassproductList wp = new ClassproductList(productTitles[i], productDescriptions[i],images[i]);
arraylist.add(wp);
}
adapter = new ListViewAdapterproduct(this, arraylist);
list.setAdapter(adapter);
editsearch = (EditText) findViewById(R.id.search);
editsearch.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable arg0) {
String text = editsearch.getText().toString().toLowerCase(Locale.getDefault());
adapter.filter(text);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
}
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
You have declared the mContext, but you have not initialized it.
You can do this in the constructor of your adapter:
public class ListViewAdapterProduct extends BaseAdapter {
Context mContext;
// the construcor that initializes the context
public ListViewAdapterProduct(Context c){
mContext = c;
}
...
However, the trace that you have posted, suggest that the problem is in your adapter's getView() method, on line 91. You should check that also.
ListViewAdapterproduct.getView(ListViewAdapterproduct.java:91)
Edit:
list.setAdapter(adapter);
In this line the adapter is null, you did not initialize it... (you set your adapter twice, first with null).
It is a good practise to define your variables locally, and make them class variable only when you actually need it. This way its harder to forget about initialization.
you have a problem in Context mContext; is null
in your activity set mContext = this; so mContext would be the activity context you will be able to use the resources
this is the correct code :
since your already sent this in your adapter you need to catch it there
public class ListViewAdapterProduct extends BaseAdapter {
Context mContext;
ArrayList<ClassproductList> arraylist;
ListView listView = (ListView) findViewById(R.id.listview1);
//add this
public ListViewAdapterProduct (Context mContext,ArrayList<ClassproductList> arraylist)
{
this.mContext = mContext;
this.arraylist = arraylist;
}
SOLVED !!!
I changed back from onItemClick (complicated to use with custom adapter) to onClick (simple to use with custom adapter) and applied:
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String mytitle=holder.title.getText().toString();
if(mytitle.equals("ProductOne")){
Intent intent1 = new Intent(mContext, ProductClass.ProductOne);
mContext.startActivity(intent1);
...
}
}
Had to add:
String mytitle=holder.title.getText().toString();

Execute Multi line SQLite query

i am trying to download a .txt file from internet and then read it and execute it.
i had success in downloading, reading and executing a single line query file.
but the problem is i cant execute multi line query from the file. i did some research and they did not suit my needs so i came up with a solution but i cant figure out some errors.
my idea is to convert a .txt file, line by line to a List and then for each String in the List execute a query within sqlite.
here's some of my code :
public void updateDB(View v){
new DownloadFileFromURL().execute(file_url);
File file = new File(Environment
.getExternalStorageDirectory().toString()
+ "/update.txt")
Log.i("UPDATE", "SQL DOWNLOADED");
while(start.equals(true)){
List<String> list = readUpdate2("update.txt");
for (String string : list) {
datasource.executeRaw(string);
Log.i("UPDATE", "EXEC SQL # : " + list.size());
}
file.delete();}}
public List<String> readUpdate2(String url){
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
List<String> list = null;
File file = new File(sdcard,url);
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while((line = br.readLine()) != null) {
// this is where the error happens :
list.add(line);
Log.i("UPDATE", "LINE : " + line);
}
}
catch (IOException e) {
}
return list;
}
the problem is i am stuck with a Nullpointer exception. it says the line is empty.
Here is the logcat :
03-30 18:01:18.965: E/AndroidRuntime(12474): FATAL EXCEPTION: main
03-30 18:01:18.965: E/AndroidRuntime(12474): java.lang.IllegalStateException: Could not execute method of the activity
03-30 18:01:18.965: E/AndroidRuntime(12474): at android.view.View$1.onClick(View.java:3838)
03-30 18:01:18.965: E/AndroidRuntime(12474): at android.view.View.performClick(View.java:4475)
03-30 18:01:18.965: E/AndroidRuntime(12474): at android.view.View$PerformClick.run(View.java:18786)
03-30 18:01:18.965: E/AndroidRuntime(12474): at android.os.Handler.handleCallback(Handler.java:730)
03-30 18:01:18.965: E/AndroidRuntime(12474): at android.os.Handler.dispatchMessage(Handler.java:92)
03-30 18:01:18.965: E/AndroidRuntime(12474): at android.os.Looper.loop(Looper.java:137)
03-30 18:01:18.965: E/AndroidRuntime(12474): at android.app.ActivityThread.main(ActivityThread.java:5493)
03-30 18:01:18.965: E/AndroidRuntime(12474): at java.lang.reflect.Method.invokeNative(Native Method)
03-30 18:01:18.965: E/AndroidRuntime(12474): at java.lang.reflect.Method.invoke(Method.java:525)
03-30 18:01:18.965: E/AndroidRuntime(12474): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
03-30 18:01:18.965: E/AndroidRuntime(12474): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
03-30 18:01:18.965: E/AndroidRuntime(12474): at dalvik.system.NativeStart.main(Native Method)
03-30 18:01:18.965: E/AndroidRuntime(12474): Caused by: java.lang.reflect.InvocationTargetException
03-30 18:01:18.965: E/AndroidRuntime(12474): at java.lang.reflect.Method.invokeNative(Native Method)
03-30 18:01:18.965: E/AndroidRuntime(12474): at java.lang.reflect.Method.invoke(Method.java:525)
03-30 18:01:18.965: E/AndroidRuntime(12474): at android.view.View$1.onClick(View.java:3833)
03-30 18:01:18.965: E/AndroidRuntime(12474): ... 11 more
03-30 18:01:18.965: E/AndroidRuntime(12474): Caused by: java.lang.NullPointerException
03-30 18:01:18.965: E/AndroidRuntime(12474): at com.meemarbashi.meemardictionary.UpdateActivity.readUpdate2(UpdateActivity.java:292)
03-30 18:01:18.965: E/AndroidRuntime(12474): at com.meemarbashi.meemardictionary.UpdateActivity.updateDB(UpdateActivity.java:223)
03-30 18:01:18.965: E/AndroidRuntime(12474): ... 14 more
so basically i am getting no text from the .txt file and then i cannot add anything to my List. what should i do ?
thanks in advance
This is probably the reason of null pointer exception:
List<String> list = null;
Try assigning a new list instead of null

null into array from loop

this answer comes from here: 2Darray to 1darray with for loop
I donĀ“t know if I can reopen it once it was answered.. my apologies if that's the case.
I modified a little that function to end like this:
public String[] initarray() {
xFINAL = new String [data_array.length];
int i = 0;
int j=0;
int UMB1 = 100;
int l = data_array.length;
for (i=0 ; i < l ; i++) {
LATIT = Double.parseDouble(data_array [i][2]);
LONGIT = Double.parseDouble(data_array [i][3]);
dist = calculateDistanceByHaversineFormula(Localizacion.LONGITUD_D, Localizacion.data_arrayUD_D,LONGIT, LATIT);
if (dist < UMB1){
xFINAL[j++] = data_array [i][0];
System.out.println(Arrays.toString(xFINAL));
System.out.println("j++" + data_array[i][0]);
}
}
return xFINAL;
}
dist = the difference in meters between 2 locations, returning an int.
data_array is already defined before.
It's "kind of working". What I get from that code is an array like this(data1 and data3 are inside the condition, data2 would be false)
[data1 , data3 , null]
And should be like this instead I think:
[data1, data3]
The error comes when I am trying to use that xFINAL array with an spinner:
public final void rel() {
String[] array_spinner = MYSQL.xFINAL;
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, array_spinner);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp_1.setAdapter(spinnerArrayAdapter);
}
When I try to load the spinner it gives me the following:
03-25 13:25:26.367: E/AndroidRuntime(30843): FATAL EXCEPTION: main
03-25 13:25:26.367: E/AndroidRuntime(30843): java.lang.NullPointerException
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.widget.Spinner.measureContentWidth(Spinner.java:681)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.widget.Spinner.onMeasure(Spinner.java:442)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.view.View.measure(View.java:15518)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.view.View.measure(View.java:15518)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.view.View.measure(View.java:15518)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.view.View.measure(View.java:15518)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.view.View.measure(View.java:15518)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-25 13:25:26.367: E/AndroidRuntime(30843): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.view.View.measure(View.java:15518)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.view.Choreographer.doFrame(Choreographer.java:532)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.os.Handler.handleCallback(Handler.java:725)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.os.Handler.dispatchMessage(Handler.java:92)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.os.Looper.loop(Looper.java:137)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-25 13:25:26.367: E/AndroidRuntime(30843): at java.lang.reflect.Method.invokeNative(Native Method)
03-25 13:25:26.367: E/AndroidRuntime(30843): at java.lang.reflect.Method.invoke(Method.java:511)
03-25 13:25:26.367: E/AndroidRuntime(30843): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-25 13:25:26.367: E/AndroidRuntime(30843): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-25 13:25:26.367: E/AndroidRuntime(30843): at dalvik.system.NativeStart.main(Native Method)
If I just get the 3 data into the spinner it works.. so I am lost.
Any help please? :)
Thanks in advance.
[edit]
03-25 19:39:23.081: I/System.out(3501): [00000001]
03-25 19:39:23.081: I/System.out(3501): 00000001
03-25 19:39:23.091: I/System.out(3501): [00000001, 00000003]
03-25 19:39:23.091: D/AndroidRuntime(3501): Shutting down VM
03-25 19:39:23.091: W/dalvikvm(3501): threadid=1: thread exiting with uncaught exception (group=0x40bac930)
03-25 19:39:23.091: E/AndroidRuntime(3501): FATAL EXCEPTION: main
03-25 19:39:23.091: E/AndroidRuntime(3501): java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
03-25 19:39:23.091: E/AndroidRuntime(3501): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
03-25 19:39:23.091: E/AndroidRuntime(3501): at java.util.ArrayList.get(ArrayList.java:304)
03-25 19:39:23.091: E/AndroidRuntime(3501): at com.example.mvlpres.c_sql.recorre_array2(c_sql.java:635)
03-25 19:39:23.091: E/AndroidRuntime(3501): at com.example.mvlpres.SincronizacionFragment$1.onClick(SincronizacionFragment.java:179)
03-25 19:39:23.091: E/AndroidRuntime(3501): at android.view.View.performClick(View.java:4204)
03-25 19:39:23.091: E/AndroidRuntime(3501): at android.view.View$PerformClick.run(View.java:17355)
03-25 19:39:23.091: E/AndroidRuntime(3501): at android.os.Handler.handleCallback(Handler.java:725)
03-25 19:39:23.091: E/AndroidRuntime(3501): at android.os.Handler.dispatchMessage(Handler.java:92)
03-25 19:39:23.091: E/AndroidRuntime(3501): at android.os.Looper.loop(Looper.java:137)
03-25 19:39:23.091: E/AndroidRuntime(3501): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-25 19:39:23.091: E/AndroidRuntime(3501): at java.lang.reflect.Method.invokeNative(Native Method)
03-25 19:39:23.091: E/AndroidRuntime(3501): at java.lang.reflect.Method.invoke(Method.java:511)
03-25 19:39:23.091: E/AndroidRuntime(3501): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-25 19:39:23.091: E/AndroidRuntime(3501): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-25 19:39:23.091: E/AndroidRuntime(3501): at dalvik.system.NativeStart.main(Native Method)
System.out.println(Arrays.toString(xFINAL));
This line should be after for loop completes.
For references (anything that holds an object) that is null.
For int/short/byte that is a 0.
For float/double that is a 0.0
For booleans that is a false.
When you create an array of String, all entries are null initially. Your array size is 3 I think so initially it is initialized with [null,null, null]. Two null get replaced with the values and one remains as it is.
EDIT: Use ArrayList instead Array
List<String> list = new ArrayList<String>();
for (i=0 ; i < l ; i++) {
LATIT = Double.parseDouble(data_array [i][2]);
LONGIT = Double.parseDouble(data_array [i][3]);
dist = calculateDistanceByHaversineFormula(Localizacion.LONGITUD_D, Localizacion.data_arrayUD_D,LONGIT, LATIT);
if (dist < UMB1){
list.add(data_array [i][0]);
System.out.println(list);
System.out.println(list.get(i));
}
}

Categories