Can not use a Runnable - java

I'm using a Runnable as a Timer that alternatively runs a block.
I got this code from here and changed it as such:
public class TikingTimer {
private long countDownInterval;
private boolean status;
public TikingTimer(long pCountDownInterval) {
this.countDownInterval = pCountDownInterval;
status = false;
Initialize();
}
public void Stop() {
status = false;
}
public void Start() {
status = true;
}
public void Initialize()
{
final Handler handler = new Handler();
final Runnable counter = new Runnable(){
public void run(){
if(status) {
//TODO my code
} else {
handler.postDelayed(this, countDownInterval);
}
}
};
handler.postDelayed(counter, countDownInterval);
}
}
... and at onCreate method I used :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TikingTimer tt = new TikingTimer(1000);
tt.Start();
}
But when I run the project, it force-closes.
Is it wrong to use Runnable at onCreate method?
Thanks.
The LogCat stack trace is:
08-11 13:04:55.384: E/AndroidRuntime(3125): FATAL EXCEPTION: main
08-11 13:04:55.384: E/AndroidRuntime(3125): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{kawthar.taghvimdigital/kawthar.taghvimdigital.MainActivity}: java.lang.NullPointerException
08-11 13:04:55.384: E/AndroidRuntime(3125): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
08-11 13:04:55.384: E/AndroidRuntime(3125): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-11 13:04:55.384: E/AndroidRuntime(3125): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-11 13:04:55.384: E/AndroidRuntime(3125): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-11 13:04:55.384: E/AndroidRuntime(3125): at android.os.Handler.dispatchMessage(Handler.java:99)
08-11 13:04:55.384: E/AndroidRuntime(3125): at android.os.Looper.loop(Looper.java:137)
08-11 13:04:55.384: E/AndroidRuntime(3125): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-11 13:04:55.384: E/AndroidRuntime(3125): at java.lang.reflect.Method.invokeNative(Native Method)
08-11 13:04:55.384: E/AndroidRuntime(3125): at java.lang.reflect.Method.invoke(Method.java:511)
08-11 13:04:55.384: E/AndroidRuntime(3125): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-11 13:04:55.384: E/AndroidRuntime(3125): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-11 13:04:55.384: E/AndroidRuntime(3125): at dalvik.system.NativeStart.main(Native Method)
08-11 13:04:55.384: E/AndroidRuntime(3125): Caused by: java.lang.NullPointerException
08-11 13:04:55.384: E/AndroidRuntime(3125): at android.app.Activity.findViewById(Activity.java:1839)
08-11 13:04:55.384: E/AndroidRuntime(3125): at kawthar.taghvimdigital.MainActivity.<init>(MainActivity.java:14)
08-11 13:04:55.384: E/AndroidRuntime(3125): at java.lang.Class.newInstanceImpl(Native Method)
08-11 13:04:55.384: E/AndroidRuntime(3125): at java.lang.Class.newInstance(Class.java:1319)
08-11 13:04:55.384: E/AndroidRuntime(3125): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
08-11 13:04:55.384: E/AndroidRuntime(3125): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
08-11 13:04:55.384: E/AndroidRuntime(3125): ... 11 more

In your MainActivity (not posted), at line 14, you are looking up a View through method:
findViewById(Activity.java:1839)
... which is not found.
Since you manipulate this view somehow, the NullPointerException you posted is thrown.

You are trying to access a view that is not defined in your xml layout of the main activity.
Check the Line 14 on your MainActivity
This issue is not due to Runnable.

Related

My app unfortunately stopped may be due to mistakes

My app consists of level, each level has 10 question. This is my first level and first question class..
public class level1 extends ActionBarActivity {
private EditText mans1;
private TextView mcount;
int sum;
public void Onclick(View v) {
mans1 = (EditText) findViewById(R.id.ans1);
mcount = (TextView) findViewById(R.id.count);
double answer = Double.parseDouble(mans1.getText().toString());
if (answer == (8 + 7))
{
Toast.makeText(level1.this, "الإجابة صحيحة", Toast.LENGTH_LONG).show();
mcount.setText(sum++);
}
else
{
Toast.makeText(level1.this, "الإجابة خاطئة", Toast.LENGTH_LONG).show();
mcount.setText(sum);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level1);
final TextView texview = (TextView)findViewById(R.id.count);
Button sendButton = (Button)findViewById(R.id.tm);
sendButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int count = Integer.parseInt(texview.getText().toString());
Intent intent = new Intent(getApplicationContext(), level1.class);
intent.putExtra("mycount", sum);
startActivity(intent);
}
});
}
public void buttonOnClickgo(View v) {
Button next = (Button) v;
startActivity(new Intent(getApplicationContext(), level1q2.class));
}
public void buttonOnClickBack(View v) {
Button back = (Button) v;
startActivity(new Intent(getApplicationContext(), Activity2.class));
}
}
and this is the first level second question class.
public class level1q2 extends ActionBarActivity {
private EditText mans1;
private TextView mcount;
int sum;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level1q2);
TextView textView = (TextView)findViewById(R.id.count);
Intent intent = getIntent();
int count = intent.getIntExtra("mycount", 0);
textView.setText(count);}
public void Onclick(View view)
{
mans1 = (EditText) findViewById(R.id.ans1);
mcount = (TextView) findViewById(R.id.count);
double answer = Double.parseDouble(mans1.getText().toString());
if (answer == (15-3)) {
Toast.makeText(level1q2.this, "الإجابة صحيحة", Toast.LENGTH_LONG).show();
int count = getIntent().getIntExtra("mycount", 0);
mcount.setText(count++);
} else {
Toast.makeText(level1q2.this, "الإجابة خاطئة", Toast.LENGTH_LONG).show();
int count = getIntent().getIntExtra("mycount", 0);
mcount.setText(count);
}
}
public void buttonOnClickgo(View v) {
Button next = (Button) v;
startActivity(new Intent(getApplicationContext(), level1q3.class));
}
public void buttonOnClickBack(View v) {
Button back = (Button) v;
startActivity(new Intent(getApplicationContext(), Activity2.class));
}
}
I did some changes in these classes because I want to pass the count variable from first question to the second question activity.
It is was working before I edited them but now it shows "unfortunately stopped" message ..
please review the codes
After debugging ..
04:30:29.101 1075-1075/com.mainuser.math.passgrade4.passgrade4 W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb3b03ba8)
08-23 04:30:29.121 1075-1075/com.mainuser.math.passgrade4.passgrade4 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.mainuser.math.passgrade4.passgrade4, PID: 1075
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mainuser.math.passgrade4.passgrade4/com.mainuser.math.passgrade4.passgrade4.level1}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
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.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
at com.mainuser.math.passgrade4.passgrade4.level1.onCreate(level1.java:34)
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)
08-23 04:30:37.051 1075-1075/com.mainuser.math.passgrade4.passgrade4 I/Process﹕ Sending signal. PID: 1075 SIG: 9
08-23 04:30:38.061 1118-1118/com.mainuser.math.passgrade4.passgrade4 I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
08-23 04:30:38.061 1118-1118/com.mainuser.math.passgrade4.passgrade4 W/dalvikvm﹕ VFY: unable to resolve virtual method 11347: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
08-23 04:30:38.061 1118-1118/com.mainuser.math.passgrade4.passgrade4 D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
08-23 04:30:38.061 1118-1118/com.mainuser.math.passgrade4.passgrade4 I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
08-23 04:30:38.071 1118-1118/com.mainuser.math.passgrade4.passgrade4 W/dalvikvm﹕ VFY: unable to resolve virtual method 11353: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
08-23 04:30:38.071 1118-1118/com.mainuser.math.passgrade4.passgrade4 D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
08-23 04:30:38.071 1118-1118/com.mainuser.math.passgrade4.passgrade4 I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
08-23 04:30:38.071 1118-1118/com.mainuser.math.passgrade4.passgrade4 W/dalvikvm﹕ VFY: unable to resolve virtual method 9041: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
08-23 04:30:38.071 1118-1118/com.mainuser.math.passgrade4.passgrade4 D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000e
08-23 04:30:38.101 1118-1118/com.mainuser.math.passgrade4.passgrade4 I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
08-23 04:30:38.101 1118-1118/com.mainuser.math.passgrade4.passgrade4 W/dalvikvm﹕ VFY: unable to resolve virtual method 366: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
08-23 04:30:38.101 1118-1118/com.mainuser.math.passgrade4.passgrade4 D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
08-23 04:30:38.111 1118-1118/com.mainuser.math.passgrade4.passgrade4 I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
08-23 04:30:38.111 1118-1118/com.mainuser.math.passgrade4.passgrade4 W/dalvikvm﹕ VFY: unable to resolve virtual method 388: Landroid/content/res/TypedArray;.getType (I)I
08-23 04:30:38.111 1118-1118/com.mainuser.math.passgrade4.passgrade4 D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
08-23 04:30:38.591 1118-1118/com.mainuser.math.passgrade4.passgrade4 D/dalvikvm﹕ GC_FOR_ALLOC freed 125K, 7% free 2894K/3100K, paused 35ms, total 35ms
The problem is exactly what your exception says:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mainuser.math.passgrade4.passgrade4/com.mainuser.math.passgrade4.passgrade4.level1}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
...
Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
at com.mainuser.math.passgrade4.passgrade4.level1.onCreate(level1.java:34)
...
Your button is an ImageButton, but you tried to cast it to Button (which is not a superclass of ImageButton).

New Activity Problems

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.

Connection being refused while sending data from Android app to server [duplicate]

This question already has answers here:
How can I fix 'android.os.NetworkOnMainThreadException'?
(66 answers)
Closed 8 years ago.
I'm new to android application development and i want to send three parameters Latitude ,
Longitude and Bimari(which i am mentioning in
my code below) to phpMyAdmin server via internet.
The problem is that after selecting bimari from
spinner when i click send button my app crashes.
My server side is working correctly.Can anyone
please tell me why i can't send my data to server ?
Thanks alot for reading !
MainActivity.java is as follows:-
public final static String EXTRA_MESSAGE = "com.example.sushma.myfirstapp.MESSAGE";
String nn, nn1, nn2, nn3, nn4, nn5, nnf; // used to make URL
String sss1, sss2; // used to convert lat and long to string
String s; // used to save bimari as string
double lat, lng;
LocationManager lm;
String provider;
Location l;
private View view;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lm = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
Criteria c = new Criteria();
provider = lm.getBestProvider(c, false);
l = lm.getLastKnownLocation(provider);
if (l != null) {
//get latitude and longitude of the location
lng = l.getLongitude();
lat = l.getLatitude();
} else {
System.out.println("No lat/long");
}
{
LinearLayout l1 = new LinearLayout(this);
l1.setBackgroundResource(R.drawable.ambulance);
setContentView(R.layout.activity_main);
}
}
public void ongobuttonclick(View view) {
this.view = view;
Spinner sp = (Spinner) findViewById(R.id.problems);
s = sp.getSelectedItem().toString();
sss1 = String.valueOf(lng);
sss2 = String.valueOf(lat);
nn = "http://192.168.2.8/tech/new_predict.php?Latitude=";
nn1 = sss2;
nn2 = "&Longitude=";
nn3 = sss1;
nn4 = "&Bimari=";
nn5 = s;
nnf = nn + nn1 + nn2 + nn3 + nn4 + nn5;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httPost = new HttpPost(nnf);
System.out.println(nnf);
new Thread(new Runnable() {
#Override
public void run() {
try {
httpclient.execute(httPost);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
Logcat
01-26 19:32:22.859 14247-14247/com.example.sushma.myfirstapp E/ViewRootImpl﹕ sendUserActionEvent() mView == null
01-26 19:32:29.085 14247-14247/com.example.sushma.myfirstapp D/AndroidRuntime﹕ Shutting down VM
01-26 19:32:29.085 14247-14247/com.example.sushma.myfirstapp W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40da9930)
01-26 19:32:29.105 14247-14247/com.example.sushma.myfirstapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3804)
at android.view.View.performClick(View.java:4439)
at android.widget.Button.performClick(Button.java:139)
at android.view.View$PerformClick.run(View.java:18395)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5317)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3799)
at android.view.View.performClick(View.java:4439)
at android.widget.Button.performClick(Button.java:139)
at android.view.View$PerformClick.run(View.java:18395)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5317)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1128)
at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
at libcore.io.IoBridge.connect(IoBridge.java:112)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
at java.net.Socket.connect(Socket.java:842)
at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
at com.example.sushma.myfirstapp.MainActivity.ongobuttonclick(MainActivity.java:176)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3799)
at android.view.View.performClick(View.java:4439)
at android.widget.Button.performClick(Button.java:139)
at android.view.View$PerformClick.run(View.java:18395)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5317)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
NewLogcat
01-26 22:45:48.807 23318-23318/com.example.sushma.myfirstapp I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
01-26 22:45:48.807 23318-23318/com.example.sushma.myfirstapp W/dalvikvm﹕ VFY: unable to resolve virtual method 11750: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
01-26 22:45:48.867 23318-23318/com.example.sushma.myfirstapp D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
01-26 22:45:48.867 23318-23318/com.example.sushma.myfirstapp I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
01-26 22:45:48.867 23318-23318/com.example.sushma.myfirstapp W/dalvikvm﹕ VFY: unable to resolve virtual method 11756: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
01-26 22:45:48.867 23318-23318/com.example.sushma.myfirstapp D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
01-26 22:45:48.867 23318-23318/com.example.sushma.myfirstapp I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
01-26 22:45:48.867 23318-23318/com.example.sushma.myfirstapp W/dalvikvm﹕ VFY: unable to resolve virtual method 9401: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
01-26 22:45:48.867 23318-23318/com.example.sushma.myfirstapp D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000e
01-26 22:45:48.887 23318-23318/com.example.sushma.myfirstapp I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
01-26 22:45:48.887 23318-23318/com.example.sushma.myfirstapp W/dalvikvm﹕ VFY: unable to resolve virtual method 534: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
01-26 22:45:48.887 23318-23318/com.example.sushma.myfirstapp D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
01-26 22:45:48.887 23318-23318/com.example.sushma.myfirstapp I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
01-26 22:45:48.887 23318-23318/com.example.sushma.myfirstapp W/dalvikvm﹕ VFY: unable to resolve virtual method 556: Landroid/content/res/TypedArray;.getType (I)I
01-26 22:45:48.887 23318-23318/com.example.sushma.myfirstapp D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
01-26 22:45:48.947 23318-23318/com.example.sushma.myfirstapp D/dalvikvm﹕ GC_FOR_ALLOC freed 157K, 13% free 8676K/9872K, paused 24ms, total 24ms
01-26 22:45:48.957 23318-23318/com.example.sushma.myfirstapp I/dalvikvm-heap﹕ Grow heap (frag case) to 10.654MB for 1228816-byte allocation
01-26 22:45:48.967 23318-23329/com.example.sushma.myfirstapp D/dalvikvm﹕ GC_FOR_ALLOC freed 1K, 11% free 9875K/11076K, paused 14ms, total 14ms
01-26 22:45:49.007 23318-23323/com.example.sushma.myfirstapp E/dalvikvm﹕ adjustAdaptiveCoef max=4194304, min=1048576, ut=568
01-26 22:45:49.007 23318-23323/com.example.sushma.myfirstapp D/dalvikvm﹕ GC_CONCURRENT freed 4K, 11% free 9871K/11076K, paused 16ms+3ms, total 40ms
01-26 22:45:49.257 23318-23318/com.example.sushma.myfirstapp D/libEGL﹕ loaded /system/lib/egl/libGLES_hawaii.so
01-26 22:45:49.267 23318-23318/com.example.sushma.myfirstapp D/﹕ mem_init ++
01-26 22:45:49.267 23318-23318/com.example.sushma.myfirstapp D/﹕ gHwMemAllocator client 3
01-26 22:45:49.267 23318-23318/com.example.sushma.myfirstapp D/﹕ **** Using ION allocator ****
01-26 22:45:49.267 23318-23318/com.example.sushma.myfirstapp D/﹕ registered SIGUSR1[10] for pid[23318]
01-26 22:45:49.267 23318-23318/com.example.sushma.myfirstapp D/﹕ HwMemAllocatorImpl Static Counters 0 0
01-26 22:45:49.267 23318-23318/com.example.sushma.myfirstapp D/﹕ HwMemAllocatorImpl[490a9dcc] totalDeviceAllocSize[0] totalFree[0] maxFree[0] in numSlabs[0]
01-26 22:45:49.277 23318-23318/com.example.sushma.myfirstapp D/﹕ mem_init 490a9dcc--
01-26 22:45:49.277 23318-23318/com.example.sushma.myfirstapp D/ION﹕ config: version(0x10000) secure(0xf000) 256M(0x22d) fast(0x608) hwwr(0x608)
01-26 22:45:49.277 23318-23318/com.example.sushma.myfirstapp D/MM_DEVICE﹕ Waiting for mm thread to come up
01-26 22:45:49.277 23318-23346/com.example.sushma.myfirstapp D/MM_DEVICE﹕ mm_device_thread starting
01-26 22:45:49.277 23318-23318/com.example.sushma.myfirstapp D/HAWAII_EGL﹕ eglCreateContext() config: 18 context: 0x4f3c4db8, VC context 1, Thread 23318
01-26 22:45:49.277 23318-23318/com.example.sushma.myfirstapp D/HAWAII_EGL﹕ Set SWAP INTERVAL 0
01-26 22:45:49.287 23318-23318/com.example.sushma.myfirstapp D/HAWAII_EGL﹕ eglCreateWindowSurface() surface: 0x4beeea48, VC surface: 1, Thread: 23318
01-26 22:45:49.287 23318-23318/com.example.sushma.myfirstapp D/HAWAII_EGL﹕ eglMakeCurrent(0x4f3c4db8, 0x4beeea48, 0x4beeea48) Thread: 23318
01-26 22:45:49.287 23318-23318/com.example.sushma.myfirstapp D/OpenGLRenderer﹕ Enabling debug mode 0
01-26 22:45:50.849 23318-23318/com.example.sushma.myfirstapp D/AbsListView﹕ Get MotionRecognitionManager
01-26 22:45:50.959 23318-23318/com.example.sushma.myfirstapp D/HAWAII_EGL﹕ Set SWAP INTERVAL 0
01-26 22:45:50.969 23318-23318/com.example.sushma.myfirstapp D/HAWAII_EGL﹕ eglCreateWindowSurface() surface: 0x4c0f1be8, VC surface: 2, Thread: 23318
01-26 22:45:50.969 23318-23318/com.example.sushma.myfirstapp D/HAWAII_EGL﹕ eglMakeCurrent(0x4f3c4db8, 0x4c0f1be8, 0x4c0f1be8) Thread: 23318
01-26 22:45:50.989 23318-23318/com.example.sushma.myfirstapp D/AbsListView﹕ unregisterIRListener() is called
01-26 22:45:50.999 23318-23318/com.example.sushma.myfirstapp D/HAWAII_EGL﹕ eglMakeCurrent(0x4f3c4db8, 0x4beeea48, 0x4beeea48) Thread: 23318
01-26 22:45:50.999 23318-23318/com.example.sushma.myfirstapp D/AbsListView﹕ unregisterIRListener() is called
01-26 22:45:50.999 23318-23318/com.example.sushma.myfirstapp D/AbsListView﹕ unregisterIRListener() is called
01-26 22:45:50.999 23318-23318/com.example.sushma.myfirstapp D/HAWAII_EGL﹕ eglMakeCurrent(0x4f3c4db8, 0x4c0f1be8, 0x4c0f1be8) Thread: 23318
01-26 22:45:51.079 23318-23318/com.example.sushma.myfirstapp D/AbsListView﹕ unregisterIRListener() is called
01-26 22:45:51.089 23318-23318/com.example.sushma.myfirstapp D/HAWAII_EGL﹕ eglMakeCurrent(0x4f3c4db8, 0x4beeea48, 0x4beeea48) Thread: 23318
01-26 22:45:52.150 23318-23318/com.example.sushma.myfirstapp D/HAWAII_EGL﹕ eglMakeCurrent(0x4f3c4db8, 0x4c0f1be8, 0x4c0f1be8) Thread: 23318
01-26 22:45:52.791 23318-23318/com.example.sushma.myfirstapp D/AbsListView﹕ onDetachedFromWindow
01-26 22:45:52.791 23318-23318/com.example.sushma.myfirstapp D/HAWAII_EGL﹕ eglMakeCurrent(NULL) Thread: 23318
01-26 22:45:52.791 23318-23318/com.example.sushma.myfirstapp D/HAWAII_EGL﹕ eglDestroySurface() surface: 0x4c0f1be8, android window 0x4c05c8f8, Thread: 23318
01-26 22:45:52.811 23318-23318/com.example.sushma.myfirstapp D/HAWAII_EGL﹕ eglMakeCurrent(0x4f3c4db8, 0x4beeea48, 0x4beeea48) Thread: 23318
01-26 22:45:52.871 23318-23318/com.example.sushma.myfirstapp E/ViewRootImpl﹕ sendUserActionEvent() mView == null
01-26 22:45:53.802 23318-23318/com.example.sushma.myfirstapp I/System.out﹕ http://192.168.2.8/tech/new_predict.php?Latitude=31.310573780338718&Longitude=75.55498407042245&Bimari=Bones
01-26 22:45:56.945 23318-23498/com.example.sushma.myfirstapp W/System.err﹕ org.apache.http.conn.HttpHostConnectException: Connection to http://192.168.2.8 refused
01-26 22:45:56.955 23318-23498/com.example.sushma.myfirstapp W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
01-26 22:45:56.955 23318-23498/com.example.sushma.myfirstapp W/System.err﹕ at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
01-26 22:45:56.955 23318-23498/com.example.sushma.myfirstapp W/System.err﹕ at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
01-26 22:45:56.955 23318-23498/com.example.sushma.myfirstapp W/System.err﹕ at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
01-26 22:45:56.955 23318-23498/com.example.sushma.myfirstapp W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
01-26 22:45:56.955 23318-23498/com.example.sushma.myfirstapp W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
01-26 22:45:56.955 23318-23498/com.example.sushma.myfirstapp W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
01-26 22:45:56.955 23318-23498/com.example.sushma.myfirstapp W/System.err﹕ at com.example.sushma.myfirstapp.MainActivity$1.run(MainActivity.java:173)
01-26 22:45:56.955 23318-23498/com.example.sushma.myfirstapp W/System.err﹕ at java.lang.Thread.run(Thread.java:856)
01-26 22:45:56.955 23318-23498/com.example.sushma.myfirstapp W/System.err﹕ Caused by: java.net.ConnectException: failed to connect to /192.168.2.8 (port 80): connect failed: EHOSTUNREACH (No route to host)
01-26 22:45:56.965 23318-23498/com.example.sushma.myfirstapp W/System.err﹕ at libcore.io.IoBridge.connect(IoBridge.java:114)
01-26 22:45:56.965 23318-23498/com.example.sushma.myfirstapp W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
01-26 22:45:56.965 23318-23498/com.example.sushma.myfirstapp W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
01-26 22:45:56.965 23318-23498/com.example.sushma.myfirstapp W/System.err﹕ at java.net.Socket.connect(Socket.java:842)
01-26 22:45:56.965 23318-23498/com.example.sushma.myfirstapp W/System.err﹕ at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
01-26 22:45:56.965 23318-23498/com.example.sushma.myfirstapp W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
01-26 22:45:56.965 23318-23498/com.example.sushma.myfirstapp W/System.err﹕ ... 8 more
01-26 22:45:56.965 23318-23498/com.example.sushma.myfirstapp W/System.err﹕ Caused by: libcore.io.ErrnoException: connect failed: EHOSTUNREACH (No route to host)
01-26 22:45:56.965 23318-23498/com.example.sushma.myfirstapp W/System.err﹕ at libcore.io.Posix.connect(Native Method)
01-26 22:45:56.965 23318-23498/com.example.sushma.myfirstapp W/System.err﹕ at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
01-26 22:45:56.965 23318-23498/com.example.sushma.myfirstapp W/System.err﹕ at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
01-26 22:45:56.965 23318-23498/com.example.sushma.myfirstapp W/System.err﹕ at libcore.io.IoBridge.connect(IoBridge.java:112)
01-26 22:45:56.965 23318-23498/com.example.sushma.myfirstapp W/System.err﹕ ... 13 more
You tried execute httpclient.execute(httPost); method in UI thread. You can not do this.
You can use AsyncTask to resolve this issue.
public void ongobuttonclick(View view) {
...
//your code
//use AsyncTask if you want ot inform ui thread
final HttpClient httpclient = new DefaultHttpClient();
final HttpPost httPost = new HttpPost(nnf);
new AsyncTask<HttpPost, Void, HttpResponse>() {
//YourParams, YourProgress, YourResult can be Void
#Override
protected HttpResponse doInBackground(HttpPost... params) {
HttpResponse response = null;
try {
response = httpclient.execute(params[0]);
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
#Override
protected void onPostExecute(HttpResponse response) {
if(response != null){
//do sth in UI thread
}
}
}.execute(httPost);
//or in new thread if you doesn't want ot inform ui thread
new Thread(new Runnable() {
#Override
public void run() {
try {
httpclient.execute(httPost);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}

AChartEngine - Charts with data from parse.com won't display

I would like to create applications forming charts based on data from parse.com. I have read some examples and tutorials but still have problem with displaying charts. Below is my code:
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import com.parse.GetCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import java.util.ArrayList;
public class LineGraph {
public ArrayList<Integer> dataArray;
XYMultipleSeriesDataset dataset;
XYMultipleSeriesRenderer renderer;
public static boolean ClickEnabled = true;
public Intent getIntent(Context context) {
ArrayList<Integer> y = this.dataArray;
XYSeries seriesY = new XYSeries("Y");
for (int i = 0; i < y.size(); i++) {
seriesY.add(i, y.get(i));
}
dataset = new XYMultipleSeriesDataset();
dataset.addSeries(seriesY);
renderer.setPanEnabled(true, false);
renderer.setClickEnabled(ClickEnabled);
renderer.setBackgroundColor(Color.WHITE);
renderer.setApplyBackgroundColor(true);
renderer.setChartTitle("Simple data");
renderer.setAxesColor(Color.BLACK);
XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
XYSeriesRenderer renderer = new XYSeriesRenderer();
renderer.setColor(Color.RED);
renderer.setPointStyle(PointStyle.DIAMOND);
mRenderer.addSeriesRenderer(renderer);
Intent intent = ChartFactory.getLineChartIntent(context, dataset, mRenderer, "Line Graph Title");
return intent;
}
public void getData() {
ParseQuery<ParseObject> query = ParseQuery.getQuery("Counters_data");
query.getInBackground("lxFzCTeOcl", new GetCallback<ParseObject>() {
public void done(ParseObject parseObject, ParseException e) {
if (e == null) {
String object = parseObject.getString("value");
Integer objectValue = Integer.parseInt(object);
if (dataArray == null) {
dataArray = new ArrayList<Integer>();
dataArray.add(objectValue);
}
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});
}
}
And there is how I invoke charts:
public void lineGraphHandler(View view) {
LineGraph line = new LineGraph();
line.getData();
Intent lineIntent = line.getIntent(this);
startActivity(lineIntent);
}
And XML part:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/counters"
android:onClick="lineGraphHandler"
android:text="Charts"
android:id="#+id/charts"/>
There is my logcat:
03-26 08:42:13.096 1229-1229/com.example.tst D/dalvikvm﹕ Late-enabling
CheckJNI 03-26 08:42:13.487 1229-1229/com.example.tst D/libEGL﹕ loaded
/system/lib/egl/libEGL_genymotion.so 03-26 08:42:13.491
1229-1229/com.example.tst D/﹕ HostConnection::get() New Host
Connection established 0xb94f4270, tid 1229 03-26 08:42:13.551
1229-1229/com.example.tst D/libEGL﹕ loaded
/system/lib/egl/libGLESv1_CM_genymotion.so 03-26 08:42:13.551
1229-1229/com.example.tst D/libEGL﹕ loaded
/system/lib/egl/libGLESv2_genymotion.so 03-26 08:42:14.035
1229-1229/com.example.tst W/EGL_genymotion﹕ eglSurfaceAttrib not
implemented 03-26 08:42:14.039 1229-1229/com.example.tst
E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from GradienCache 03-26
08:42:14.043 1229-1229/com.example.tst E/OpenGLRenderer﹕
MAX_TEXTURE_SIZE: 4096 03-26 08:42:14.055 1229-1229/com.example.tst
E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from
Caches::initConstraints() 03-26 08:42:14.063 1229-1229/com.example.tst
E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 4096 03-26 08:42:14.063
1229-1229/com.example.tst D/OpenGLRenderer﹕ Enabling debug mode 0
03-26 08:42:50.327 1229-1229/com.example.tst D/dalvikvm﹕ GC_FOR_ALLOC
freed 200K, 8% free 2975K/3228K, paused 10ms, total 13ms 03-26
08:42:51.675 1229-1229/com.example.tst D/dalvikvm﹕ GC_FOR_ALLOC freed
431K, 14% free 3056K/3540K, paused 22ms, total 28ms 03-26 08:42:52.043
1229-1229/com.example.tst W/EGL_genymotion﹕ eglSurfaceAttrib not
implemented 03-26 08:42:53.543 1229-1229/com.example.tst
I/Choreographer﹕ Skipped 89 frames! The application may be doing too
much work on its main thread. 03-26 08:43:01.747
1229-1229/com.example.tst D/AndroidRuntime﹕ Shutting down VM 03-26
08:43:01.747 1229-1229/com.example.tst W/dalvikvm﹕ threadid=1: thread
exiting with uncaught exception (group=0xa4d8fb20) 03-26 08:43:01.767
1229-1229/com.example.tst E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.tst, PID: 1229 java.lang.IllegalStateException:
Could not execute method of the activity at
android.view.View$1.onClick(View.java:3823) at
android.view.View.performClick(View.java:4438) at
android.view.View$PerformClick.run(View.java:18422) at
android.os.Handler.handleCallback(Handler.java:733) at
android.os.Handler.dispatchMessage(Handler.java:95) 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.reflect.InvocationTargetException at
java.lang.reflect.Method.invokeNative(Native Method) at
java.lang.reflect.Method.invoke(Method.java:515) at
android.view.View$1.onClick(View.java:3818) at
android.view.View.performClick(View.java:4438) at
android.view.View$PerformClick.run(View.java:18422) at
android.os.Handler.handleCallback(Handler.java:733) at
android.os.Handler.dispatchMessage(Handler.java:95) 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.example.tst.LineGraph.getIntent(LineGraph.java:36) at
com.example.tst.MainActivity.lineGraphHandler(MainActivity.java:44)
at java.lang.reflect.Method.invokeNative(Native Method) at
java.lang.reflect.Method.invoke(Method.java:515) at
android.view.View$1.onClick(View.java:3818) at
android.view.View.performClick(View.java:4438) at
android.view.View$PerformClick.run(View.java:18422) at
android.os.Handler.handleCallback(Handler.java:733) at
android.os.Handler.dispatchMessage(Handler.java:95) 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) 03-26 08:43:04.507
1229-1229/com.example.tst I/Process﹕ Sending signal. PID: 1229 SIG: 9
I don't understand where the problem is. My app starts but crashes immediately when I push "chart" button. Is it data type of problem or because I misunderstand something?
Thank you in advance.
I tried like this but still got crash:
public void done(ParseObject parseObject, ParseException e) {
if (e == null) {
String object = parseObject.getString("value");
Integer objectValue = Integer.parseInt(object);
if (dataArray == null) {
dataArray = new ArrayList<Integer>();
dataArray.add(objectValue);
ArrayList<Integer> y = dataArray;
XYSeries seriesY = new XYSeries("Y");
for (int i = 0; i < y.size(); i++) {
seriesY.add(i, y.get(i));
dataset = new XYMultipleSeriesDataset();
dataset.addSeries(seriesY);
}
}
Your getData() retrieves the data asynchronously. dataArray won't be initialized immediately when you call getIntent().
Wait for the async operation to complete before using the data there. For example, call the code requiring that data from the done() callback.

App is force closing when i run it? Unable to start activity

When i run my app, and i press on a button it force closes. I fixed my Android manifest and i cannot find the problem. Here is the Logcat:
03-07 23:48:53.035: D/dalvikvm(5457): GC_EXTERNAL_ALLOC freed 43K, 49% free 2797K/5379K, external 1596K/2108K, paused 99ms
03-07 23:48:53.132: D/dalvikvm(5457): GC_EXTERNAL_ALLOC freed 1K, 49% free 2796K/5379K, external 3471K/4335K, paused 35ms
03-07 23:48:53.691: D/dalvikvm(5457): GC_EXTERNAL_ALLOC freed <1K, 48% free 2798K/5379K, external 5048K/5580K, paused 53ms
03-07 23:48:54.281: D/dalvikvm(5457): GC_EXTERNAL_ALLOC freed <1K, 48% free 2799K/5379K, external 8143K/8403K, paused 48ms
03-07 23:48:55.996: D/AndroidRuntime(5457): Shutting down VM
03-07 23:48:55.996: W/dalvikvm(5457): threadid=1: thread exiting with uncaught exception (group=0x40015578)
03-07 23:48:56.039: E/AndroidRuntime(5457): FATAL EXCEPTION: main
03-07 23:48:56.039: E/AndroidRuntime(5457): android.content.ActivityNotFoundException: Unable to find explicit activity class {izzy.n/izzy.n.main1}; have you declared this activity in your AndroidManifest.xml?
03-07 23:48:56.039: E/AndroidRuntime(5457): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
03-07 23:48:56.039: E/AndroidRuntime(5457): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
03-07 23:48:56.039: E/AndroidRuntime(5457): at android.app.Activity.startActivityForResult(Activity.java:2827)
03-07 23:48:56.039: E/AndroidRuntime(5457): at android.app.Activity.startActivity(Activity.java:2933)
03-07 23:48:56.039: E/AndroidRuntime(5457): at izzy.n.IzzynActivity$2.onClick(IzzynActivity.java:31)
03-07 23:48:56.039: E/AndroidRuntime(5457): at android.view.View.performClick(View.java:2538)
03-07 23:48:56.039: E/AndroidRuntime(5457): at android.view.View$PerformClick.run(View.java:9152)
03-07 23:48:56.039: E/AndroidRuntime(5457): at android.os.Handler.handleCallback(Handler.java:587)
03-07 23:48:56.039: E/AndroidRuntime(5457): at android.os.Handler.dispatchMessage(Handler.java:92)
03-07 23:48:56.039: E/AndroidRuntime(5457): at android.os.Looper.loop(Looper.java:130)
03-07 23:48:56.039: E/AndroidRuntime(5457): at android.app.ActivityThread.main(ActivityThread.java:3687)
03-07 23:48:56.039: E/AndroidRuntime(5457): at java.lang.reflect.Method.invokeNative(Native Method)
03-07 23:48:56.039: E/AndroidRuntime(5457): at java.lang.reflect.Method.invoke(Method.java:507)
03-07 23:48:56.039: E/AndroidRuntime(5457): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
03-07 23:48:56.039: E/AndroidRuntime(5457): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-07 23:48:56.039: E/AndroidRuntime(5457): at dalvik.system.NativeStart.main(Native Method)
03-07 23:50:31.398: D/dalvikvm(5553): GC_EXTERNAL_ALLOC freed 47K, 49% free 2797K/5379K, external 1596K/2108K, paused 21ms
03-07 23:50:31.457: D/dalvikvm(5553): GC_EXTERNAL_ALLOC freed 1K, 49% free 2796K/5379K, external 3471K/4335K, paused 22ms
03-07 23:50:31.660: D/dalvikvm(5553): GC_EXTERNAL_ALLOC freed <1K, 48% free 2798K/5379K, external 5048K/5580K, paused 25ms
03-07 23:50:31.753: D/dalvikvm(5553): GC_EXTERNAL_ALLOC freed <1K, 48% free 2799K/5379K, external 8143K/8403K, paused 20ms
03-07 23:50:41.910: D/AndroidRuntime(5553): Shutting down VM
03-07 23:50:41.910: W/dalvikvm(5553): threadid=1: thread exiting with uncaught exception (group=0x40015578)
03-07 23:50:41.933: E/AndroidRuntime(5553): FATAL EXCEPTION: main
03-07 23:50:41.933: E/AndroidRuntime(5553): android.content.ActivityNotFoundException: Unable to find explicit activity class {izzy.n/izzy.n.main1}; have you declared this activity in your AndroidManifest.xml?
03-07 23:50:41.933: E/AndroidRuntime(5553): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
03-07 23:50:41.933: E/AndroidRuntime(5553): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
03-07 23:50:41.933: E/AndroidRuntime(5553): at android.app.Activity.startActivityForResult(Activity.java:2827)
03-07 23:50:41.933: E/AndroidRuntime(5553): at android.app.Activity.startActivity(Activity.java:2933)
03-07 23:50:41.933: E/AndroidRuntime(5553): at izzy.n.IzzynActivity$2.onClick(IzzynActivity.java:31)
03-07 23:50:41.933: E/AndroidRuntime(5553): at android.view.View.performClick(View.java:2538)
03-07 23:50:41.933: E/AndroidRuntime(5553): at android.view.View$PerformClick.run(View.java:9152)
03-07 23:50:41.933: E/AndroidRuntime(5553): at android.os.Handler.handleCallback(Handler.java:587)
03-07 23:50:41.933: E/AndroidRuntime(5553): at android.os.Handler.dispatchMessage(Handler.java:92)
03-07 23:50:41.933: E/AndroidRuntime(5553): at android.os.Looper.loop(Looper.java:130)
03-07 23:50:41.933: E/AndroidRuntime(5553): at android.app.ActivityThread.main(ActivityThread.java:3687)
03-07 23:50:41.933: E/AndroidRuntime(5553): at java.lang.reflect.Method.invokeNative(Native Method)
03-07 23:50:41.933: E/AndroidRuntime(5553): at java.lang.reflect.Method.invoke(Method.java:507)
03-07 23:50:41.933: E/AndroidRuntime(5553): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
03-07 23:50:41.933: E/AndroidRuntime(5553): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-07 23:50:41.933: E/AndroidRuntime(5553): at dalvik.system.NativeStart.main(Native Method)
03-07 23:51:20.371: D/dalvikvm(5609): GC_EXTERNAL_ALLOC freed 48K, 49% free 2797K/5379K, external 1596K/2108K, paused 97ms
03-07 23:51:20.503: D/dalvikvm(5609): GC_EXTERNAL_ALLOC freed 1K, 49% free 2796K/5379K, external 3471K/4335K, paused 57ms
03-07 23:51:20.792: D/dalvikvm(5609): GC_EXTERNAL_ALLOC freed <1K, 48% free 2798K/5379K, external 5048K/5580K, paused 24ms
03-07 23:51:21.039: D/dalvikvm(5609): GC_EXTERNAL_ALLOC freed <1K, 48% free 2799K/5379K, external 8143K/8403K, paused 65ms
03-07 23:51:26.679: D/AndroidRuntime(5609): Shutting down VM
03-07 23:51:26.679: W/dalvikvm(5609): threadid=1: thread exiting with uncaught exception (group=0x40015578)
03-07 23:51:26.714: E/AndroidRuntime(5609): FATAL EXCEPTION: main
03-07 23:51:26.714: E/AndroidRuntime(5609): android.content.ActivityNotFoundException: Unable to find explicit activity class {izzy.n/izzy.n.main1}; have you declared this activity in your AndroidManifest.xml?
03-07 23:51:26.714: E/AndroidRuntime(5609): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
03-07 23:51:26.714: E/AndroidRuntime(5609): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
03-07 23:51:26.714: E/AndroidRuntime(5609): at android.app.Activity.startActivityForResult(Activity.java:2827)
03-07 23:51:26.714: E/AndroidRuntime(5609): at android.app.Activity.startActivity(Activity.java:2933)
03-07 23:51:26.714: E/AndroidRuntime(5609): at izzy.n.IzzynActivity$2.onClick(IzzynActivity.java:31)
03-07 23:51:26.714: E/AndroidRuntime(5609): at android.view.View.performClick(View.java:2538)
03-07 23:51:26.714: E/AndroidRuntime(5609): at android.view.View$PerformClick.run(View.java:9152)
03-07 23:51:26.714: E/AndroidRuntime(5609): at android.os.Handler.handleCallback(Handler.java:587)
03-07 23:51:26.714: E/AndroidRuntime(5609): at android.os.Handler.dispatchMessage(Handler.java:92)
03-07 23:51:26.714: E/AndroidRuntime(5609): at android.os.Looper.loop(Looper.java:130)
03-07 23:51:26.714: E/AndroidRuntime(5609): at android.app.ActivityThread.main(ActivityThread.java:3687)
03-07 23:51:26.714: E/AndroidRuntime(5609): at java.lang.reflect.Method.invokeNative(Native Method)
03-07 23:51:26.714: E/AndroidRuntime(5609): at java.lang.reflect.Method.invoke(Method.java:507)
03-07 23:51:26.714: E/AndroidRuntime(5609): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
03-07 23:51:26.714: E/AndroidRuntime(5609): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-07 23:51:26.714: E/AndroidRuntime(5609): at dalvik.system.NativeStart.main(Native Method)
03-07 23:59:24.920: D/dalvikvm(5784): GC_EXTERNAL_ALLOC freed 51K, 49% free 2797K/5379K, external 1596K/2108K, paused 54ms
03-07 23:59:25.104: D/dalvikvm(5784): GC_EXTERNAL_ALLOC freed 1K, 49% free 2796K/5379K, external 3471K/4335K, paused 46ms
03-07 23:59:25.537: D/dalvikvm(5784): GC_EXTERNAL_ALLOC freed <1K, 48% free 2798K/5379K, external 5048K/5580K, paused 24ms
03-07 23:59:25.846: D/dalvikvm(5784): GC_EXTERNAL_ALLOC freed <1K, 48% free 2799K/5379K, external 8143K/8403K, paused 38ms
03-07 23:59:41.502: D/dalvikvm(5784): GC_EXTERNAL_ALLOC freed 10K, 48% free 2828K/5379K, external 11785K/11958K, paused 25ms
03-07 23:59:41.740: D/dalvikvm(5784): GC_EXTERNAL_ALLOC freed <1K, 48% free 2829K/5379K, external 13363K/14068K, paused 25ms
03-07 23:59:41.865: D/dalvikvm(5784): GC_EXTERNAL_ALLOC freed <1K, 48% free 2830K/5379K, external 16457K/17091K, paused 20ms
03-07 23:59:42.607: D/AndroidRuntime(5784): Shutting down VM
03-07 23:59:42.607: W/dalvikvm(5784): threadid=1: thread exiting with uncaught exception (group=0x40015578)
03-07 23:59:42.783: E/AndroidRuntime(5784): FATAL EXCEPTION: main
03-07 23:59:42.783: E/AndroidRuntime(5784): java.lang.RuntimeException: Unable to start activity ComponentInfo{izzy.n/izzy.n.main1}: java.lang.NullPointerException
03-07 23:59:42.783: E/AndroidRuntime(5784): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
03-07 23:59:42.783: E/AndroidRuntime(5784): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
03-07 23:59:42.783: E/AndroidRuntime(5784): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-07 23:59:42.783: E/AndroidRuntime(5784): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
03-07 23:59:42.783: E/AndroidRuntime(5784): at android.os.Handler.dispatchMessage(Handler.java:99)
03-07 23:59:42.783: E/AndroidRuntime(5784): at android.os.Looper.loop(Looper.java:130)
03-07 23:59:42.783: E/AndroidRuntime(5784): at android.app.ActivityThread.main(ActivityThread.java:3687)
03-07 23:59:42.783: E/AndroidRuntime(5784): at java.lang.reflect.Method.invokeNative(Native Method)
03-07 23:59:42.783: E/AndroidRuntime(5784): at java.lang.reflect.Method.invoke(Method.java:507)
03-07 23:59:42.783: E/AndroidRuntime(5784): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
03-07 23:59:42.783: E/AndroidRuntime(5784): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-07 23:59:42.783: E/AndroidRuntime(5784): at dalvik.system.NativeStart.main(Native Method)
03-07 23:59:42.783: E/AndroidRuntime(5784): Caused by: java.lang.NullPointerException
03-07 23:59:42.783: E/AndroidRuntime(5784): at izzy.n.main1.populateCalendarSpinner(main1.java:62)
03-07 23:59:42.783: E/AndroidRuntime(5784): at izzy.n.main1.onCreate(main1.java:52)
03-07 23:59:42.783: E/AndroidRuntime(5784): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-07 23:59:42.783: E/AndroidRuntime(5784): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
03-07 23:59:42.783: E/AndroidRuntime(5784): ... 11 more
03-07 23:59:52.850: I/Process(5784): Sending signal. PID: 5784 SIG: 9
03-08 00:11:06.982: D/dalvikvm(6029): GC_EXTERNAL_ALLOC freed 46K, 49% free 2797K/5379K, external 1596K/2108K, paused 29ms
03-08 00:11:07.033: D/dalvikvm(6029): GC_EXTERNAL_ALLOC freed 1K, 49% free 2796K/5379K, external 3471K/4335K, paused 20ms
03-08 00:11:07.248: D/dalvikvm(6029): GC_EXTERNAL_ALLOC freed <1K, 48% free 2798K/5379K, external 5048K/5580K, paused 21ms
03-08 00:11:07.350: D/dalvikvm(6029): GC_EXTERNAL_ALLOC freed <1K, 48% free 2799K/5379K, external 8143K/8403K, paused 19ms
03-08 00:11:10.588: D/dalvikvm(6029): GC_EXTERNAL_ALLOC freed 9K, 48% free 2828K/5379K, external 11785K/11958K, paused 100ms
03-08 00:11:11.068: D/dalvikvm(6029): GC_EXTERNAL_ALLOC freed 1K, 48% free 2829K/5379K, external 13363K/14068K, paused 49ms
03-08 00:11:11.365: D/dalvikvm(6029): GC_EXTERNAL_ALLOC freed <1K, 48% free 2830K/5379K, external 16457K/17091K, paused 47ms
03-08 00:11:11.736: D/AndroidRuntime(6029): Shutting down VM
03-08 00:11:11.736: W/dalvikvm(6029): threadid=1: thread exiting with uncaught exception (group=0x40015578)
03-08 00:11:11.752: E/AndroidRuntime(6029): FATAL EXCEPTION: main
03-08 00:11:11.752: E/AndroidRuntime(6029): java.lang.RuntimeException: Unable to start activity ComponentInfo{izzy.n/izzy.n.main1}: java.lang.NullPointerException
03-08 00:11:11.752: E/AndroidRuntime(6029): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
03-08 00:11:11.752: E/AndroidRuntime(6029): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
03-08 00:11:11.752: E/AndroidRuntime(6029): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-08 00:11:11.752: E/AndroidRuntime(6029): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
03-08 00:11:11.752: E/AndroidRuntime(6029): at android.os.Handler.dispatchMessage(Handler.java:99)
03-08 00:11:11.752: E/AndroidRuntime(6029): at android.os.Looper.loop(Looper.java:130)
03-08 00:11:11.752: E/AndroidRuntime(6029): at android.app.ActivityThread.main(ActivityThread.java:3687)
03-08 00:11:11.752: E/AndroidRuntime(6029): at java.lang.reflect.Method.invokeNative(Native Method)
03-08 00:11:11.752: E/AndroidRuntime(6029): at java.lang.reflect.Method.invoke(Method.java:507)
03-08 00:11:11.752: E/AndroidRuntime(6029): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
03-08 00:11:11.752: E/AndroidRuntime(6029): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-08 00:11:11.752: E/AndroidRuntime(6029): at dalvik.system.NativeStart.main(Native Method)
03-08 00:11:11.752: E/AndroidRuntime(6029): Caused by: java.lang.NullPointerException
03-08 00:11:11.752: E/AndroidRuntime(6029): at izzy.n.main1.populateCalendarSpinner(main1.java:62)
03-08 00:11:11.752: E/AndroidRuntime(6029): at izzy.n.main1.onCreate(main1.java:52)
03-08 00:11:11.752: E/AndroidRuntime(6029): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-08 00:11:11.752: E/AndroidRuntime(6029): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
03-08 00:11:11.752: E/AndroidRuntime(6029): ... 11 more
03-08 00:16:11.924: I/Process(6029): Sending signal. PID: 6029 SIG: 9
03-08 00:16:14.525: D/dalvikvm(6173): GC_EXTERNAL_ALLOC freed 10K, 48% free 2828K/5379K, external 11785K/11958K, paused 20ms
03-08 00:16:14.678: D/dalvikvm(6173): GC_EXTERNAL_ALLOC freed 1K, 48% free 2829K/5379K, external 13363K/14068K, paused 22ms
03-08 00:16:14.779: D/dalvikvm(6173): GC_EXTERNAL_ALLOC freed <1K, 48% free 2829K/5379K, external 16457K/17091K, paused 21ms
03-08 00:16:15.037: D/AndroidRuntime(6173): Shutting down VM
03-08 00:16:15.037: W/dalvikvm(6173): threadid=1: thread exiting with uncaught exception (group=0x40015578)
03-08 00:16:15.037: E/AndroidRuntime(6173): FATAL EXCEPTION: main
03-08 00:16:15.037: E/AndroidRuntime(6173): java.lang.RuntimeException: Unable to start activity ComponentInfo{izzy.n/izzy.n.main1}: java.lang.NullPointerException
03-08 00:16:15.037: E/AndroidRuntime(6173): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
03-08 00:16:15.037: E/AndroidRuntime(6173): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
03-08 00:16:15.037: E/AndroidRuntime(6173): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-08 00:16:15.037: E/AndroidRuntime(6173): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
03-08 00:16:15.037: E/AndroidRuntime(6173): at android.os.Handler.dispatchMessage(Handler.java:99)
03-08 00:16:15.037: E/AndroidRuntime(6173): at android.os.Looper.loop(Looper.java:130)
03-08 00:16:15.037: E/AndroidRuntime(6173): at android.app.ActivityThread.main(ActivityThread.java:3687)
03-08 00:16:15.037: E/AndroidRuntime(6173): at java.lang.reflect.Method.invokeNative(Native Method)
03-08 00:16:15.037: E/AndroidRuntime(6173): at java.lang.reflect.Method.invoke(Method.java:507)
03-08 00:16:15.037: E/AndroidRuntime(6173): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
03-08 00:16:15.037: E/AndroidRuntime(6173): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-08 00:16:15.037: E/AndroidRuntime(6173): at dalvik.system.NativeStart.main(Native Method)
03-08 00:16:15.037: E/AndroidRuntime(6173): Caused by: java.lang.NullPointerException
03-08 00:16:15.037: E/AndroidRuntime(6173): at izzy.n.main1.populateCalendarSpinner(main1.java:62)
03-08 00:16:15.037: E/AndroidRuntime(6173): at izzy.n.main1.onCreate(main1.java:52)
03-08 00:16:15.037: E/AndroidRuntime(6173): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-08 00:16:15.037: E/AndroidRuntime(6173): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
03-08 00:16:15.037: E/AndroidRuntime(6173): ... 11 more
Here is the code for Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="izzy.n"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.READ_CALENDAR"></uses-permission>
<uses-permission android:name="android.permission.WRITE_CALENDAR"></uses-permission>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name="izzy.n.IzzynActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="izzy.n.notes"
android:label="#string/notes"></activity>
<activity
android:name="izzy.n.calculator"
android:label="#string/calculator"></activity>
<activity android:name="izzy.n.main1"
android:label="#string/app_name"></activity>
</application>
</manifest>
and finally here is the main1.java:
class MyCalendar {
public String name;
public String id;
public MyCalendar(String _name, String _id) {
name = _name;
id = _id;
}
#Override
public String toString() {
return name;
}
}
public class main1 extends Activity {
/*********************************************************************
* UI part*/
private Spinner m_spinner_calender;
private Button m_button_add;
private Button m_button_add2;
private Button m_button_getEvents;
private TextView m_text_event;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*get calendar list and populate the view*/
getCalendars();
populateCalendarSpinner();
populateAddBtn();
populateAddBtn2();
populateTextEvent();
populateGetEventsBtn();
}
private void populateCalendarSpinner() {
m_spinner_calender = (Spinner)this.findViewById(R.id.spinner_calendar);
ArrayAdapter l_arrayAdapter = new ArrayAdapter(this.getApplicationContext(), android.R.layout.simple_spinner_item, m_calendars);
l_arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
m_spinner_calender.setAdapter(l_arrayAdapter);
m_spinner_calender.setSelection(0);
m_spinner_calender.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> p_parent, View p_view,
int p_pos, long p_id) {
m_selectedCalendarId = m_calendars[(int)p_id].id;
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {}
});
}
private void populateAddBtn() {
m_button_add = (Button) this.findViewById(R.id.button_add);
m_button_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addEvent();
}
});
}
private void populateAddBtn2() {
m_button_add2 = (Button) this.findViewById(R.id.button_add2);
m_button_add2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addEvent2();
}
});
}
private void populateGetEventsBtn() {
m_button_getEvents = (Button) findViewById(R.id.button_get_events);
m_button_getEvents.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getLastThreeEvents();
}
});
}
private void populateTextEvent() {
m_text_event = (TextView) findViewById(R.id.text_event);
String l_str = "title: roman10 calendar tutorial test\n" +
"description: This is a simple test for calendar api\n" +
"eventLocation: #home\n" +
"start time:" + getDateTimeStr(0) + "\n" +
"end time: " + getDateTimeStr(30) + "\n" +
"event status: confirmed\n" +
"all day: no\n" +
"has alarm: yes\n";
m_text_event.setText(l_str);
}
/****************************************************************
* Data part
*/
/*retrieve a list of available calendars*/
private MyCalendar m_calendars[];
private String m_selectedCalendarId = "0";
private void getCalendars() {
String[] l_projection = new String[]{"_id", "displayName"};
Uri l_calendars;
if (Build.VERSION.SDK_INT >= 8) {
l_calendars = Uri.parse("content://com.android.calendar/calendars");
} else {
l_calendars = Uri.parse("content://calendar/calendars");
}
Cursor l_managedCursor = this.managedQuery(l_calendars, l_projection, null, null, null); //all calendars
//Cursor l_managedCursor = this.managedQuery(l_calendars, l_projection, "selected=1", null, null); //active calendars
if (l_managedCursor.moveToFirst()) {
m_calendars = new MyCalendar[l_managedCursor.getCount()];
String l_calName;
String l_calId;
int l_cnt = 0;
int l_nameCol = l_managedCursor.getColumnIndex(l_projection[1]);
int l_idCol = l_managedCursor.getColumnIndex(l_projection[0]);
do {
l_calName = l_managedCursor.getString(l_nameCol);
l_calId = l_managedCursor.getString(l_idCol);
m_calendars[l_cnt] = new MyCalendar(l_calName, l_calId);
++l_cnt;
} while (l_managedCursor.moveToNext());
}
}
/*add an event to calendar*/
private void addEvent() {
ContentValues l_event = new ContentValues();
l_event.put("calendar_id", m_selectedCalendarId);
l_event.put("title", "roman10 calendar tutorial test");
l_event.put("description", "This is a simple test for calendar api");
l_event.put("eventLocation", "#home");
l_event.put("dtstart", System.currentTimeMillis());
l_event.put("dtend", System.currentTimeMillis() + 1800*1000);
l_event.put("allDay", 0);
//status: 0~ tentative; 1~ confirmed; 2~ canceled
l_event.put("eventStatus", 1);
//0~ default; 1~ confidential; 2~ private; 3~ public
l_event.put("visibility", 0);
//0~ opaque, no timing conflict is allowed; 1~ transparency, allow overlap of scheduling
l_event.put("transparency", 0);
//0~ false; 1~ true
l_event.put("hasAlarm", 1);
Uri l_eventUri;
if (Build.VERSION.SDK_INT >= 8) {
l_eventUri = Uri.parse("content://com.android.calendar/events");
} else {
l_eventUri = Uri.parse("content://calendar/events");
}
Uri l_uri = this.getContentResolver().insert(l_eventUri, l_event);
Log.v("++++++test", l_uri.toString());
}
/*add an event through intent, this doesn't require any permission
* just send intent to android calendar
* http://www.openintents.org/en/uris*/
private void addEvent2() {
Intent l_intent = new Intent(Intent.ACTION_EDIT);
l_intent.setType("vnd.android.cursor.item/event");
//l_intent.putExtra("calendar_id", m_selectedCalendarId); //this doesn't work
l_intent.putExtra("title", "roman10 calendar tutorial test");
l_intent.putExtra("description", "This is a simple test for calendar api");
l_intent.putExtra("eventLocation", "#home");
l_intent.putExtra("beginTime", System.currentTimeMillis());
l_intent.putExtra("endTime", System.currentTimeMillis() + 1800*1000);
l_intent.putExtra("allDay", 0);
//status: 0~ tentative; 1~ confirmed; 2~ canceled
l_intent.putExtra("eventStatus", 1);
//0~ default; 1~ confidential; 2~ private; 3~ public
l_intent.putExtra("visibility", 0);
//0~ opaque, no timing conflict is allowed; 1~ transparency, allow overlap of scheduling
l_intent.putExtra("transparency", 0);
//0~ false; 1~ true
l_intent.putExtra("hasAlarm", 1);
try {
startActivity(l_intent);
} catch (Exception e) {
Toast.makeText(this.getApplicationContext(), "Sorry, no compatible calendar is found!", Toast.LENGTH_LONG).show();
}
}
/*get a list of events
* http://jimblackler.net/blog/?p=151*/
private void getLastThreeEvents() {
Uri l_eventUri;
if (Build.VERSION.SDK_INT >= 8) {
l_eventUri = Uri.parse("content://com.android.calendar/events");
} else {
l_eventUri = Uri.parse("content://calendar/events");
}
String[] l_projection = new String[]{"title", "dtstart", "dtend"};
Cursor l_managedCursor = this.managedQuery(l_eventUri, l_projection, "calendar_id=" + m_selectedCalendarId, null, "dtstart DESC, dtend DESC");
//Cursor l_managedCursor = this.managedQuery(l_eventUri, l_projection, null, null, null);
if (l_managedCursor.moveToFirst()) {
int l_cnt = 0;
String l_title;
String l_begin;
String l_end;
StringBuilder l_displayText = new StringBuilder();
int l_colTitle = l_managedCursor.getColumnIndex(l_projection[0]);
int l_colBegin = l_managedCursor.getColumnIndex(l_projection[1]);
int l_colEnd = l_managedCursor.getColumnIndex(l_projection[1]);
do {
l_title = l_managedCursor.getString(l_colTitle);
l_begin = getDateTimeStr(l_managedCursor.getString(l_colBegin));
l_end = getDateTimeStr(l_managedCursor.getString(l_colEnd));
l_displayText.append(l_title + "\n" + l_begin + "\n" + l_end + "\n----------------\n");
++l_cnt;
} while (l_managedCursor.moveToNext() && l_cnt < 3);
m_text_event.setText(l_displayText.toString());
}
}
/************************************************
* utility part
*/
private static final String DATE_TIME_FORMAT = "yyyy MMM dd, HH:mm:ss";
public static String getDateTimeStr(int p_delay_min) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT);
if (p_delay_min == 0) {
return sdf.format(cal.getTime());
} else {
Date l_time = cal.getTime();
l_time.setMinutes(l_time.getMinutes() + p_delay_min);
return sdf.format(l_time);
}
}
public static String getDateTimeStr(String p_time_in_millis) {
SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT);
Date l_time = new Date(Long.parseLong(p_time_in_millis));
return sdf.format(l_time);
}
}
Fix this:
03-07 23:50:41.933: E/AndroidRuntime(5553): android.content.ActivityNotFoundException: Unable to find explicit activity class {izzy.n/izzy.n.main1}; have you declared this activity in your AndroidManifest.xml?
ALSO
Point out which line its complaining about.
A Object in the populateCalendarSpinner() method is null . Please put try catch and debug.
Your log show
03-08 00:16:15.037: E/AndroidRuntime(6173): Caused by: java.lang.NullPointerException
03-08 00:16:15.037: E/AndroidRuntime(6173): at izzy.n.main1.populateCalendarSpinner(main1.java:62)
03-08 00:16:15.037: E/AndroidRuntime(6173): at izzy.n.main1.onCreate(main1.java:52)
Activity not found is the old log. You already fixed it.
if you're sure you declared it, make sure to clean your project. Sometimes Eclipse doesn't realize your XML has changed and is acting on older versions.
<activity android:name="main1"
android:label="#string/app_name"></activity>
should be
<activity android:name="izzy.n.main1"
android:label="#string/app_name"></activity>
<activity android:name="main1"
android:label="#string/app_name"></activity>
Try with changing this to
<activity android:name="izzy.n.main1"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Your log also says NullPointerException in populateCalendarSpinner(); debug and fix that too.
Just do this to debug:
try {
populateCalendarSpinner();
}
catch(NullPointerException e) {
e.printStackTrace();
}

Categories