Android 空指针异常?

发布于 2024-12-23 12:40:18 字数 8889 浏览 4 评论 0原文

您好,当我尝试根据 onResume 中的铃声音量当前级别设置作为首选项的 seekbar 的当前进度时,我得到以下信息:

12-31 22:02:12.559: E/AndroidRuntime(266): java.lang.RuntimeException: Unable to resume activity {com.camelCaseD.nsettings/com.camelCaseD.nsettings.Toggles}: java.lang.NullPointerException
12-31 22:02:12.559: E/AndroidRuntime(266):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2120)
12-31 22:02:12.559: E/AndroidRuntime(266):  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2135)
12-31 22:02:12.559: E/AndroidRuntime(266):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1668)
12-31 22:02:12.559: E/AndroidRuntime(266):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-31 22:02:12.559: E/AndroidRuntime(266):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-31 22:02:12.559: E/AndroidRuntime(266):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-31 22:02:12.559: E/AndroidRuntime(266):  at android.os.Looper.loop(Looper.java:123)
12-31 22:02:12.559: E/AndroidRuntime(266):  at android.app.ActivityThread.main(ActivityThread.java:3683)
12-31 22:02:12.559: E/AndroidRuntime(266):  at java.lang.reflect.Method.invokeNative(Native Method)
12-31 22:02:12.559: E/AndroidRuntime(266):  at java.lang.reflect.Method.invoke(Method.java:507)
12-31 22:02:12.559: E/AndroidRuntime(266):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-31 22:02:12.559: E/AndroidRuntime(266):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-31 22:02:12.559: E/AndroidRuntime(266):  at dalvik.system.NativeStart.main(Native Method)
12-31 22:02:12.559: E/AndroidRuntime(266): Caused by: java.lang.NullPointerException
12-31 22:02:12.559: E/AndroidRuntime(266):  at com.camelCaseD.nsettings.SeekBarPreference.setProgressC(SeekBarPreference.java:215)
12-31 22:02:12.559: E/AndroidRuntime(266):  at com.camelCaseD.nsettings.Toggles.onResume(Toggles.java:112)
12-31 22:02:12.559: E/AndroidRuntime(266):  at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150)
12-31 22:02:12.559: E/AndroidRuntime(266):  at android.app.Activity.performResume(Activity.java:3832)
12-31 22:02:12.559: E/AndroidRuntime(266):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2110)
12-31 22:02:12.559: E/AndroidRuntime(266):  ... 12 more

这是 >SeekBarPreference 类:

import com.camelCaseD.nsettings.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.preference.Preference;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;
import android.widget.TableLayout;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.media.AudioManager;

public class SeekBarPreference extends Preference implements OnSeekBarChangeListener {

    private final String TAG = getClass().getName();

    private static final int DEFAULT_VALUE = 50;

    private int mMinValue      = 0;
    private int mCurrentValue;
    private SeekBar mSeekBar;

    private AudioManager mgr=null;

    private View mView;

    private int mStream;

    public SeekBarPreference(Context context, AttributeSet attrs, int stream) {
        super(context, attrs);

        mStream = stream;
    }

    public SeekBarPreference(Context context, AttributeSet attrs, int defStyle, int stream) {
        super(context, attrs, defStyle);

        mStream = stream;
    }

    @Override
    protected View onCreateView(ViewGroup parent){

        TableLayout layout =  null;

        try {
            LayoutInflater mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            layout = (TableLayout)mInflater.inflate(R.layout.seek_bar_preference, parent, false);
        }
        catch(Exception e)
        {
            Log.e(TAG, "Error creating seek bar preference", e);
        }

        return layout;

    }

    private void initBar(SeekBar bar, final int stream, View view) {
        mgr=(AudioManager)getContext().getSystemService(Context.AUDIO_SERVICE);

        bar.setMax(mgr.getStreamMaxVolume(stream));
        bar.setProgress(mgr.getStreamVolume(stream));

        TextView mTitle = (TextView) view.findViewById(android.R.id.title);
        TextView mSummary = (TextView) view.findViewById(android.R.id.summary);

        switch(stream) {
        case AudioManager.STREAM_RING:
            mTitle.setText("Ringer Volume");
            mSummary.setText("Slide to adjust ringer volume.");
            break;

        case AudioManager.STREAM_MUSIC:
            mTitle.setText("Media Volume");
            mSummary.setText("Slide to adjust media volume.");
            break;
        }

        bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
          public void onProgressChanged(SeekBar bar, int progress,
                                        boolean fromUser) {
            mgr.setStreamVolume(stream, progress,
                                AudioManager.FLAG_PLAY_SOUND);
          }

          public void onStartTrackingTouch(SeekBar bar) {
            // no-op
          }

          public void onStopTrackingTouch(SeekBar bar) {
            // no-op
          }
        });
      }

    @Override
    public void onBindView(View view) {
        super.onBindView(view);
        try
        {
            mView = view;
            mSeekBar = (SeekBar) view.findViewById(R.id.seek_bar);
            initBar(mSeekBar, mStream, view);
        }
        catch(Exception ex) {
            Log.e(TAG, "Error binding view: " + ex.toString());
        }

        updateView(view);
    }

    /**
     * Update a SeekBarPreference view with our current state
     * @param view
     */
    protected void updateView(View view) {

        try {
            TableLayout layout = (TableLayout)view;

            mSeekBar.setProgress(mCurrentValue - mMinValue);
        }
        catch(Exception e) {
            Log.e(TAG, "Error updating seek bar preference", e);
        }

    }

    @Override 
    protected Object onGetDefaultValue(TypedArray ta, int index){

        int defaultValue = ta.getInt(index, DEFAULT_VALUE);
        return defaultValue;

    }

    @Override
    protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {

    }

    public void setProgressC(int stream) {
        mgr=(AudioManager)getContext().getSystemService(Context.AUDIO_SERVICE);
        switch(stream) {
        case AudioManager.STREAM_RING:
            SeekBar bar = (SeekBar)mView.findViewById(R.id.seek_bar);
            bar.setProgress(mgr.getStreamVolume(AudioManager.STREAM_RING));
            break;
        }
    }

}

视图类扩展了 Preference ,视图的布局如下所示:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res/com.commonsware.android.syssvc.volume"
  android:stretchColumns="1"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingLeft="20px"
  android:paddingRight="10px"
>
  <TableRow android:paddingTop="10px" 
            android:paddingBottom="2px" >
            <TextView android:id="@android:id/title" android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="22dp"
            android:typeface="sans"
            android:textStyle="normal"
            android:textColor="#ffffff" />
  </TableRow>
  <TableRow android:paddingTop="2px" 
            android:paddingBottom="2px" >
  <TextView android:id="@android:id/summary" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
  </TableRow>
  <TableRow
    android:paddingBottom="20px">
    <SeekBar
      android:id="@+id/seek_bar"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
    />
   </TableRow>  
</TableLayout>

然后在我的 PreferenceActivity 类中:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    PreferenceCategory mAudCat = (PreferenceCategory) findPreference("audCat");

    SeekBarPreference mRingerVol = new SeekBarPreference(this, null, AudioManager.STREAM_RING);
    mRingerVol.setKey("ringerVol");

    mAudCat.addPreference(mRingerVol);
}

@Override
public void onResume() {
    super.onResume();

    SeekBarPreference mRingerVol = (SeekBarPreference) findPreference("ringerVol");
    mRingerVol.setProgressC(AudioManager.STREAM_RING);
}

任何帮助将不胜感激。

Hello when I try to set the current progress of a seekbar that is a preference, based on the ringer volumes current level in onResume I get the following:

12-31 22:02:12.559: E/AndroidRuntime(266): java.lang.RuntimeException: Unable to resume activity {com.camelCaseD.nsettings/com.camelCaseD.nsettings.Toggles}: java.lang.NullPointerException
12-31 22:02:12.559: E/AndroidRuntime(266):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2120)
12-31 22:02:12.559: E/AndroidRuntime(266):  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2135)
12-31 22:02:12.559: E/AndroidRuntime(266):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1668)
12-31 22:02:12.559: E/AndroidRuntime(266):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-31 22:02:12.559: E/AndroidRuntime(266):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-31 22:02:12.559: E/AndroidRuntime(266):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-31 22:02:12.559: E/AndroidRuntime(266):  at android.os.Looper.loop(Looper.java:123)
12-31 22:02:12.559: E/AndroidRuntime(266):  at android.app.ActivityThread.main(ActivityThread.java:3683)
12-31 22:02:12.559: E/AndroidRuntime(266):  at java.lang.reflect.Method.invokeNative(Native Method)
12-31 22:02:12.559: E/AndroidRuntime(266):  at java.lang.reflect.Method.invoke(Method.java:507)
12-31 22:02:12.559: E/AndroidRuntime(266):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-31 22:02:12.559: E/AndroidRuntime(266):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-31 22:02:12.559: E/AndroidRuntime(266):  at dalvik.system.NativeStart.main(Native Method)
12-31 22:02:12.559: E/AndroidRuntime(266): Caused by: java.lang.NullPointerException
12-31 22:02:12.559: E/AndroidRuntime(266):  at com.camelCaseD.nsettings.SeekBarPreference.setProgressC(SeekBarPreference.java:215)
12-31 22:02:12.559: E/AndroidRuntime(266):  at com.camelCaseD.nsettings.Toggles.onResume(Toggles.java:112)
12-31 22:02:12.559: E/AndroidRuntime(266):  at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150)
12-31 22:02:12.559: E/AndroidRuntime(266):  at android.app.Activity.performResume(Activity.java:3832)
12-31 22:02:12.559: E/AndroidRuntime(266):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2110)
12-31 22:02:12.559: E/AndroidRuntime(266):  ... 12 more

Here is the SeekBarPreference Class:

import com.camelCaseD.nsettings.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.preference.Preference;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;
import android.widget.TableLayout;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.media.AudioManager;

public class SeekBarPreference extends Preference implements OnSeekBarChangeListener {

    private final String TAG = getClass().getName();

    private static final int DEFAULT_VALUE = 50;

    private int mMinValue      = 0;
    private int mCurrentValue;
    private SeekBar mSeekBar;

    private AudioManager mgr=null;

    private View mView;

    private int mStream;

    public SeekBarPreference(Context context, AttributeSet attrs, int stream) {
        super(context, attrs);

        mStream = stream;
    }

    public SeekBarPreference(Context context, AttributeSet attrs, int defStyle, int stream) {
        super(context, attrs, defStyle);

        mStream = stream;
    }

    @Override
    protected View onCreateView(ViewGroup parent){

        TableLayout layout =  null;

        try {
            LayoutInflater mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            layout = (TableLayout)mInflater.inflate(R.layout.seek_bar_preference, parent, false);
        }
        catch(Exception e)
        {
            Log.e(TAG, "Error creating seek bar preference", e);
        }

        return layout;

    }

    private void initBar(SeekBar bar, final int stream, View view) {
        mgr=(AudioManager)getContext().getSystemService(Context.AUDIO_SERVICE);

        bar.setMax(mgr.getStreamMaxVolume(stream));
        bar.setProgress(mgr.getStreamVolume(stream));

        TextView mTitle = (TextView) view.findViewById(android.R.id.title);
        TextView mSummary = (TextView) view.findViewById(android.R.id.summary);

        switch(stream) {
        case AudioManager.STREAM_RING:
            mTitle.setText("Ringer Volume");
            mSummary.setText("Slide to adjust ringer volume.");
            break;

        case AudioManager.STREAM_MUSIC:
            mTitle.setText("Media Volume");
            mSummary.setText("Slide to adjust media volume.");
            break;
        }

        bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
          public void onProgressChanged(SeekBar bar, int progress,
                                        boolean fromUser) {
            mgr.setStreamVolume(stream, progress,
                                AudioManager.FLAG_PLAY_SOUND);
          }

          public void onStartTrackingTouch(SeekBar bar) {
            // no-op
          }

          public void onStopTrackingTouch(SeekBar bar) {
            // no-op
          }
        });
      }

    @Override
    public void onBindView(View view) {
        super.onBindView(view);
        try
        {
            mView = view;
            mSeekBar = (SeekBar) view.findViewById(R.id.seek_bar);
            initBar(mSeekBar, mStream, view);
        }
        catch(Exception ex) {
            Log.e(TAG, "Error binding view: " + ex.toString());
        }

        updateView(view);
    }

    /**
     * Update a SeekBarPreference view with our current state
     * @param view
     */
    protected void updateView(View view) {

        try {
            TableLayout layout = (TableLayout)view;

            mSeekBar.setProgress(mCurrentValue - mMinValue);
        }
        catch(Exception e) {
            Log.e(TAG, "Error updating seek bar preference", e);
        }

    }

    @Override 
    protected Object onGetDefaultValue(TypedArray ta, int index){

        int defaultValue = ta.getInt(index, DEFAULT_VALUE);
        return defaultValue;

    }

    @Override
    protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {

    }

    public void setProgressC(int stream) {
        mgr=(AudioManager)getContext().getSystemService(Context.AUDIO_SERVICE);
        switch(stream) {
        case AudioManager.STREAM_RING:
            SeekBar bar = (SeekBar)mView.findViewById(R.id.seek_bar);
            bar.setProgress(mgr.getStreamVolume(AudioManager.STREAM_RING));
            break;
        }
    }

}

The view class extends Preference and the layout of the view is shown below:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res/com.commonsware.android.syssvc.volume"
  android:stretchColumns="1"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingLeft="20px"
  android:paddingRight="10px"
>
  <TableRow android:paddingTop="10px" 
            android:paddingBottom="2px" >
            <TextView android:id="@android:id/title" android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="22dp"
            android:typeface="sans"
            android:textStyle="normal"
            android:textColor="#ffffff" />
  </TableRow>
  <TableRow android:paddingTop="2px" 
            android:paddingBottom="2px" >
  <TextView android:id="@android:id/summary" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
  </TableRow>
  <TableRow
    android:paddingBottom="20px">
    <SeekBar
      android:id="@+id/seek_bar"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
    />
   </TableRow>  
</TableLayout>

Then inside my PreferenceActivity class:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    PreferenceCategory mAudCat = (PreferenceCategory) findPreference("audCat");

    SeekBarPreference mRingerVol = new SeekBarPreference(this, null, AudioManager.STREAM_RING);
    mRingerVol.setKey("ringerVol");

    mAudCat.addPreference(mRingerVol);
}

@Override
public void onResume() {
    super.onResume();

    SeekBarPreference mRingerVol = (SeekBarPreference) findPreference("ringerVol");
    mRingerVol.setProgressC(AudioManager.STREAM_RING);
}

Any help will be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晒暮凉 2024-12-30 12:40:18

我如何找出错误的根源。

setProgressC 中,当我将 onBindView 中的变量 mView 的值和 setProgressC 本身输出到 LogCat 中后,变量 mView 为 null 。

How I have figured the source of the error.

In setProgressC the variable mView is null after I outputted the value of this variable in onBindView and setProgressC itself into LogCat.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文