簡體   English   中英

進行其他活動時出錯

[英]Error when going to another activity

我有一個帶有兩個按鈕“設置”和“任務”的主頁,打開了不同的活動。 當我直接進入“任務”活動時,它始終會強制關閉,但是如果我先轉到“設置”,然后返回主菜單並按“任務”,它就可以正常工作。 我認為可能會發生這種情況,因為我在“設置”活動中有一些變量,因此在我進入“任務”之前必須先啟動它們。 特別是在logcat中寫的錯誤是在xml文件的第六行,如下所示:

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

任何想法出了什么問題? 整個日志:

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

問題發生在這里:

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

事情(S)是/是null在那里( speedSpeed

當您直接轉到Task而不是從Settings轉到Task時為什么崩潰的原因:

這些靜態變量似乎是“ Settings活動”的一部分。 如果您尚未訪問過這些變量,則這些變量應保持為null ,因為沒有任何東西可以賦予它們非null 從而造成NPE。

我建議您重新考慮您的方法,您不能讓UI組件以這種方式依賴變量,尤其是因為UI組件是通過xml實例化的。

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

最后,給出有意義且唯一的變量名, speedSpeed僅相差一個字母大寫,容易使任何閱讀代碼的人感到困惑。

這里不對勁

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

您的問題來自那里。

我想你的Settings.Speed為null

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM