Okay, so i finally got my button to start my new activity from the main activity. now, the problem i have is that when the new activity starts, the avd gives me an error saying "unfortunately, yourApp has stopped working". I tried for hours to detect the problem untill i began to comment some part of the new activity's code and then finally, i found it. keep in mind, that almost all the variables i used in the new activity also had common names and ids to those in the mainActivity, but this should not be the problem since i am not passing anything through both activities...at least not now. The problem was from the spinners. their declaration worked fine...i guess, the findViewByid method gave no problem, the arrayAdapter also gave no problems but i noticed that the app only shuts down when i remove the comments from the onItemSelectedListener and the setAdapters methods. i don't know why this happens. i used the same code for the mainActivity and it works perfectly. Dunno if i overloaded the onCreate method but as i said earlier, it works fine in the mainActivity. the new activity code for th onCreate method is given below...the other parts of the code works fine.
public class firstYearSecondSemester2 extends Activity implements AdapterView.OnItemSelectedListener {
private String[] grades;
public Spinner spinner0, spinner1, spinner2, spinner3, spinner4, spinner5, spinner6, spinner7, spinner8;
private double[] grade_values = {0.0, 5.0, 4.0, 3.0, 2.0, 1.0, 0.0};
//private Spinner gpa_grade0, gpa_grade1, gpa_grade2, gpa_grade3, gpa_grade4, gpa_grade5;
private double gpa_grade_0, gpa_grade_1, gpa_grade_2, gpa_grade_3, gpa_grade_4, gpa_grade_5, gpa_grade_6, gpa_grade_7, gpa_grade_8;
private TextView gpa, gpa_credits0, gpa_credits1, gpa_credits2, gpa_credits3, gpa_credits4,gpa_credits5, gpa_credits6, gpa_credits7,gpa_credits8;
private int gpa_credits_0, gpa_credits_1, gpa_credits_2, gpa_credits_3, gpa_credits_4, gpa_credits_5, gpa_credits_6, gpa_credits_7, gpa_credits_8;
public int[] spinner_ids;
public int spinner_check;
public int spinner_index;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_year_second_semester2);
spinner_ids = new int[9];
spinner_check = 0;
spinner_index = 0;
gpa = (TextView) findViewById(R.id.gpa_results_text);
grades = getResources().getStringArray(R.array.grades);
gpa_credits0 = (TextView) findViewById(R.id.gpa_credits_0);
gpa_credits1 = (TextView) findViewById(R.id.gpa_credits_1);
gpa_credits2 = (TextView) findViewById(R.id.gpa_credits_2);
gpa_credits3 = (TextView) findViewById(R.id.gpa_credits_3);
gpa_credits4 = (TextView) findViewById(R.id.gpa_credits_4);
gpa_credits5 = (TextView) findViewById(R.id.gpa_credits_5);
gpa_credits6 = (TextView) findViewById(R.id.gpa_credits_6);
gpa_credits7 = (TextView) findViewById(R.id.gpa_credits_7);
gpa_credits8 = (TextView) findViewById(R.id.gpa_credits_8);
spinner0 = (Spinner) findViewById(R.id.gradeSpinner0);
spinner1 = (Spinner) findViewById(R.id.gradeSpinner1);
spinner2 = (Spinner) findViewById(R.id.gradeSpinner2);
spinner3 = (Spinner) findViewById(R.id.gradeSpinner3);
spinner4 = (Spinner) findViewById(R.id.gradeSpinner4);
spinner5 = (Spinner) findViewById(R.id.gradeSpinner5);
spinner6 = (Spinner) findViewById(R.id.gradeSpinner6);
spinner7 = (Spinner) findViewById(R.id.gradeSpinner7);
spinner8 = (Spinner) findViewById(R.id.gradeSpinner8);
ArrayAdapter<String> each_grade = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, grades);
each_grade.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner0.setAdapter(new NothingSelectedSpinnerAdapter(each_grade, R.layout.contact_spinner_row_nothing_selected,/* R.layout.contact_spinner_nothing_selected_dropdown, // Optional*/this));
spinner1.setAdapter(new NothingSelectedSpinnerAdapter(each_grade, R.layout.contact_spinner_row_nothing_selected,/* R.layout.contact_spinner_nothing_selected_dropdown, // Optional*/this));
spinner2.setAdapter(new NothingSelectedSpinnerAdapter(each_grade, R.layout.contact_spinner_row_nothing_selected,/* R.layout.contact_spinner_nothing_selected_dropdown, // Optional*/this));
spinner3.setAdapter(new NothingSelectedSpinnerAdapter(each_grade, R.layout.contact_spinner_row_nothing_selected,/* R.layout.contact_spinner_nothing_selected_dropdown, // Optional*/this));
spinner4.setAdapter(new NothingSelectedSpinnerAdapter(each_grade, R.layout.contact_spinner_row_nothing_selected,/* R.layout.contact_spinner_nothing_selected_dropdown, // Optional*/this));
spinner5.setAdapter(new NothingSelectedSpinnerAdapter(each_grade, R.layout.contact_spinner_row_nothing_selected,/* R.layout.contact_spinner_nothing_selected_dropdown, // Optional*/this));
spinner6.setAdapter(new NothingSelectedSpinnerAdapter(each_grade, R.layout.contact_spinner_row_nothing_selected,/* R.layout.contact_spinner_nothing_selected_dropdown, // Optional*/this));
spinner7.setAdapter(new NothingSelectedSpinnerAdapter(each_grade, R.layout.contact_spinner_row_nothing_selected,/* R.layout.contact_spinner_nothing_selected_dropdown, // Optional*/this));
spinner8.setAdapter(new NothingSelectedSpinnerAdapter(each_grade, R.layout.contact_spinner_row_nothing_selected,/* R.layout.contact_spinner_nothing_selected_dropdown, // Optional*/this));
spinner0.setOnItemSelectedListener(firstYearSecondSemester2.this);
spinner1.setOnItemSelectedListener(firstYearSecondSemester2.this);
spinner2.setOnItemSelectedListener(firstYearSecondSemester2.this);
spinner3.setOnItemSelectedListener(firstYearSecondSemester2.this);
spinner4.setOnItemSelectedListener(firstYearSecondSemester2.this);
spinner5.setOnItemSelectedListener(firstYearSecondSemester2.this);
spinner6.setOnItemSelectedListener(firstYearSecondSemester2.this);
spinner7.setOnItemSelectedListener(firstYearSecondSemester2.this);
spinner8.setOnItemSelectedListener(firstYearSecondSemester2.this);
}
Logcat Messages
04-14 02:21:16.260 1772-1772/com.daftnerds.juliusugochukwu.cgpa4 D/dalvikvm﹕ Not late-enabling CheckJNI (already on)
04-14 02:21:16.830 1772-1772/com.daftnerds.juliusugochukwu.cgpa4 I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
04-14 02:21:16.830 1772-1772/com.daftnerds.juliusugochukwu.cgpa4 W/dalvikvm﹕ VFY: unable to resolve virtual method 11350: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
04-14 02:21:16.830 1772-1772/com.daftnerds.juliusugochukwu.cgpa4 D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
04-14 02:21:16.830 1772-1772/com.daftnerds.juliusugochukwu.cgpa4 I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
04-14 02:21:16.830 1772-1772/com.daftnerds.juliusugochukwu.cgpa4 W/dalvikvm﹕ VFY: unable to resolve virtual method 11356: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
04-14 02:21:16.830 1772-1772/com.daftnerds.juliusugochukwu.cgpa4 D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
04-14 02:21:16.830 1772-1772/com.daftnerds.juliusugochukwu.cgpa4 I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
04-14 02:21:16.830 1772-1772/com.daftnerds.juliusugochukwu.cgpa4 W/dalvikvm﹕ VFY: unable to resolve virtual method 9044: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
04-14 02:21:16.830 1772-1772/com.daftnerds.juliusugochukwu.cgpa4 D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000e
04-14 02:21:16.910 1772-1772/com.daftnerds.juliusugochukwu.cgpa4 I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
04-14 02:21:16.910 1772-1772/com.daftnerds.juliusugochukwu.cgpa4 W/dalvikvm﹕ VFY: unable to resolve virtual method 368: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
04-14 02:21:16.910 1772-1772/com.daftnerds.juliusugochukwu.cgpa4 D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
04-14 02:21:16.910 1772-1772/com.daftnerds.juliusugochukwu.cgpa4 I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
04-14 02:21:16.910 1772-1772/com.daftnerds.juliusugochukwu.cgpa4 W/dalvikvm﹕ VFY: unable to resolve virtual method 390: Landroid/content/res/TypedArray;.getType (I)I
04-14 02:21:16.910 1772-1772/com.daftnerds.juliusugochukwu.cgpa4 D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
04-14 02:21:17.150 1772-1772/com.daftnerds.juliusugochukwu.cgpa4 D/dalvikvm﹕ GC_FOR_ALLOC freed 140K, 8% free 3075K/3340K, paused 0ms, total 5ms
04-14 02:21:17.150 1772-1772/com.daftnerds.juliusugochukwu.cgpa4 I/dalvikvm-heap﹕ Grow heap (frag case) to 4.195MB for 1127532-byte allocation
04-14 02:21:17.210 1772-1781/com.daftnerds.juliusugochukwu.cgpa4 D/dalvikvm﹕ GC_FOR_ALLOC freed 13K, 7% free 4163K/4444K, paused 3ms, total 4ms
04-14 02:21:18.930 1772-1772/com.daftnerds.juliusugochukwu.cgpa4 I/Choreographer﹕ Skipped 103 frames! The application may be doing too much work on its main thread.
04-14 02:21:18.930 1772-1772/com.daftnerds.juliusugochukwu.cgpa4 D/gralloc_goldfish﹕ Emulator without GPU emulation detected.
04-14 02:27:17.189 1772-1772/com.daftnerds.juliusugochukwu.cgpa4 I/Choreographer﹕ Skipped 60 frames! The application may be doing too much work on its main thread.
04-14 02:27:17.309 1772-1772/com.daftnerds.juliusugochukwu.cgpa4 D/dalvikvm﹕ GC_FOR_ALLOC freed 168K, 7% free 4507K/4796K, paused 2ms, total 2ms
04-14 02:29:34.831 2264-2264/com.daftnerds.juliusugochukwu.cgpa4 D/gralloc_goldfish﹕ Emulator without GPU emulation detected.
Now, once i click the button to start the new activity, this happens
04-14 02:30:40.562 2264-2264/com.daftnerds.juliusugochukwu.cgpa4 D/AndroidRuntime﹕ Shutting down VM
04-14 02:30:40.562 2264-2264/com.daftnerds.juliusugochukwu.cgpa4 W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb0d3cb20)
04-14 02:30:40.602 2264-2264/com.daftnerds.juliusugochukwu.cgpa4 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.daftnerds.juliusugochukwu.cgpa4, PID: 2264
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.daftnerds.juliusugochukwu.cgpa4/com.daftnerds.juliusugochukwu.cgpa4.firstYearSecondSemester2}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.daftnerds.juliusugochukwu.cgpa4.firstYearSecondSemester2.onCreate(firstYearSecondSemester2.java:76)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
change line with
spinner0.setAdapter(new NothingSelectedSpinnerAdapter(each_grade,R.layout.contact_spinner_row_nothing_selected,this));
and clean your project once.
Omg...found the problem. Truly it was line 76. Please don't be annoyed. I initially had 9 spinners in the main activity and 7 in the new activity. I copied the main activity spinner codes and pasted in the new activity. So when it gets to the 8th spinner (line 76), it gives an error since that spinner is not available in the layout. Thanks for all the help suggested guys...really appreciate them.
im making a simple calories calculator and it gives me that error, i already reviewed the code and searched here for the solution but im not able to see why is not running.
I thought it was because i had not initialized the variable, so i did it and still got the same error, maybe is something with the spinners, im new on using spinners
here is the code:
public class CaloriesCalculator extends ActionBarActivity {
EditText etAge, etWeight, etHeight;
Button btnCalculate;
TextView tvResult;
Spinner spinnerGender, spinnerActivity;
String itemGender, itemActivity;
int Height=0;
int Weight=0;
int Age=0;;
double bmr=0.0;
double tdee=0.0;
String result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.caloriescalculator);
spinnerGender=(Spinner)findViewById(R.id.spinnerGender);
spinnerActivity=(Spinner)findViewById(R.id.spinnerActivity);
etAge=(EditText)findViewById(R.id.etAge);
etWeight=(EditText)findViewById(R.id.etWeight);
etHeight=(EditText)findViewById(R.id.etHeight);
tvResult=(TextView)findViewById(R.id.tvResult);
List<String> list = new ArrayList<String>();
list.add("Male");
list.add("Female");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, list );
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerGender.setAdapter(dataAdapter);
List<String> list2 = new ArrayList<String>();
list.add("Sedentary");
list.add("Lightly Active");
list.add("Moderalety Active");
list.add("Very Active");
list.add("Extremely Active");
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, list2 );
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerActivity.setAdapter(dataAdapter2);
btnCalculate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
spinnerGender.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
spinnerActivity.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
itemGender=String.valueOf(spinnerGender.getSelectedItem());
itemActivity=String.valueOf(spinnerActivity.getSelectedItem());
if(itemGender=="Male"){
Weight=Integer.parseInt(etWeight.getText().toString());
Height=Integer.parseInt(etHeight.getText().toString());
Age=Integer.parseInt(etAge.getText().toString());
if(itemActivity=="Sedentary"){
bmr=66+((13.7 * Weight)+(5*Height))-(6.8*Age);
tdee=bmr*1.2;
}
else if(itemActivity=="Lightly Active"){
bmr=66+((13.7 * Weight)+(5*Height))-(6.8*Age);
tdee=bmr*1.375;
}
else if(itemActivity=="Moderalety Active"){
bmr=66+((13.7 * Weight)+(5*Height))-(6.8*Age);
tdee=bmr*1.55;
}
else if(itemActivity=="Very Active"){
bmr=66+((13.7 * Weight)+(5*Height))-(6.8*Age);
tdee=bmr*1.725;
}
else if(itemActivity=="Extremely Active"){
bmr=66+((13.7 * Weight)+(5*Height))-(6.8*Age);
tdee=bmr*1.9;
}
}
else if(itemGender=="Female") {
Weight=Integer.parseInt(etWeight.getText().toString());
Height=Integer.parseInt(etHeight.getText().toString());
Age=Integer.parseInt(etAge.getText().toString());
if(itemActivity=="Sedentary"){
bmr=655+((9.6*Weight)+(1.8*Height))-(4.7*Age);
tdee=bmr*1.2;
}
else if(itemActivity=="Lightly Active"){
bmr=655+((9.6*Weight)+(1.8*Height))-(4.7*Age);
tdee=bmr*1.375;
}
else if(itemActivity=="Moderalety Active"){
bmr=655+((9.6*Weight)+(1.8*Height))-(4.7*Age);
tdee=bmr*1.55;
}
else if(itemActivity=="Very Active"){
bmr=655+((9.6*Weight)+(1.8*Height))-(4.7*Age);
tdee=bmr*1.725;
}
else if(itemActivity=="Extremely Active"){
bmr=655+((9.6*Weight)+(1.8*Height))-(4.7*Age);
tdee=bmr*1.9;
}
}
result=Double.toString(tdee);
tvResult.setText(result);
}
});
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
logcat:
11-28 17:20:05.501: E/AndroidRuntime(1455): FATAL EXCEPTION: main
11-28 17:20:05.501: E/AndroidRuntime(1455): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.calculadoracalorias/com.app.calculadoracalorias.CaloriesCalculator}: java.lang.NullPointerException
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.os.Handler.dispatchMessage(Handler.java:99)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.os.Looper.loop(Looper.java:137)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-28 17:20:05.501: E/AndroidRuntime(1455): at java.lang.reflect.Method.invokeNative(Native Method)
11-28 17:20:05.501: E/AndroidRuntime(1455): at java.lang.reflect.Method.invoke(Method.java:525)
11-28 17:20:05.501: E/AndroidRuntime(1455): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-28 17:20:05.501: E/AndroidRuntime(1455): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-28 17:20:05.501: E/AndroidRuntime(1455): at dalvik.system.NativeStart.main(Native Method)
11-28 17:20:05.501: E/AndroidRuntime(1455): Caused by: java.lang.NullPointerException
11-28 17:20:05.501: E/AndroidRuntime(1455): at com.app.calculadoracalorias.CaloriesCalculator.onCreate(CaloriesCalculator.java:67)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.Activity.performCreate(Activity.java:5133)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-28 17:20:05.501: E/AndroidRuntime(1455): ... 11 more
11-28 17:24:42.341: D/AndroidRuntime(1507): Shutting down VM
11-28 17:24:42.341: W/dalvikvm(1507): threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-28 17:24:42.371: E/AndroidRuntime(1507): FATAL EXCEPTION: main
11-28 17:24:42.371: E/AndroidRuntime(1507): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.calculadoracalorias/com.app.calculadoracalorias.CaloriesCalculator}: java.lang.NullPointerException
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.os.Handler.dispatchMessage(Handler.java:99)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.os.Looper.loop(Looper.java:137)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-28 17:24:42.371: E/AndroidRuntime(1507): at java.lang.reflect.Method.invokeNative(Native Method)
11-28 17:24:42.371: E/AndroidRuntime(1507): at java.lang.reflect.Method.invoke(Method.java:525)
11-28 17:24:42.371: E/AndroidRuntime(1507): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-28 17:24:42.371: E/AndroidRuntime(1507): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-28 17:24:42.371: E/AndroidRuntime(1507): at dalvik.system.NativeStart.main(Native Method)
11-28 17:24:42.371: E/AndroidRuntime(1507): Caused by: java.lang.NullPointerException
11-28 17:24:42.371: E/AndroidRuntime(1507): at com.app.calculadoracalorias.CaloriesCalculator.onCreate(CaloriesCalculator.java:70)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.Activity.performCreate(Activity.java:5133)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-28 17:24:42.371: E/AndroidRuntime(1507): ... 11 more
Yout btnCalculate is null, you should initialize before doing the onclickListener:
btnCalculate = (Button)findViewById(R.id.btnCalculate); //your id
Also note that you are initializing the dataAdapter2 but actually you are using dataAdapert, and list2 dont have any elements, because you declare it but u are filling always list1
I'm trying to create a ListPreference in Android. But instead of creating all in XML i want to add the Entries and EntriesValues in JAVA. I have this XML:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<ListPreference android:key="delay"
android:title="#string/settings_push_delay"
android:defaultValue="Default">
</ListPreference>
</PreferenceScreen>
Then i have this class, extending PreferenceActivity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
final ListPreference listPreference = (ListPreference) findPreference("delay");
listPreference.setOnPreferenceClickListener(this);
}
public boolean onPreferenceClick(Preference preference) {
ListPreference lp = (ListPreference)preference;
String[] array={"1","2","3"};
CharSequence[] entries = array;
CharSequence[] entryValues = array;
lp.setEntries(entries);
lp.setDefaultValue("1");
lp.setEntryValues(entryValues);
return true;
}
This is just a test that i'm trying to do, so i can fully understand how to create Preferences dynamically.
Forgot to say that i've having exceptions while running this code:
04-14 00:47:10.432: E/AndroidRuntime(1330): FATAL EXCEPTION: main
04-14 00:47:10.432: E/AndroidRuntime(1330): java.lang.IllegalStateException: ListPreference requires an entries array and an entryValues array.
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.preference.ListPreference.onPrepareDialogBuilder(ListPreference.java:232)
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.preference.DialogPreference.showDialog(DialogPreference.java:293)
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.preference.DialogPreference.onClick(DialogPreference.java:264)
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.preference.Preference.performClick(Preference.java:939)
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:202)
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.widget.AbsListView$1.run(AbsListView.java:3168)
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.os.Handler.handleCallback(Handler.java:605)
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.os.Handler.dispatchMessage(Handler.java:92)
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.os.Looper.loop(Looper.java:137)
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.app.ActivityThread.main(ActivityThread.java:4424)
04-14 00:47:10.432: E/AndroidRuntime(1330): at java.lang.reflect.Method.invokeNative(Native Method)
04-14 00:47:10.432: E/AndroidRuntime(1330): at java.lang.reflect.Method.invoke(Method.java:511)
04-14 00:47:10.432: E/AndroidRuntime(1330): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-14 00:47:10.432: E/AndroidRuntime(1330): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-14 00:47:10.432: E/AndroidRuntime(1330): at dalvik.system.NativeStart.main(Native Method)
My question is, how i create a ListPreference without defining the Entries and EntryValues on the XML. How can do that in JAVA file. So, how i solve this exceptions?
Thanks,
You cannot be setting the entries and entryValues of the ListPreference in an onClick event when there are no entries and entryValues to begin with.
Instead do it in the onCreate.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
final ListPreference listPreference = (ListPreference) findPreference("delay");
String[] array={"1","2","3"};
CharSequence[] entries = array;
CharSequence[] entryValues = array;
listPreference.setEntries(entries);
listPreference.setDefaultValue("1");
listPreference.setEntryValues(entryValues);
}
Then if you need set values dynamically in onClick listener, it should work okay.
I am trying to create an application that sends a file to a server. The application consists of one button that when clicked, will upload a file to a server. Here is the code that I have so far:
public class MainActivity extends Activity implements OnClickListener {
final String TAG = "sendButton";
final String TAG2 = "messageButton";
TextView messageText;
Button uploadButton;
int serverResponseCode = 0;
String result = null;
String url = "http://192.168.1.18";
File file = new File("example.txt");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// findViewById(R.id.button2).setOnClickListener(this);
//findViewById(R.id.button2).setOnClickListener(this);
setupsendMessage();
//setupmessageButton();
uploadButton = (Button)findViewById(R.id.button1);
messageText = (TextView)findViewById(R.id.button2);
}
private void setupsendMessage() {
// do something when the button is pressed
//
Button sendButton = (Button) findViewById(R.id.button1);
sendButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
FileBody fileBody = new FileBody(file);
builder.addPart("file", fileBody);
final HttpEntity yourEntity = builder.build();
post.setEntity(yourEntity);
HttpResponse response = client.execute(post);
HttpEntity httpEntity = response.getEntity();
result = EntityUtils.toString(httpEntity);
Log.v("result", result);
}
catch(Exception e)
{
e.printStackTrace();}
Log.i(TAG, "File Sent to Server");
Toast.makeText(MainActivity.this, "File Sent to Server", Toast.LENGTH_LONG).show();
}
});
};
When I run the code the application everything appears fine, but when I press the button nothing happens and I get an error on the trace.
The error is as follows:
04-14 17:20:39.504: W/IInputConnectionWrapper(1693): showStatusIcon on inactive InputConnection
04-14 17:20:46.121: W/System.err(1693): java.net.SocketException: Permission denied
04-14 17:20:46.121: W/System.err(1693): at org.apache.harmony.luni.platform.OSNetworkSystem.socket(Native Method)
04-14 17:20:46.121: W/System.err(1693): at dalvik.system.BlockGuard$WrappedNetworkSystem.socket(BlockGuard.java:335)
04-14 17:20:46.121: W/System.err(1693): at org.apache.harmony.luni.net.PlainSocketImpl.create(PlainSocketImpl.java:216)
04-14 17:20:46.121: W/System.err(1693): at java.net.Socket.checkOpenAndCreate(Socket.java:802)
04-14 17:20:46.121: W/System.err(1693): at java.net.Socket.connect(Socket.java:948)
04-14 17:20:46.121: W/System.err(1693): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
04-14 17:20:46.121: W/System.err(1693): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143)
04-14 17:20:46.121: W/System.err(1693): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-14 17:20:46.121: W/System.err(1693): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-14 17:20:46.121: W/System.err(1693): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
04-14 17:20:46.121: W/System.err(1693): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
04-14 17:20:46.121: W/System.err(1693): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
04-14 17:20:46.121: W/System.err(1693): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
04-14 17:20:46.121: W/System.err(1693): at school.project.application.MainActivity$1.onClick(MainActivity.java:117)
04-14 17:20:46.121: W/System.err(1693): at android.view.View.performClick(View.java:2485)
04-14 17:20:46.131: W/System.err(1693): at android.view.View$PerformClick.run(View.java:9081)
04-14 17:20:46.131: W/System.err(1693): at android.os.Handler.handleCallback(Handler.java:587)
04-14 17:20:46.131: W/System.err(1693): at android.os.Handler.dispatchMessage(Handler.java:92)
04-14 17:20:46.131: W/System.err(1693): at android.os.Looper.loop(Looper.java:130)
04-14 17:20:46.131: W/System.err(1693): at android.app.ActivityThread.main(ActivityThread.java:3696)
04-14 17:20:46.131: W/System.err(1693): at java.lang.reflect.Method.invokeNative(Native Method)
04-14 17:20:46.131: W/System.err(1693): at java.lang.reflect.Method.invoke(Method.java:507)
04-14 17:20:46.131: W/System.err(1693): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
04-14 17:20:46.131: W/System.err(1693): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
04-14 17:20:46.131: W/System.err(1693): at dalvik.system.NativeStart.main(Native Method)
04-14 17:20:46.131: I/sendButton(1693): File Sent to Server
You may need to enable Internet access in the project's manifest file:
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
You need to have permission to send data via internet, Add the following line to your AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
LogCat:
05-18 13:35:31.954: INFO/ActivityManager(51): Starting activity: Intent { cmp=elf.app/.RoomInfoActivity (has extras) }
05-18 13:35:32.535: WARN/ActivityManager(51): Activity pause timeout for HistoryRecord{44e19170 elf.app/.RoomListActivity}
05-18 13:35:37.623: WARN/WindowManager(51): Key dispatching timed out sending to elf.app/elf.app.RoomListActivity
05-18 13:35:37.623: WARN/WindowManager(51): Dispatch state: null
05-18 13:35:37.634: WARN/WindowManager(51): Current state: {{null to Window{44e364d8 elf.app/elf.app.RoomListActivity paused=false} # 1305725737637 lw=null lb=null fin=true gfw=true ed=true tts=0 wf=false fp=false mcf=null}}
05-18 13:35:37.634: WARN/WindowManager(51): Continuing to wait for key to be dispatched
05-18 13:35:37.664: WARN/WindowManager(51): No window to dispatch pointer action 0
05-18 13:35:37.674: WARN/WindowManager(51): No window to dispatch pointer action 1
05-18 13:35:42.018: WARN/ActivityManager(51): Launch timeout has expired, giving up wake lock!
05-18 13:35:42.608: WARN/ActivityManager(51): Activity idle timeout for HistoryRecord{44e1eff0 elf.app/.RoomInfoActivity}
05-18 13:35:43.434: DEBUG/dalvikvm(51): threadid=15: bogus mon 1+0>0; adjusting
05-18 13:37:03.754: DEBUG/AndroidRuntime(219): Shutting down VM
05-18 13:37:03.754: WARN/dalvikvm(219): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
05-18 13:37:05.425: ERROR/AndroidRuntime(219): Uncaught handler: thread main exiting due to uncaught exception
05-18 13:37:05.554: ERROR/AndroidRuntime(219): java.lang.RuntimeException: Unable to start activity ComponentInfo{elf.app/elf.app.RoomInfoActivity}: java.lang.NullPointerException
05-18 13:37:05.554: ERROR/AndroidRuntime(219): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
05-18 13:37:05.554: ERROR/AndroidRuntime(219): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
05-18 13:37:05.554: ERROR/AndroidRuntime(219): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
05-18 13:37:05.554: ERROR/AndroidRuntime(219): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
05-18 13:37:05.554: ERROR/AndroidRuntime(219): at android.os.Handler.dispatchMessage(Handler.java:99)
05-18 13:37:05.554: ERROR/AndroidRuntime(219): at android.os.Looper.loop(Looper.java:123)
05-18 13:37:05.554: ERROR/AndroidRuntime(219): at android.app.ActivityThread.main(ActivityThread.java:4363)
05-18 13:37:05.554: ERROR/AndroidRuntime(219): at java.lang.reflect.Method.invokeNative(Native Method)
05-18 13:37:05.554: ERROR/AndroidRuntime(219): at java.lang.reflect.Method.invoke(Method.java:521)
05-18 13:37:05.554: ERROR/AndroidRuntime(219): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-18 13:37:05.554: ERROR/AndroidRuntime(219): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-18 13:37:05.554: ERROR/AndroidRuntime(219): at dalvik.system.NativeStart.main(Native Method)
05-18 13:37:05.554: ERROR/AndroidRuntime(219): Caused by: java.lang.NullPointerException
05-18 13:37:05.554: ERROR/AndroidRuntime(219): at elf.app.RoomInfoActivity.onCreate(RoomInfoActivity.java:39)
05-18 13:37:05.554: ERROR/AndroidRuntime(219): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-18 13:37:05.554: ERROR/AndroidRuntime(219): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
05-18 13:37:05.554: ERROR/AndroidRuntime(219): ... 11 more
05-18 13:37:05.724: INFO/Process(51): Sending signal. PID: 219 SIG: 3
05-18 13:37:05.736: INFO/dalvikvm(219): threadid=7: reacting to signal 3
05-18 13:37:05.736: ERROR/dalvikvm(219): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
05-18 13:37:06.025: INFO/ARMAssembler(51): generated scanline__00000077:03515104_00000000_00000000 [ 33 ipp] (47 ins) at [0x40d678:0x40d734] in 855613 ns
05-18 13:37:06.094: INFO/ARMAssembler(51): generated scanline__00000177:03515104_00001001_00000000 [ 91 ipp] (114 ins) at [0x41eee8:0x41f0b0] in 1649122 ns
Code:
package elf.app;
import elf.app.comm.CommClientMod;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
/**
* displays details about the selected room
*/
public class RoomInfoActivity extends Activity implements OnClickListener {
TextView rumInfo;
CharSequence rumInfoText;
Button buttonCleaned;
CharSequence buttonCleanedText;
TextView rumStatus;
CharSequence rumStatusText;
CommClientMod comm;
boolean cleaned = false;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.room_info);
comm = new CommClientMod("127.0.0.1", 62626);
rumInfo = (TextView)findViewById(R.id.textInfo);
buttonCleaned = (Button)findViewById(R.id.buttonCleaned);
rumStatus = (TextView)findViewById(R.id.textStatus);
rumInfoText = (CharSequence)getIntent().getExtras().getString("entry");
rumInfo.setText(rumInfoText);
buttonCleanedText = "Färdig med städningen";
buttonCleaned.setText(buttonCleanedText);
rumStatusText = (CharSequence)"Status: "+checkStatus();
rumStatus.setText(rumStatusText);
}
public void onClick(View v) {
if(v.equals(buttonCleaned)) {
if(cleaned==false) {
cleaned=true;
comm.send("skicka meddelande rummet är städat");
}
else {
cleaned=false;
comm.send("skicka meddelande rummet är inte städat");
}
}
}
public String checkStatus() {
if(cleaned==false)
return "Ej städat";
else
return "Städat";
}
}
Don't really understand why I'm getting a RuntimeException here..
if the line numbers from the code match your pastebin, it appears buttonCleaned = (Button)findViewById(R.id.buttonCleaned); is not finding that view. Does it exist in R.layout.room_info?
Just make a note that you have to put the setContentView(...) before using any component (e.g a button) belong to that view. Otherwise, the null exception will be raised.