NullPointerException whenever I try to switch tabs - java

I currently have an activity with 3 tabs, each containing it's own fragment. The first and second tab are relatively the same, as far as layout goes. The first tab loads fine whenever the activity loads, however when i switch to the second tab, I immediately get a NullPointerException. Here is the code for the second tab
StudentFragmentTab2
[package]
[imports]
public class StudentFragmentTab2 extends Fragment {
View view;
LinearLayout layout;
ListView listview;
String Student_JSON;
JSONArray examArray;
ExamObject exams;
ArrayList<ExamObject> list = new ArrayList<ExamObject>();
public static loginFunctions session = new loginFunctions();
AdapterView.AdapterContextMenuInfo info;
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_student_tab1, container, false);
Bundle args = getArguments();
Student_JSON = args.getString("STUDENT_JSON");
try {
examArray = new JSONArray(Student_JSON);
for (int i = 0; i < examArray.length(); i++) {
if (examArray.getJSONObject(i).getString("examTaken").equals("False")) {
exams = new ExamObject();
exams.setId(examArray.getJSONObject(i).getString("examID"));
exams.setName(examArray.getJSONObject(i).getString("examName"));
}
list.add(exams);
}
layout = (LinearLayout) view;
listview = (ListView) view.findViewById(R.id.listView1);
listview.setAdapter(new CurrentExamAdapter(list,
getActivity().getBaseContext()));
registerForContextMenu(listview);
listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
} catch (JSONException e) {
Log.w("JSON PARSING ERROR",
"Imported JSON String can't be converted to JSON object");
e.printStackTrace();
}
return view;
}
...
}
The NullPointerException points to the following class CurrentExamAdapter
CurrentExamAdapter
[package]
[imports]
public class CurrentExamAdapter extends BaseAdapter {
private ArrayList<ExamObject> data;
Context c;
public CurrentExamAdapter(ArrayList<ExamObject> datain, Context cin) {
data = datain;
c = cin;
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return data.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_current_exams, null);
}
TextView examName = (TextView) v.findViewById(R.id.EXAMNAME);
TextView examStatus = (TextView) v.findViewById(R.id.status);
ExamObject msg = data.get(position);
examName.setText(msg.EXAM_NAME); //<---LOGCAT says this is
// where the exception occurs
examStatus.setText(msg.EXAM_STATUS);
return v;
}
}
The first tab fragment is completely identical to the second, except for a different condition. What is the app trying to reference that isn't there?
EDIT
As requested, here is the stack trace:
10-26 04:28:41.529: W/IInputConnectionWrapper(21182): showStatusIcon on inactive InputConnection
10-26 04:28:44.856: I/Adreno-EGL(22413): <qeglDrvAPI_eglInitialize:381>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_KK_2.7_RB1.04.04.02.007.040_msm8960_KK_2.7_RB1_CL3869936_release_AU (CL3869936)
10-26 04:28:44.856: I/Adreno-EGL(22413): OpenGL ES Shader Compiler Version: 17.01.11.SPL
10-26 04:28:44.856: I/Adreno-EGL(22413): Build Date: 02/26/14 Wed
10-26 04:28:44.856: I/Adreno-EGL(22413): Local Branch:
10-26 04:28:44.856: I/Adreno-EGL(22413): Remote Branch: quic/kk_2.7_rb1.29
10-26 04:28:44.856: I/Adreno-EGL(22413): Local Patches: NONE
10-26 04:28:44.856: I/Adreno-EGL(22413): Reconstruct Branch: NOTHING
10-26 04:28:44.903: D/OpenGLRenderer(22413): Enabling debug mode 0
10-26 04:28:53.881: W/InputEventReceiver(22413): Attempted to finish an input event but the input event receiver has already been disposed.
10-26 04:28:59.566: D/AndroidRuntime(22413): Shutting down VM
10-26 04:28:59.566: W/dalvikvm(22413): threadid=1: thread exiting with uncaught exception (group=0x416dad40)
10-26 04:28:59.577: E/AndroidRuntime(22413): FATAL EXCEPTION: main
10-26 04:28:59.577: E/AndroidRuntime(22413): Process: com.example.cs490project, PID: 22413
10-26 04:28:59.577: E/AndroidRuntime(22413): java.lang.NullPointerException
10-26 04:28:59.577: E/AndroidRuntime(22413): at com.example.cs490project.CurrentExamAdapter.getView(CurrentExamAdapter.java:47)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.widget.AbsListView.obtainView(AbsListView.java:2255)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.widget.ListView.onMeasure(ListView.java:1175)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.View.measure(View.java:16537)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.View.measure(View.java:16537)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.View.measure(View.java:16537)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.View.measure(View.java:16537)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
10-26 04:28:59.577: E/AndroidRuntime(22413): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.View.measure(View.java:16537)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-26 04:28:59.577: E/AndroidRuntime(22413): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.View.measure(View.java:16537)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1942)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1132)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1321)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1019)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5725)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.Choreographer.doFrame(Choreographer.java:544)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.os.Handler.handleCallback(Handler.java:733)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.os.Handler.dispatchMessage(Handler.java:95)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.os.Looper.loop(Looper.java:136)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.app.ActivityThread.main(ActivityThread.java:5086)
10-26 04:28:59.577: E/AndroidRuntime(22413): at java.lang.reflect.Method.invokeNative(Native Method)
10-26 04:28:59.577: E/AndroidRuntime(22413): at java.lang.reflect.Method.invoke(Method.java:515)
10-26 04:28:59.577: E/AndroidRuntime(22413): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-26 04:28:59.577: E/AndroidRuntime(22413): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-26 04:28:59.577: E/AndroidRuntime(22413): at dalvik.system.NativeStart.main(Native Method)

The null pointer expception kept emerging from this line:
ExamObject msg = data.get(position);
If convert that piece of code to:
if(data.get(position) != null){
ExamObject msg = data.get(position);
examName.setText(msg.EXAM_NAME);
examStatus.setText(msg.EXAM_STATUS);
}
The second tab will show as it should. Strange, but I'm not complaining.

Related

Null Pointer Exception while setting value of TextView in an Array Adapter class - Android

I'm getting a NullPointerException while trying to start an Activity which contains a ListView .
In the getView method of the adapter class, the exception happens when the setText function of a textView is being called .
The code bellow is my adapter class:
public class QuestionsListAdapter extends ArrayAdapter<Question> {
Context context;
List<Question> questions;
public QuestionsListAdapter(Context context, List<Question> questions){
super(context, R.layout.list_item_question, questions);
this.context = context;
this.questions = questions;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = vi.inflate(R.layout.list_item_question, null);
Question question = questions.get(position);
TextView tv = (TextView) view.findViewById(R.id.question_list_item_string);
Log.i(TableCreator.LOG_TAG, question.toString()); //this works fine and the string is not null .
tv.setText(question.toString()+""); //NULL POINTER EXCEPTION .
return view;
}
}
As you see, I've logged the string in the logcat and it works just fine, but the next line makes the mistake .
And this is the logcat output:
05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist I/Operation Checklist﹕ |-Question-> id:1 summary:mySummary comment:myComment solution:mySolution ownerList:dummyOwner
05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist D/AndroidRuntime﹕ Shutting down VM
05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb0f5f648)
05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at org.kabiri.operationchecklist.adapter.QuestionsListAdapter.getView(QuestionsListAdapter.java:43)
at android.widget.AbsListView.obtainView(AbsListView.java:2177)
at android.widget.ListView.makeAndAddView(ListView.java:1840)
at android.widget.ListView.fillDown(ListView.java:675)
at android.widget.ListView.fillFromTop(ListView.java:736)
at android.widget.ListView.layoutChildren(ListView.java:1655)
at android.widget.AbsListView.onLayout(AbsListView.java:2012)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:502)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
The logcat shows that the error happens on this line of my adapter class:
tv.setText(question.toString()+"");
I really appreciate your help .
You already know where the problem is!
tv.setText(question.toString()+"");
is causing the NPE that means the TextView tv is null. And that means that the line
TextView tv = (TextView) view.findViewById(R.id.question_list_item_string);
is not able to find the TextView. Check the question_list_item_string id and make sure it matches the id in your list_item_question.xml file

NineOldAndroids animation not working on API>10

I am using NineOldAndroid library to perform animations. The animations work fine for API<=10. But for API>10 the app force closes. This is my code:
import static com.nineoldandroids.view.ViewPropertyAnimator.animate;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.view.MenuItem;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.Animator.AnimatorListener;
import com.nineoldandroids.animation.ObjectAnimator;
public class ActivityActualMain extends SherlockActivity {
LinearLayout container1, container2;
RelativeLayout viewTree;
ImageView image, image1, image2;
TextView tv, tv1, tv2, tv3, tv4;
ObjectAnimator anim;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final int duration = 2000;
setContentView(R.layout.activity_actual_main);
ActionBar bar = getSupportActionBar();
bar.setDisplayHomeAsUpEnabled(true);
bar.setBackgroundDrawable(getResources().getDrawable(
R.drawable.red_actionbar));
viewTree = (RelativeLayout) findViewById(R.id.viewTree);
container1 = (LinearLayout) findViewById(R.id.linearLayout1);
container2 = (LinearLayout) findViewById(R.id.linearLayout2);
image = (ImageView) findViewById(R.id.imageView1);
image1 = (ImageView) findViewById(R.id.imageView2);
image2 = (ImageView) findViewById(R.id.imageView3);
tv = (TextView) findViewById(R.id.text_tech_des);
viewTree.getViewTreeObserver().addOnGlobalLayoutListener(//to check if the layout has been placed in activity
new OnGlobalLayoutListener() {
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < 16) {
viewTree.getViewTreeObserver().removeGlobalOnLayoutListener(this);}
else{
viewTree.getViewTreeObserver()
.removeOnGlobalLayoutListener(this);
}
anim = ObjectAnimator.ofFloat(image, "y", 0f,
image.getTop());
anim.addListener(new AnimatorListener() {
#Override
public void onAnimationStart(Animator arg0) {
// TODO Auto-generated method stub
ObjectAnimator.ofFloat(tv, "alpha", 1, 0, 1)
.setDuration(duration).start();//line no 82
ObjectAnimator.ofFloat(container1, "x", 0f,
container1.getLeft()).setDuration(1000).start();
ObjectAnimator.ofFloat(container2, "x", 0f,
container2.getLeft()).setDuration(1000).start();
}
#Override
public void onAnimationRepeat(Animator arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animator arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationCancel(Animator arg0) {
// TODO Auto-generated method stub
}
});
anim.setDuration(duration).start();//line no 106
}
});
}
}
This is my stack trace:
10-26 19:23:15.203: E/AndroidRuntime(21541): FATAL EXCEPTION: main
10-26 19:23:15.203: E/AndroidRuntime(21541): java.lang.NullPointerException
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.nineoldandroids.animation.PropertyValuesHolder.setupSetterAndGetter(PropertyValuesHolder.java:523)
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.nineoldandroids.animation.ObjectAnimator.initAnimation(ObjectAnimator.java:410)
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.nineoldandroids.animation.ValueAnimator.setCurrentPlayTime(ValueAnimator.java:538)
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:928)
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:951)
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.nineoldandroids.animation.ObjectAnimator.start(ObjectAnimator.java:385)
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.vishalaksh.technex.ActivityActualMain$2$1.onAnimationStart(ActivityActualMain.java:82)
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:937)
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:951)
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.nineoldandroids.animation.ObjectAnimator.start(ObjectAnimator.java:385)
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.vishalaksh.technex.ActivityActualMain$2.onGlobalLayout(ActivityActualMain.java:106)
10-26 19:23:15.203: E/AndroidRuntime(21541): at android.view.ViewTreeObserver.dispatchOnGlobalLayout(ViewTreeObserver.java:808)
10-26 19:23:15.203: E/AndroidRuntime(21541): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1768)
10-26 19:23:15.203: E/AndroidRuntime(21541): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
10-26 19:23:15.203: E/AndroidRuntime(21541): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
10-26 19:23:15.203: E/AndroidRuntime(21541): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
10-26 19:23:15.203: E/AndroidRuntime(21541): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
10-26 19:23:15.203: E/AndroidRuntime(21541): at android.view.Choreographer.doFrame(Choreographer.java:532)
10-26 19:23:15.203: E/AndroidRuntime(21541): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
10-26 19:23:15.203: E/AndroidRuntime(21541): at android.os.Handler.handleCallback(Handler.java:730)
10-26 19:23:15.203: E/AndroidRuntime(21541): at android.os.Handler.dispatchMessage(Handler.java:92)
10-26 19:23:15.203: E/AndroidRuntime(21541): at android.os.Looper.loop(Looper.java:137)
10-26 19:23:15.203: E/AndroidRuntime(21541): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-26 19:23:15.203: E/AndroidRuntime(21541): at java.lang.reflect.Method.invokeNative(Native Method)
10-26 19:23:15.203: E/AndroidRuntime(21541): at java.lang.reflect.Method.invoke(Method.java:525)
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-26 19:23:15.203: E/AndroidRuntime(21541): at dalvik.system.NativeStart.main(Native Method)
I had an issue with this library on devices API < 10, which gave the same stacktrace. Given i was using the source code of NineOldAndroids i decided to investigate the problem inside the library.
After some tests i noticed that this error occurs because the lib tries to invoke some methods that doesn't exists on the view (since its an old API level). Searching a bit more i found the AnimatorProxy class which has a static method called "wrap". This method is used to encapsulate View objects in older Android versions emulating the presence of some animation methods as such setScaleX/Y, setTransalationX/Y.
To fix the problem i had to open the ObjectAnimator class and search for all occurrences of this line (In my library code i found 4 occurrences):
mTarget = target;
Inside that class i created the following method:
private void setTarget(Object obj) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB && obj instanceof View) {
mTarget = AnimatorProxy.wrap((View) obj);
} else {
mTarget = obj;
}
}
And replaced the line above with:
setTarget(target);
Don't know if it can fix your problem since you said that it occurs on API 10+ (the opposite of mine) but its a good point to start.

Spinner problems with Sharedpreferences

Sorry,this is my first time to put questions in this site
My Function: I set two Buttons to restore and save the information in the Spinner and EditText with the function Sharedpreferences.
I execute the program at the first time. The program will appear the error state,if I click the Button "restore" to restore the information in Spinner. But I haven't met the problem when I restore the information in EditText.
This is the code in Spinner
private Spinner.OnItemSelectedListener getfeet = new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
// TODO Auto-generated method stub
feet_out = parent.getSelectedItemPosition() + 2;
select_f = feet.getSelectedItemPosition(); //save the position you choose
Toast.makeText(MainActivity.this,
"you chose " + parent.getSelectedItem().toString(),
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
};
private Spinner.OnItemSelectedListener getinch = new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
// TODO Auto-generated method stub
inch_out = parent.getSelectedItemPosition();
select_i = inch.getSelectedItemPosition(); //save the position you choose
Toast.makeText(MainActivity.this,
"you chose " + parent.getSelectedItem().toString(),
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
};
This is the code which executes the function of save in Sharedpreferences
private void save_() {
settings = getSharedPreferences("DATA", 0);
settings.edit().putInt("DATA_FEET", select_f) //store the position in DATA_FEET and DATA_INCH
.putInt("DATA_INCH", select_i)
.putString("DATA_WEIGHT", weight.getText().toString()).commit();
Toast.makeText(MainActivity.this, R.string.done, Toast.LENGTH_SHORT) //save done
.show();
}
This is the code which executes the function of restore in Sharedpreferences
private void restore_() {
feet.setSelection(settings.getInt("DATA_FEET", select_f)); //restore the position
inch.setSelection(settings.getInt("DATA_INCH", select_i));
weight.setText(settings.getString("DATA_WEIGHT", "EMPTY"));
}
My problem is that I can't use the function of restore at the program executing at the first time. Is there any solution to solve the problem?? Because it is normal in EditText,but it is abnormal in Spinner. Thanks :))
This is the state. :))
10-23 23:14:11.677: D/TextLayoutCache(26370): Using debug level: 0 - Debug Enabled: 0
10-23 23:14:11.747: D/libEGL(26370): loaded /system/lib/egl/libGLES_android.so
10-23 23:14:11.797: D/libEGL(26370): loaded /system/lib/egl/libEGL_mali.so
10-23 23:14:11.827: D/libEGL(26370): loaded /system/lib/egl/libGLESv1_CM_mali.so
10-23 23:14:11.827: D/libEGL(26370): loaded /system/lib/egl/libGLESv2_mali.so
10-23 23:14:11.887: D/OpenGLRenderer(26370): Enabling debug mode 0
10-23 23:14:16.762: D/AndroidRuntime(26370): Shutting down VM
10-23 23:14:16.762: W/dalvikvm(26370): threadid=1: thread exiting with uncaught exception (group=0x40aaa210)
10-23 23:14:16.802: E/AndroidRuntime(26370): FATAL EXCEPTION: main
10-23 23:14:16.802: E/AndroidRuntime(26370): java.lang.NullPointerException
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.example.bmi.MainActivity.restore_(MainActivity.java:44)
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.example.bmi.MainActivity.access$1(MainActivity.java:43)
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.example.bmi.MainActivity$2.onClick(MainActivity.java:99)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.view.View.performClick(View.java:3574)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.view.View$PerformClick.run(View.java:14293)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.os.Handler.handleCallback(Handler.java:605)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.os.Handler.dispatchMessage(Handler.java:92)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.os.Looper.loop(Looper.java:137)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.app.ActivityThread.main(ActivityThread.java:4448)
10-23 23:14:16.802: E/AndroidRuntime(26370): at java.lang.reflect.Method.invokeNative(Native Method)
10-23 23:14:16.802: E/AndroidRuntime(26370): at java.lang.reflect.Method.invoke(Method.java:511)
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
10-23 23:14:16.802: E/AndroidRuntime(26370): at dalvik.system.NativeStart.main(Native Method)
This is the method to call the restore_()
private OnClickListener reback_1 = new OnClickListener() {
public void onClick(View v) {
restore_();
}
};
After I replaced the select_f and select_i into 0, it appeared the problem
10-24 00:01:30.957: D/TextLayoutCache(28836): Using debug level: 0 - Debug Enabled: 0
10-24 00:01:31.017: D/libEGL(28836): loaded /system/lib/egl/libGLES_android.so
10-24 00:01:31.037: D/libEGL(28836): loaded /system/lib/egl/libEGL_mali.so
10-24 00:01:31.057: D/libEGL(28836): loaded /system/lib/egl/libGLESv1_CM_mali.so
10-24 00:01:31.057: D/libEGL(28836): loaded /system/lib/egl/libGLESv2_mali.so
10-24 00:01:31.087: D/OpenGLRenderer(28836): Enabling debug mode 0
10-24 00:01:36.262: D/AndroidRuntime(28836): Shutting down VM
10-24 00:01:36.262: W/dalvikvm(28836): threadid=1: thread exiting with uncaught exception (group=0x40aaa210)
10-24 00:01:36.282: E/AndroidRuntime(28836): FATAL EXCEPTION: main
10-24 00:01:36.282: E/AndroidRuntime(28836): java.lang.NullPointerException
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.example.bmi.MainActivity.restore_(MainActivity.java:44)
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.example.bmi.MainActivity.access$1(MainActivity.java:43)
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.example.bmi.MainActivity$2.onClick(MainActivity.java:99)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.view.View.performClick(View.java:3574)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.view.View$PerformClick.run(View.java:14293)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.os.Handler.handleCallback(Handler.java:605)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.os.Handler.dispatchMessage(Handler.java:92)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.os.Looper.loop(Looper.java:137)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.app.ActivityThread.main(ActivityThread.java:4448)
10-24 00:01:36.282: E/AndroidRuntime(28836): at java.lang.reflect.Method.invokeNative(Native Method)
10-24 00:01:36.282: E/AndroidRuntime(28836): at java.lang.reflect.Method.invoke(Method.java:511)
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
10-24 00:01:36.282: E/AndroidRuntime(28836): at dalvik.system.NativeStart.main(Native Method)
10-24 00:01:37.803: I/Process(28836): Sending signal. PID: 28836 SIG: 9
You are using the SharedPrefs wrong.
feet.setSelection(settings.getInt("DATA_FEET", select_f));
when you try to fetch the integer-value from sharedPref, you get returned null! because i think that you misunderstand how it is done:
settings.getInt("myKey", defaultValue);
returns you the value for the key "myKey". if "myKey" has no value set, then it returns you the "defaultValue". In your case it returns the current value of "select_f". And as it looks, the value of select_f is null, when you run your application for the first time.
So you have to decide whether to initialize "select_f" before you retrieve the sharedPrefs or you enter another defaultValue here.

Adding to ListView from and EditView input

I have seen these questions and tried all the answers. Can anyone tell me what I am doing wrong here?
I have all the declarations on top, then I have my arrayadapter, then I have my enter button with onClick but when I try to input anything as soon as I click the button it crashes.
Code:
//create a list
final ListView shotList = (ListView) findViewById(R.id.shotList);
String[] values = new String[] { "6/10/13 (Attack)", "4/25/13 (Dr. Apt.)", "3/4/13 (Attack)" };
Button enterButton = (Button)findViewById(R.id.enterButt);
final EditText editText = (EditText)findViewById(R.id.enterText);
final String input = editText.getText().toString();
final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < values.length; ++i) {
list.add(values[i]);
}
final StableArrayAdapter adapter = new StableArrayAdapter(this,
android.R.layout.simple_list_item_1, list);
shotList.setAdapter(adapter);
//add data to list via enter button
enterButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
list.add(input);
adapter.notifyDataSetChanged();
//list.add(new String(editText.getText().toString()));
}
});
////////////////////////////////////////////////////////////////////////////////////
//array adapter
private class StableArrayAdapter extends ArrayAdapter<String> {
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
public StableArrayAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
for (int i = 0; i < objects.size(); ++i) {
mIdMap.put(objects.get(i), i);
}
}
#Override
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item);
}
#Override
public boolean hasStableIds() {
return true;
}
}
}
Logcat:
06-28 19:24:52.149: D/AndroidRuntime(29515): Shutting down VM
06-28 19:24:52.149: W/dalvikvm(29515): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
06-28 19:24:52.239: E/AndroidRuntime(29515): FATAL EXCEPTION: main
06-28 19:24:52.239: E/AndroidRuntime(29515): java.lang.NullPointerException
06-28 19:24:52.239: E/AndroidRuntime(29515): at com.asthmaassistant.ShotActivity$StableArrayAdapter.getItemId(ShotActivity.java:104)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.widget.AbsListView.obtainView(AbsListView.java:2180)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.widget.ListView.onMeasure(ListView.java:1158)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.view.View.measure(View.java:15518)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.view.View.measure(View.java:15518)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.view.View.measure(View.java:15518)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.view.View.measure(View.java:15518)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-28 19:24:52.239: E/AndroidRuntime(29515): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.view.View.measure(View.java:15518)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.view.Choreographer.doFrame(Choreographer.java:532)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.os.Handler.handleCallback(Handler.java:725)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.os.Handler.dispatchMessage(Handler.java:92)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.os.Looper.loop(Looper.java:137)
06-28 19:24:52.239: E/AndroidRuntime(29515): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-28 19:24:52.239: E/AndroidRuntime(29515): at java.lang.reflect.Method.invokeNative(Native Method)
06-28 19:24:52.239: E/AndroidRuntime(29515): at java.lang.reflect.Method.invoke(Method.java:511)
06-28 19:24:52.239: E/AndroidRuntime(29515): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-28 19:24:52.239: E/AndroidRuntime(29515): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-28 19:24:52.239: E/AndroidRuntime(29515): at dalvik.system.NativeStart.main(Native Method)
06-28 19:24:55.409: I/Process(29515): Sending signal. PID: 29515 SIG: 9
06-28 19:24:57.329: E/Trace(29543): error opening trace file: No such file or directory (2)
06-28 19:24:58.129: D/dalvikvm(29543): GC_FOR_ALLOC freed 71K, 8% free 2498K/2692K, paused 77ms, total 82ms
06-28 19:24:58.161: I/dalvikvm-heap(29543): Grow heap (frag case) to 3.470MB for 960016-byte allocation
06-28 19:24:58.309: D/dalvikvm(29543): GC_FOR_ALLOC freed 2K, 6% free 3433K/3632K, paused 146ms, total 146ms
06-28 19:24:58.439: D/dalvikvm(29543): GC_CONCURRENT freed <1K, 6% free 3433K/3632K, paused 16ms+19ms, total 132ms
06-28 19:24:59.479: I/Choreographer(29543): Skipped 49 frames! The application may be doing too much work on its main thread.
06-28 19:24:59.609: D/gralloc_goldfish(29543): Emulator without GPU emulation detected.
LOGCAT:
06-30 19:25:34.192: E/AndroidRuntime(9478): java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(2131230742, class android.widget.ListView) with Adapter(class com.asthmaassistant.StableArrayAdapter)]
You "input" variable is null when you are clicking the button , you are storing data into that variable inside onCreate , actually you should do it inside onClick method .
put your
String input = editText.getText().toString();
code inside onClick method.
I think the problem is the way how you store the ids , you have to pass the newly added items id to your adapter .You get ids only when constructor of your adapter gets called .
You are getting null pointer because after adding new item , you constructor is not being called and hence id for newly added item could not be found.

Why do I get these errors on my logcat whenever I run my Android app in eclipse?

I'm trying to test if my android app works, it consists of 2 activity screens. There are no errors in my codes but my app won't run. It always gives me this error on the emulator "Unfortunately 'application name' has stopped."
Here is my Activity code
public class MainActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View title = getWindow().findViewById(android.R.id.title);
View titleBar = (View) title.getParent();
titleBar.setBackgroundColor(Color.RED);
Button next=(Button)findViewById(R.id.DGButton);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent=new Intent(view.getContext(),
Activity2.class);
startActivityForResult(myIntent, 0);
}});}
public void calculateClickHandler(View view)
{
if (view.getId() == R.id.CalculateButton)
{
EditText ageText = (EditText)findViewById
(R.id.AgeField);
EditText weightText = (EditText)findViewById
(R.id.WeightField);
EditText ftText = (EditText)findViewById
(R.id.HeightField);
EditText inText = (EditText)findViewById
(R.id.HeightField2);
RadioGroup weightRG = (RadioGroup) findViewById
(R.id.WeightRG);
RadioGroup sexRG = (RadioGroup) findViewById
(R.id.SexRG);
TextView resultText = (TextView)findViewById
(R.id.ResultLabel);
TextView normalBMIText = (TextView)findViewById
(R.id.NormalBMI);
TextView idealKgText = (TextView)findViewById
(R.id.IdealKgLabel);
TextView idealLbText = (TextView)findViewById
(R.id.IdealLbLabel);
int age = Integer.parseInt(ageText.getText
().toString());
double weight = Double.parseDouble
(weightText.getText().toString());
double ftheight = Double.parseDouble(ftText.getText
().toString());
double inheight = Double.parseDouble(inText.getText
().toString());
int checkedRadioButton1 =
weightRG.getCheckedRadioButtonId();
int checkedRadioButton2 =
sexRG.getCheckedRadioButtonId();
double bmiValue = calculateBMI(weight, ftheight,
inheight, checkedRadioButton1);
String bmiInterpretation1 = interpretBMI1(bmiValue);
String bmiInterpretation2 = interpretBMI2(age);
String bmiInterpretation3 = interpretBMI3(ftheight,
inheight, checkedRadioButton2);
String bmiInterpretation4 = interpretBMI4(ftheight,
inheight, checkedRadioButton2);
resultText.setText(bmiValue + " - " +
bmiInterpretation1);
normalBMIText.setText(""+bmiInterpretation2);
idealKgText.setText(""+bmiInterpretation3);
idealLbText.setText(""+bmiInterpretation4);
Intent intent1 = new Intent(MainActivity.this,
Activity2.class);
Bundle b = new Bundle();
b.putDouble("key", bmiValue);
intent1.putExtras(b);
startActivity(intent1);
}}
This is the error logcat:
12-26 02:50:45.606: E/AndroidRuntime(1776): FATAL EXCEPTION: main
12-26 02:50:45.606: E/AndroidRuntime(1776): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bmicaldg/com.example.bmicaldg.MainActivity}: java.lang.NullPointerException
12-26 02:50:45.606: E/AndroidRuntime(1776): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-26 02:50:45.606: E/AndroidRuntime(1776): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-26 02:50:45.606: E/AndroidRuntime(1776): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-26 02:50:45.606: E/AndroidRuntime(1776): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-26 02:50:45.606: E/AndroidRuntime(1776): at android.os.Handler.dispatchMessage(Handler.java:99)
12-26 02:50:45.606: E/AndroidRuntime(1776): at android.os.Looper.loop(Looper.java:137)
12-26 02:50:45.606: E/AndroidRuntime(1776): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-26 02:50:45.606: E/AndroidRuntime(1776): at java.lang.reflect.Method.invokeNative(Native Method)
12-26 02:50:45.606: E/AndroidRuntime(1776): at java.lang.reflect.Method.invoke(Method.java:511)
12-26 02:50:45.606: E/AndroidRuntime(1776): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-26 02:50:45.606: E/AndroidRuntime(1776): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-26 02:50:45.606: E/AndroidRuntime(1776): at dalvik.system.NativeStart.main(Native Method)
12-26 02:50:45.606: E/AndroidRuntime(1776): Caused by: java.lang.NullPointerException
12-26 02:50:45.606: E/AndroidRuntime(1776): at com.example.bmicaldg.MainActivity.onCreate(MainActivity.java:25)
12-26 02:50:45.606: E/AndroidRuntime(1776): at android.app.Activity.performCreate(Activity.java:5104)
12-26 02:50:45.606: E/AndroidRuntime(1776): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-26 02:50:45.606: E/AndroidRuntime(1776): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-26 02:50:45.606: E/AndroidRuntime(1776): ... 11 more
This is the debug logcat:
12-26 03:04:06.238: I/Process(1854): Sending signal. PID: 1854 SIG: 9
12-26 03:04:12.779: W/Trace(1876): Unexpected value from nativeGetEnabledTags: 0
12-26 03:04:12.837: W/Trace(1876): Unexpected value from nativeGetEnabledTags: 0
12-26 03:04:14.866: D/dalvikvm(1876): GC_CONCURRENT freed 76K, 7% free 2723K/2916K, paused 32ms+32ms, total 270ms
12-26 03:04:15.427: D/AndroidRuntime(1876): Shutting down VM
12-26 03:04:15.456: W/dalvikvm(1876): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
12-26 03:04:15.546: E/AndroidRuntime(1876): FATAL EXCEPTION: main
12-26 03:04:15.546: E/AndroidRuntime(1876): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bmicaldg/com.example.bmicaldg.MainActivity}: java.lang.NullPointerException
12-26 03:04:15.546: E/AndroidRuntime(1876): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-26 03:04:15.546: E/AndroidRuntime(1876): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-26 03:04:15.546: E/AndroidRuntime(1876): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-26 03:04:15.546: E/AndroidRuntime(1876): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-26 03:04:15.546: E/AndroidRuntime(1876): at android.os.Handler.dispatchMessage(Handler.java:99)
12-26 03:04:15.546: E/AndroidRuntime(1876): at android.os.Looper.loop(Looper.java:137)
12-26 03:04:15.546: E/AndroidRuntime(1876): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-26 03:04:15.546: E/AndroidRuntime(1876): at java.lang.reflect.Method.invokeNative(Native Method)
12-26 03:04:15.546: E/AndroidRuntime(1876): at java.lang.reflect.Method.invoke(Method.java:511)
12-26 03:04:15.546: E/AndroidRuntime(1876): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-26 03:04:15.546: E/AndroidRuntime(1876): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-26 03:04:15.546: E/AndroidRuntime(1876): at dalvik.system.NativeStart.main(Native Method)
12-26 03:04:15.546: E/AndroidRuntime(1876): Caused by: java.lang.NullPointerException
12-26 03:04:15.546: E/AndroidRuntime(1876): at com.example.bmicaldg.MainActivity.onCreate(MainActivity.java:25)
12-26 03:04:15.546: E/AndroidRuntime(1876): at android.app.Activity.performCreate(Activity.java:5104)
12-26 03:04:15.546: E/AndroidRuntime(1876): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-26 03:04:15.546: E/AndroidRuntime(1876): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-26 03:04:15.546: E/AndroidRuntime(1876): ... 11 more
12-26 03:04:22.547: I/Process(1876): Sending signal. PID: 1876 SIG: 9
Any help is appreciated
02:50:45.606: E/AndroidRuntime(1776):
Caused by: java.lang.NullPointerException 12-26 02:50:45.606: E/AndroidRuntime(1776): at
com.example.bmicaldg.MainActivity.onCreate(MainActivity.java:25) 12-26
There is no code in your question so hard to tell how to fix, but based on stack trace, line 25 in MainActivity.java throwing NullPointerException. Code at line25 is some how resulting as null and you are trying to call some action on null reference which results in NullPointerException.
This is caused by NullPointerException. You might be accesing the null object which hasn't been initialized yet. You edit your question with your code, so that there would be chances of having my answer edited.
Caused by: java.lang.NullPointerException 12-26 02:50:45.606: E/AndroidRuntime(1776): at com.example.bmicaldg.MainActivity.onCreate(MainActivity.java:25)
This is where your error lies, line 25 of your MainActivity class. Whatever you are referencing is null.
Change your MainActivity onCreate code as:
public class MainActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View title = getWindow().findViewById(android.R.id.title);
if (title != null) {
ViewParent titleBar = title.getParent();
if (titleBar != null && (titleBar instanceof View)) {
View parentView = (View)titleBar;
parentView.setBackgroundColor(Color.RED);
}
}
// Your Code here...

Categories