Reputation: 607
I have a main page with two buttons "Settings" and "task", which opens different activities. It always force closes when I am going straight to "task" activity, but it works alright if I go to "Settings" first, then come back to main menu and press "task". I am thinking that this could happen because I have some variables in "Settings" activity so they must be initiated before I go to "task". particularly it is written in the logcat that error is in 6th line of xml file, which looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.example.splasher.ScrollTextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:id="@+id/scrolltext">
</com.example.splasher.ScrollTextView>
</LinearLayout>
Error is: fatal exception: main. unable to start activity componentinfo (com.example.splasher/com.example.splasher.Views):android.view.InflateException: Binary XML line #7:error inflating class com.example.splasher.ScrollTextView Code of ScrollTextView.java is:
package com.example.splasher;
import android.content.Context;
import android.graphics.Rect;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.animation.LinearInterpolator;
import android.widget.Scroller;
import android.widget.TextView;
public class ScrollTextView extends TextView {
// scrolling feature
private Scroller mSlr;
// milliseconds for a round of scrolling
private int mRndDuration = Integer.parseInt(Settings.speed[Settings.Speed.getSelectedItemPosition()]);
// the X offset when paused
private int mXPaused = 0;
// whether it's being paused
private boolean mPaused = true;
/*
* constructor
*/
public ScrollTextView(Context context) {
this(context, null);
// customize the TextView
setSingleLine();
setEllipsize(null);
setVisibility(INVISIBLE);
}
/*
* constructor
*/
public ScrollTextView(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.textViewStyle);
// customize the TextView
setSingleLine();
setEllipsize(null);
setVisibility(INVISIBLE);
}
/*
* constructor
*/
public ScrollTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// customize the TextView
setSingleLine();
setEllipsize(null);
setVisibility(INVISIBLE);
}
/**
* begin to scroll the text from the original position
*/
public void startScroll() {
// begin from the very right side
mXPaused = -1 * getWidth();
// assume it's paused
mPaused = true;
resumeScroll();
}
/**
* resume the scroll from the pausing point
*/
public void resumeScroll() {
if (!mPaused)
return;
// Do not know why it would not scroll sometimes
// if setHorizontallyScrolling is called in constructor.
setHorizontallyScrolling(true);
// use LinearInterpolator for steady scrolling
mSlr = new Scroller(this.getContext(), new LinearInterpolator());
setScroller(mSlr);
int scrollingLen = calculateScrollingLen();
int distance = scrollingLen - (getWidth() + mXPaused);
int duration = (new Double(mRndDuration * distance * 1.00000
/ scrollingLen)).intValue();
setVisibility(VISIBLE);
mSlr.startScroll(mXPaused, 0, distance, 0, duration);
mPaused = false;
}
/**
* calculate the scrolling length of the text in pixel
*
* @return the scrolling length in pixels
*/
private int calculateScrollingLen() {
TextPaint tp = getPaint();
Rect rect = new Rect();
String strTxt = getText().toString();
tp.getTextBounds(strTxt, 0, strTxt.length(), rect);
int scrollingLen = rect.width() + getWidth();
rect = null;
return scrollingLen;
}
/**
* pause scrolling the text
*/
public void pauseScroll() {
if (null == mSlr)
return;
if (mPaused)
return;
mPaused = true;
// abortAnimation sets the current X to be the final X,
// and sets isFinished to be true
// so current position shall be saved
mXPaused = mSlr.getCurrX();
mSlr.abortAnimation();
}
@Override
/*
* override the computeScroll to restart scrolling when finished so as that
* the text is scrolled forever
*/
public void computeScroll() {
super.computeScroll();
if (null == mSlr) return;
if (mSlr.isFinished() && (!mPaused)) {
this.startScroll();
}
}
public int getRndDuration() {
return mRndDuration;
}
public void setRndDuration(int duration) {
this.mRndDuration = duration;
}
public boolean isPaused() {
return mPaused;
}
}
Any ideas what is going wrong? Whole logcatt:
03-18 00:04:26.161: E/AndroidRuntime(24081): FATAL EXCEPTION: main
03-18 00:04:26.161: E/AndroidRuntime(24081): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.splasher/com.example.splasher.Views}: android.view.InflateException: Binary XML file line #7: Error inflating class com.example.splasher.ScrollTextView
03-18 00:04:26.161: E/AndroidRuntime(24081): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1872)
03-18 00:04:26.161: E/AndroidRuntime(24081): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
03-18 00:04:26.161: E/AndroidRuntime(24081): at android.app.ActivityThread.access$1500(ActivityThread.java:135)
03-18 00:04:26.161: E/AndroidRuntime(24081): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
03-18 00:04:26.161: E/AndroidRuntime(24081): at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 00:04:26.161: E/AndroidRuntime(24081): at android.os.Looper.loop(Looper.java:150)
03-18 00:04:26.161: E/AndroidRuntime(24081): at android.app.ActivityThread.main(ActivityThread.java:4385)
03-18 00:04:26.161: E/AndroidRuntime(24081): at java.lang.reflect.Method.invokeNative(Native Method)
03-18 00:04:26.161: E/AndroidRuntime(24081): at java.lang.reflect.Method.invoke(Method.java:507)
03-18 00:04:26.161: E/AndroidRuntime(24081): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
03-18 00:04:26.161: E/AndroidRuntime(24081): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
03-18 00:04:26.161: E/AndroidRuntime(24081): at dalvik.system.NativeStart.main(Native Method)
03-18 00:04:26.161: E/AndroidRuntime(24081): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class com.example.splasher.ScrollTextView
03-18 00:04:26.161: E/AndroidRuntime(24081): at android.view.LayoutInflater.createView(LayoutInflater.java:518)
03-18 00:04:26.161: E/AndroidRuntime(24081): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
03-18 00:04:26.161: E/AndroidRuntime(24081): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
03-18 00:04:26.161: E/AndroidRuntime(24081): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
03-18 00:04:26.161: E/AndroidRuntime(24081): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
03-18 00:04:26.161: E/AndroidRuntime(24081): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
03-18 00:04:26.161: E/AndroidRuntime(24081): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:250)
03-18 00:04:26.161: E/AndroidRuntime(24081): at android.app.Activity.setContentView(Activity.java:1742)
03-18 00:04:26.161: E/AndroidRuntime(24081): at com.example.splasher.Views.onCreate(Views.java:26)
03-18 00:04:26.161: E/AndroidRuntime(24081): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
03-18 00:04:26.161: E/AndroidRuntime(24081): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836)
03-18 00:04:26.161: E/AndroidRuntime(24081): ... 11 more
03-18 00:04:26.161: E/AndroidRuntime(24081): Caused by: java.lang.reflect.InvocationTargetException
03-18 00:04:26.161: E/AndroidRuntime(24081): at java.lang.reflect.Constructor.constructNative(Native Method)
03-18 00:04:26.161: E/AndroidRuntime(24081): at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
03-18 00:04:26.161: E/AndroidRuntime(24081): at android.view.LayoutInflater.createView(LayoutInflater.java:505)
03-18 00:04:26.161: E/AndroidRuntime(24081): ... 21 more
03-18 00:04:26.161: E/AndroidRuntime(24081): Caused by: java.lang.NullPointerException
03-18 00:04:26.161: E/AndroidRuntime(24081): at com.example.splasher.ScrollTextView.<init>(ScrollTextView.java:17)
03-18 00:04:26.161: E/AndroidRuntime(24081): at com.example.splasher.ScrollTextView.<init>(ScrollTextView.java:40)
03-18 00:04:26.161: E/AndroidRuntime(24081): ... 24 more
Upvotes: 2
Views: 136
Reputation: 11310
Something is wrong here
private int mRndDuration = Integer.parseInt(Settings.speed[Settings.Speed.getSelectedItemPosition()]);
Your Probleme comes from there.
I suppose your Settings.Speed is null
Upvotes: 0
Reputation: 36449
The problem occurs here:
private int mRndDuration = Integer.parseInt(Settings.speed[Settings.Speed.getSelectedItemPosition()]);
Something(s) is/are null
there (speed
and Speed
)
Reasoning why it crashes when you go straight to Task
but not from Settings
to Task
:
Those static variables seem to be a part of the Settings
Activity. If you haven't visited it, those variables should remain null
, since there is nothing to give them anything non-null
. Thus causing the NPE.
I suggest you rethink your approach, you cannot have UI components depend on variables in this manner, espically since the UI component is being instantiated via xml
.
You should either pass the values needed to your next Activity and update the UI from the next Activity or save the values to persistent storage.
Lastly, give meanigful and unique variable names, speed
and Speed
only differ by one lettercase, easy to confuse anyone reading the code.
Upvotes: 1