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>

错误是:致命异常:main。无法启动活动 componentinfo (com.example.splasher/com.example.splasher.Views):android.view.InflateException: Binary XML line #7:error inflating class 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
4

2 回答 2

1

问题出现在这里:

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

有东西/在null那里(speedSpeed

推理为什么当你直接去Task而不是从Settings到时它会崩溃Task

这些静态变量似乎是Settings活动的一部分。如果您还没有访问过它,那么这些变量应该保留null,因为没有什么可以给它们任何非null. 从而造成NPE。

我建议你重新考虑你的方法,你不能让 UI 组件以这种方式依赖于变量,尤其是因为 UI 组件是通过xml.

您应该将所需的值传递给下一个 Activity 并从下一个 Activity 更新 UI,或者将值保存到持久存储中。

最后,给出有意义且唯一的变量名,speed并且Speed只有一个字母大小写,容易混淆阅读代码的任何人。

于 2013-03-18T00:27:50.130 回答
0

这里不对劲

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

你的问题来自那里。

我想你的 Settings.Speed 是空的

于 2013-03-18T00:28:09.447 回答