2013-03-18 37 views
2

我有一個主頁面有兩個按鈕「設置」和「任務」,它打開不同的活動。當我直接進入「任務」活動時,它總是強制關閉,但如果我先進入「設置」,然後返回到主菜單並按下「任務」,它就可以正常工作。我在想,這可能會發生,因爲我在「設置」活動中有一些變量,所以必須在我進入「任務」之前啓動它們。 特別是它是寫在logcat中的錯誤是在XML文件中的第6行,它看起來像這樣:去其他活動時出錯

<?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> 

錯誤是:致命的異常:主要的。無法啓動活動componentinfo(com.example.splasher/com.example.splasher.Views):android.view.InflateException:二進制XML行#7:錯誤膨脹類com.example.splasher.ScrollTextView ScrollTextView.java的代碼是:

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; 
    } 
    } 

任何想法發生了什麼問題? 全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 
+0

後的*整個堆棧跟蹤* – 2013-03-18 00:18:47

+0

我更新了我的問題 – 2013-03-18 00:24:03

+0

檢查03-18 00:04:26.161:NPE在com.example.splasher.ScrollTextView。 (ScrollTextView.java:17) – 2013-03-18 00:25:57

回答

1

出現的問題就在這裏:

private int mRndDuration = Integer.parseInt(Settings.speed[Settings.Speed.getSelectedItemPosition()]); 

東西(S)是/是null有(speedSpeed

推理爲什麼會崩潰,當你直接去Task但不是從SettingsTask

這些靜態變量似乎是Settings活動的一部分。如果你還沒有訪問過它,那麼這些變量應該保持爲null,因爲沒有任何東西可以給它們提供任何非null。從而導致NPE。

我建議你重新考慮你的方法,你不能讓UI組件依靠這種方式的變量,特別是因爲UI組件正在通過xml實例化。

您應該將所需的值傳遞給下一個活動,並從下一活動更新UI 或將值保存到永久存儲。

最後,給出一個簡單易懂的變量名,speedSpeed只相差一個字母,容易混淆閱讀代碼的人。

+0

如果我將所有信息保存到文件中,並且在需要時從文件中提取它會有幫助嗎? – 2013-03-18 00:34:53

+0

@ L.G。是的,這將在*永久存儲*下。 – 2013-03-18 00:35:34

+0

但它會讓程序慢得多嗎? – 2013-03-18 00:36:12

0

有些事情錯在這裏

private int mRndDuration = Integer.parseInt(Settings.speed[Settings.Speed.getSelectedItemPosition()]); 

你Probleme來自那裏。

我想你Settings.Speed是空

+0

*「這裏有什麼不對勁」*不是答案。 – m0skit0 2013-03-18 00:29:09

+0

@ m0skit0我還在寫作。請耐心等待並取消您的投票 – 2013-03-18 00:30:08