求助关于在dialog中实现多个Seekbar中找不到Listener的问题

一颗大葱 2015-05-25 06:03:42
想要在一个dialog中实现一个RGB调色板的功能,使用到3个Seekbar,各代表RGB三色。
这是ColorSelectDialog用于实现RGB调色板功能。
package com.jiang.jianbujian.modle;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.jiang.jianbujian.R;

/**
* Created by jiangjun on 2015/5/25.
*/
public class ColorSelectDialog extends AlertDialog implements OnSeekBarChangeListener
{
//定义控件
private int backgrouund;
private int color_r;
private int color_g;
private int color_b;
private View colorview; //用于颜色预览

private SeekBar seekBar_R;
private SeekBar seekBar_G;
private SeekBar seekBar_B;

private TextView show_color_R;
private TextView show_color_G;
private TextView show_color_B;

//这里应该设置protected,但如果设置为protected则Activity无法调用,如有知道为什么请告知,虚心求教
public ColorSelectDialog(Context context,final ColorSelect colorSelect) {
super(context);
View view = getLayoutInflater().inflate(R.layout.colorselect_layout, null);
//初始化dialog控件
seekBar_R = (SeekBar) findViewById(R.id.seekBar_r);
seekBar_G = (SeekBar) findViewById(R.id.seekBar_g);
seekBar_B = (SeekBar) findViewById(R.id.seekBar_b);

show_color_R = (TextView) findViewById(R.id.show_color_r);
show_color_G = (TextView) findViewById(R.id.show_color_g);
show_color_B = (TextView) findViewById(R.id.show_color_b);

colorview = findViewById(R.id.color_selecter);

setView(view);

seekBar_R.setOnSeekBarChangeListener(ColorSelectDialog.this);
seekBar_G.setOnSeekBarChangeListener(ColorSelectDialog.this);
seekBar_B.setOnSeekBarChangeListener(ColorSelectDialog.this);

//定义了一个接口用于传值
colorSelect.colorselect_R(color_r);
colorSelect.colorselect_G(color_g);
colorSelect.colorselect_B(color_b);
}


@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
switch (seekBar.getId()) {
case R.id.seekBar_r:
color_r = progress;
show_color_R.setText("R是:" + progress);
break;
case R.id.seekBar_g:
color_g = progress;
show_color_G.setText("G是:" + progress);
break;
case R.id.seekBar_b:
color_b = progress;
show_color_B.setText("B是:" + progress);
break;
}
colorselect();
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

}

//从Seekbar获取的值设置背景颜色,用colorview进行预览
protected void colorselect(){
backgrouund = Color.rgb(color_r,color_g,color_b);
colorview.setBackgroundColor(backgrouund);
}
}

这是布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/color_select_layout"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
>

<View
android:id="@+id/color_selecter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#342345"
/>
</LinearLayout>


<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="1">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text=" R "/>
<SeekBar
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:max="255"
android:id="@+id/seekBar_r" />
<TextView
android:id="@+id/show_color_r"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text=" G "/>
<SeekBar
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:max="255"
android:id="@+id/seekBar_g" />
<TextView
android:id="@+id/show_color_g"
android:layout_width="wrap_content"
android:layout_height="match_parent"

/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text=" B "/>
<SeekBar
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:max="255"
android:id="@+id/seekBar_b" />
<TextView
android:id="@+id/show_color_b"
android:layout_width="wrap_content"
android:layout_height="match_parent"

/>
</LinearLayout>




</LinearLayout>

</LinearLayout>

这是在Activity中调用
 ColorSelectDialog colorSelectDialog = new ColorSelectDialog(NoteActivity.this,NoteActivity.this);
colorSelectDialog.show();

这是Logcat
05-25 17:58:36.553  26017-26017/com.jiang.jianbujian E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.jiang.jianbujian.modle.ColorSelectDialog.<init>(ColorSelectDialog.java:50)
at com.jiang.jianbujian.activity.NoteActivity.onOptionsItemSelected(NoteActivity.java:165)
at android.app.Activity.onMenuItemSelected(Activity.java:2536)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:350)
at android.support.v7.app.ActionBarActivity.onMenuItemSelected(ActionBarActivity.java:155)
at android.support.v7.app.ActionBarActivityDelegate$1.onMenuItemSelected(ActionBarActivityDelegate.java:74)
at android.support.v7.widget.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:44)
at android.support.v7.internal.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:77)
at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:163)
at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:738)
at android.support.v7.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:802)
at android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:153)
at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:949)
at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:939)
at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:596)
at android.support.v7.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:145)
at android.view.View.performClick(View.java:4100)
at android.view.View$PerformClick.run(View.java:17016)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4813)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
at dalvik.system.NativeStart.main(Native Method)

请问各位大神这个问题如何解决呢,我才学Android不久,对很多东西理解还不深刻,麻烦各位了。
...全文
233 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
一颗大葱 2015-05-27
  • 打赏
  • 举报
回复
引用 2 楼 heaimnmn 的回复:
它报的是空指针把,控件没绑定问题
恩 是的
哎,真难 2015-05-26
  • 打赏
  • 举报
回复
它报的是空指针把,控件没绑定问题
一颗大葱 2015-05-25
  • 打赏
  • 举报
回复
我已经找到问题所在了
        //初始化dialog控件
        seekBar_R = (SeekBar) findViewById(R.id.seekBar_r);
        seekBar_G = (SeekBar) findViewById(R.id.seekBar_g);
        seekBar_B = (SeekBar) findViewById(R.id.seekBar_b);
 
        show_color_R = (TextView) findViewById(R.id.show_color_r);
        show_color_G = (TextView) findViewById(R.id.show_color_g);
        show_color_B = (TextView) findViewById(R.id.show_color_b);
 
        colorview = findViewById(R.id.color_selecter);
 
改为
        seekBar_R = (SeekBar) view.findViewById(R.id.seekBar_r);
        seekBar_G = (SeekBar) view.findViewById(R.id.seekBar_g);
        seekBar_B = (SeekBar) view.findViewById(R.id.seekBar_b);
        show_color_R = (TextView) view.findViewById(R.id.show_color_r);
        show_color_G = (TextView) view.findViewById(R.id.show_color_g);
        show_color_B = (TextView) view.findViewById(R.id.show_color_b);
        colorview = view.findViewById(R.id.color_selecter);
忘了指定view了。

80,350

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧