想要在一个dialog中实现一个RGB调色板的功能,使用到3个Seekbar,各代表RGB三色。
这是ColorSelectDialog用于实现RGB调色板功能。
这是布局文件
这是在Activity中调用
这是Logcat
请问各位大神这个问题如何解决呢,我才学Android不久,对很多东西理解还不深刻,麻烦各位了。
这是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不久,对很多东西理解还不深刻,麻烦各位了。