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));
}
}
Related
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);
}
I have a simple background task to sign up, displaying a progress dialog for the user. The issue is that I believe I'm getting NullPointerException is because the second activity that's being launched in the intent is somehow being called before the dialogProgressHide(); method, even though that one is listed first in the code, therefore different context and the progress is returning null causing error.
book.saveInBackground(new SaveCallback()
{
#Override
public void done(ParseException e) {
if (e == null)
{
dialogProgressHide();
Toast.makeText(getApplicationContext(), "Your book was posted correctly!", Toast.LENGTH_LONG).show();
finish();
}
else
{
Toast.makeText(getApplicationContext(), "An error has occured: " + e + "\n" + "Please try again!", Toast.LENGTH_LONG).show();
dialogProgressHide();
}
}
});
I could be wrong but that makes sense to me, question is how would I fix this? I simply want the dialog to disappear before finishing the activity, so that it doesn't attempt to when the progress is no longer in context.
Here is dialog methods if needed:
private void dialogProgressShow() {
mDialog = new ProgressDialog(this);
mDialog.setMessage("One moment please while we post your book.");
mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mDialog.show();
}
private void dialogProgressHide() {
if (mDialog.isShowing())
{
mDialog.dismiss();
mDialog = null;
}
}
}
LOGCAT:
03-25 17:12:44.862: E/AndroidRuntime(16528): FATAL EXCEPTION: main
03-25 17:12:44.862: E/AndroidRuntime(16528): Process: com.jameswilson.booksale, PID: 16528
03-25 17:12:44.862: E/AndroidRuntime(16528): java.lang.NullPointerException
03-25 17:12:44.862: E/AndroidRuntime(16528): at com.jameswilson.booksale.PostBookActivity.dialogProgressHide(PostBookActivity.java:255)
03-25 17:12:44.862: E/AndroidRuntime(16528): at com.jameswilson.booksale.PostBookActivity.access$1(PostBookActivity.java:254)
03-25 17:12:44.862: E/AndroidRuntime(16528): at com.jameswilson.booksale.PostBookActivity$2$1.done(PostBookActivity.java:144)
03-25 17:12:44.862: E/AndroidRuntime(16528): at com.parse.SaveCallback.internalDone(SaveCallback.java:39)
03-25 17:12:44.862: E/AndroidRuntime(16528): at com.parse.SaveCallback.internalDone(SaveCallback.java:27)
03-25 17:12:44.862: E/AndroidRuntime(16528): at com.parse.Parse$6$1.run(Parse.java:846)
03-25 17:12:44.862: E/AndroidRuntime(16528): at android.os.Handler.handleCallback(Handler.java:733)
03-25 17:12:44.862: E/AndroidRuntime(16528): at android.os.Handler.dispatchMessage(Handler.java:95)
03-25 17:12:44.862: E/AndroidRuntime(16528): at android.os.Looper.loop(Looper.java:136)
03-25 17:12:44.862: E/AndroidRuntime(16528): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-25 17:12:44.862: E/AndroidRuntime(16528): at java.lang.reflect.Method.invokeNative(Native Method)
03-25 17:12:44.862: E/AndroidRuntime(16528): at java.lang.reflect.Method.invoke(Method.java:515)
03-25 17:12:44.862: E/AndroidRuntime(16528): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-25 17:12:44.862: E/AndroidRuntime(16528): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-25 17:12:44.862: E/AndroidRuntime(16528): at dalvik.system.NativeStart.main(Native Method)
Check if your dialog reference is null before trying to dismiss it
private void dialogProgressHide() {
if (mDialog != null && mDialog.isShowing())
{
mDialog.dismiss();
mDialog = null;
}
}
I am new to Android.
I've just started to build my first Android app and have been struggling with this errorfor the past 2 days.
Finding no solution I had to come here.
Error:
Unfortunately "app name" has stopped
This is main activity layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="TextView" />
</RelativeLayout>
This is another layout:
<?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">
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
logcat error
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.listview/com.example.listview.MainActivity}: java.lang.NullPointerException
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.app.ActivityThread.access$700(ActivityThread.java:135)
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.os.Handler.dispatchMessage(Handler.java:102)
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.os.Looper.loop(Looper.java:137)
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.app.ActivityThread.main(ActivityThread.java:4998)
03-25 12:01:29.772: E/AndroidRuntime(1314): at java.lang.reflect.Method.invokeNative(Native Method)
03-25 12:01:29.772: E/AndroidRuntime(1314): at java.lang.reflect.Method.invoke(Method.java:515)
03-25 12:01:29.772: E/AndroidRuntime(1314): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-25 12:01:29.772: E/AndroidRuntime(1314): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-25 12:01:29.772: E/AndroidRuntime(1314): at dalvik.system.NativeStart.main(Native Method)
03-25 12:01:29.772: E/AndroidRuntime(1314): Caused by: java.lang.NullPointerException
03-25 12:01:29.772: E/AndroidRuntime(1314): at com.example.listview.MainActivity.onCreate(MainActivity.java:36)
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.app.Activity.performCreate(Activity.java:5243)
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
03-25 12:01:29.772: E/AndroidRuntime(1314): ... 11 more
03-25 12:01:35.622: I/Process(1314): Sending signal. PID: 1314 SIG: 9
03-25 12:11:56.522: D/AndroidRuntime(1378): Shutting down VM
03-25 12:11:56.522: W/dalvikvm(1378): threadid=1: thread exiting with uncaught exception (group=0xb3adbb90)
03-25 12:11:56.542: E/AndroidRuntime(1378): FATAL EXCEPTION: main
03-25 12:11:56.542: E/AndroidRuntime(1378): Process: com.example.listview, PID: 1378
03-25 12:11:56.542: E/AndroidRuntime(1378): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.listview/com.example.listview.MainActivity}: java.lang.NullPointerException
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.app.ActivityThread.access$700(ActivityThread.java:135)
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.os.Handler.dispatchMessage(Handler.java:102)
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.os.Looper.loop(Looper.java:137)
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.app.ActivityThread.main(ActivityThread.java:4998)
03-25 12:11:56.542: E/AndroidRuntime(1378): at java.lang.reflect.Method.invokeNative(Native Method)
03-25 12:11:56.542: E/AndroidRuntime(1378): at java.lang.reflect.Method.invoke(Method.java:515)
03-25 12:11:56.542: E/AndroidRuntime(1378): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-25 12:11:56.542: E/AndroidRuntime(1378): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-25 12:11:56.542: E/AndroidRuntime(1378): at dalvik.system.NativeStart.main(Native Method)
03-25 12:11:56.542: E/AndroidRuntime(1378): Caused by: java.lang.NullPointerException
03-25 12:11:56.542: E/AndroidRuntime(1378): at com.example.listview.MainActivity.onCreate(MainActivity.java:36)
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.app.Activity.performCreate(Activity.java:5243)
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
03-25 12:11:56.542: E/AndroidRuntime(1378): ... 11 more
main activity code
package com.example.listview;
import java.util.ArrayList;
import java.util.Arrays;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lst = (ListView)findViewById(R.id.listView1);
//ListView mainListView = null;
String[] test = new String[] { "Mercury", "Venus", "Earth", "Mars",
"Jupiter", "Saturn", "Uranus", "Neptune"};
ArrayList<String> planetList = new ArrayList<String>();
planetList.addAll( Arrays.asList(test) );
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);
// Add more planets. If you passed a String[] instead of a List<String>
// into the ArrayAdapter constructor, you must not add more items.
// Otherwise an exception will occur.
listAdapter.add( "Ceres" );
listAdapter.add( "Pluto" );
listAdapter.add( "Haumea" );
listAdapter.add( "Makemake" );
listAdapter.add( "Eris" );
// Set the ArrayAdapter as the ListView's adapter.
lst.setAdapter( listAdapter );
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Your listview is not contained in the activity's layout so when you call it it gives you a null pointer exception. you could put the listview inside the main activity's layout.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="TextView" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</RelativeLayout>
Line 36 is:
lst.setAdapter( listAdapter );
Means that lst is probably null.
ListView lst = (ListView)findViewById(R.id.listView1); //NULL
Failed to find the view, because you load main_activity layout, which does not have a ListView with id=listView1.
Probably you need to load another layout, which it contains.
ListView lst = (ListView)findViewById(R.id.listView1); return null because you don't use the right content in setContentView(R.layout.activity_main);. You have two options:
Use a include to put a layout inside another:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
... >
<TextView
... />
<include android:layout="#layout\my_layout_listview" />
</RelativeLayout>
Or set the other layout which contains your ListView in:
setContentView(R.layout.my_layout_listview);
I'm trying to setup a method of executing one task if my application has been run previously - and another task if it has not.
To do this I have created a String runPrevious:
My problem is every time I attempt to execute the application I get a force close due to a null pointer exception here:
if (runPrevious.equals("yes")) {
which I don't understand - since I'm declaring the value of the String here:
String runPrevious = "No";
LOGCAT:
12-13 13:20:12.485: E/AndroidRuntime(1803): FATAL EXCEPTION: main
12-13 13:20:12.485: E/AndroidRuntime(1803): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.idg.omv/com.idg.omv.ui.phone.Home}: java.lang.NullPointerException
12-13 13:20:12.485: E/AndroidRuntime(1803): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
12-13 13:20:12.485: E/AndroidRuntime(1803): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
12-13 13:20:12.485: E/AndroidRuntime(1803): at android.app.ActivityThread.access$600(ActivityThread.java:156)
12-13 13:20:12.485: E/AndroidRuntime(1803): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
12-13 13:20:12.485: E/AndroidRuntime(1803): at android.os.Handler.dispatchMessage(Handler.java:99)
12-13 13:20:12.485: E/AndroidRuntime(1803): at android.os.Looper.loop(Looper.java:153)
12-13 13:20:12.485: E/AndroidRuntime(1803): at android.app.ActivityThread.main(ActivityThread.java:5297)
12-13 13:20:12.485: E/AndroidRuntime(1803): at java.lang.reflect.Method.invokeNative(Native Method)
12-13 13:20:12.485: E/AndroidRuntime(1803): at java.lang.reflect.Method.invoke(Method.java:511)
12-13 13:20:12.485: E/AndroidRuntime(1803): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
12-13 13:20:12.485: E/AndroidRuntime(1803): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
12-13 13:20:12.485: E/AndroidRuntime(1803): at dalvik.system.NativeStart.main(Native Method)
12-13 13:20:12.485: E/AndroidRuntime(1803): Caused by: java.lang.NullPointerException
12-13 13:20:12.485: E/AndroidRuntime(1803): at com.idg.omv.ui.phone.Home.onCreate(Home.java:113)
12-13 13:20:12.485: E/AndroidRuntime(1803): at android.app.Activity.performCreate(Activity.java:5262)
12-13 13:20:12.485: E/AndroidRuntime(1803): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
12-13 13:20:12.485: E/AndroidRuntime(1803): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
12-13 13:20:12.485: E/AndroidRuntime(1803): ... 11 more
12-13 13:20:13.969: I/Process(1803): Sending signal. PID: 1803 SIG: 9
JAVA
public class Home extends YouTubeBaseActivity implements
VideoClickListener {
String runPrevious = "No";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
String runPrevious = getIntent().getStringExtra("runPrevious");
if (runPrevious.equals("yes")) {
String playlist = getIntent().getStringExtra("playlist");
new GetYouTubeUserVideosTask(responseHandler, playlist).execute();
}else{
new GetYouTubeUserVideosTask(responseHandler, playlist).execute();
}
You are declaring it once again (probably with null value)
String runPrevious = getIntent().getStringExtra("runPrevious");
do not test strings for null if you are comparing them to a literal value. Instead use non-null-pointer succeptible compairsons.
In your case:
if ("yes".equals(runPrevious))
This will never throw a null pointer exception.
I am getting a NPE, but when I check the logcat there are no points at which it is using my code.
The program crashes after attempting to show an AlertDialog containing a RadioButtonGroup. It previously worked and the only changes I remember are after the dialog.
Now I recieve the following:
EDIT:
06-19 17:15:19.430: E/AndroidRuntime(898): java.lang.NullPointerException
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.AbsListView.obtainView(AbsListView.java:2143)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.ListView.onMeasure(ListView.java:1158)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.View.measure(View.java:15518)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.View.measure(View.java:15518)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.View.measure(View.java:15518)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.View.measure(View.java:15518)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.View.measure(View.java:15518)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-19 17:15:19.430: E/AndroidRuntime(898): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.View.measure(View.java:15518)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.Choreographer.doFrame(Choreographer.java:532)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.os.Handler.handleCallback(Handler.java:725)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.os.Handler.dispatchMessage(Handler.java:92)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.os.Looper.loop(Looper.java:137)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-19 17:15:19.430: E/AndroidRuntime(898): at java.lang.reflect.Method.invokeNative(Native Method)
06-19 17:15:19.430: E/AndroidRuntime(898): at java.lang.reflect.Method.invoke(Method.java:511)
06-19 17:15:19.430: E/AndroidRuntime(898): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-19 17:15:19.430: E/AndroidRuntime(898): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-19 17:15:19.430: E/AndroidRuntime(898): at dalvik.system.NativeStart.main(Native Method)
The last code block that is run, when I step through, is:
public void addTeam (View view) {
//open select race dialog
AlertDialog alertDialog;
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
String[] items = new String[races.size()];
int i = 0;
for (Race race : races) {
//add radio button for each race
items[i] = race.raceName;
}
builder.setTitle(getString(R.string.select_race));
builder.setSingleChoiceItems(items, 0, null);
builder.setPositiveButton(R.string.done, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int whichButton) {
dialogInterface.dismiss();
Team team = new Team();
team.raceIndex = ((AlertDialog) dialogInterface).getListView().getCheckedItemPosition();
team.raceName = races.get(team.raceIndex).raceName;
setContentView(R.layout.edit_team);
((TextView) findViewById(R.id.raceTV)).setText(team.raceName);
}
});
alertDialog = builder.create();
alertDialog.show();
}
Without a line reference to my code, I do not even know where to start. Help is appreciated.
Here is an error in your code:
int i = 0;
for (Race race : races) {
//add radio button for each race
items[i] = race.raceName;
}
You don't increment i in the for loop.
May be this causes the crash.
You have two dialog instances. 1) AlertDialog dialog; and 2) DialogInterface dialog. So most probably AlertDialog dialog; throwing NPE as it was never initilized so defaluts to null
Replace this
((TextView) findViewById(R.id.raceTV)).setText(team.raceName);
with
((TextView) dialog.findViewById(R.id.raceTV)).setText(team.raceName);