在try-catch循环中获取我的代码。它在我的手机上运行良好(三星Galaxy S3,但在Eclipse模拟器中出现运行时错误。
在我为方法添加颜色设置并使用GradientDrawable作为我的按钮的背景后出现错误(即使这似乎在其他位置工作正常)。
我完全迷失在这里......请看看,谢谢!!
生成错误的代码 注意:我在"上放置一个断点,返回super.onContextItemSelected(item);",在return语句之后/之后生成运行时错误。
@SuppressLint("NewApi") @SuppressWarnings("deprecation")
@Override
public boolean onContextItemSelected(MenuItem item) {
int timerLocation = findTimer(item.getGroupId());
if (timerLocation > -1)
{
final MyCountDownTimer selectedFrag = this.myTimersList.get(timerLocation).fragmentRef;
switch (item.getItemId()) {
case R.id.timer_settings:
// Open timer specific options menu:
defaultSettingsMenu(selectedFrag);
return true;
default:
return super.onContextItemSelected(item);
}
}
else timerClickScreen.setText("ERROR: timer not found: " + String.valueOf(item.getGroupId()) + ", "+ String.valueOf(timerLocation));
return super.onContextItemSelected(item);
}
主要方法:
private void defaultSettingsMenu(MyCountDownTimer selectedFragIN)
{
try
{
final MyCountDownTimer selectedFrag = selectedFragIN;
final Dialog dialog_settings = new Dialog(this);
dialog_settings.setContentView(R.layout.timer_set_options);
if (selectedFrag == null) dialog_settings.setTitle(R.string.default_settings_for_timer);
else dialog_settings.setTitle(R.string.timer_settings);
if (selectedFrag == null)
{
// Set title color:
dialog_settings.getWindow().setTitleColor(getDefaultColor());
// Set divider color:
int dividerTop = dialog_settings.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
View divider1 = dialog_settings.findViewById(dividerTop);
divider1.setBackgroundColor(getDefaultColor());
}
else
{
// Set title color:
dialog_settings.getWindow().setTitleColor(selectedFrag.getTimerColor());
// Set divider color:
int dividerTop = dialog_settings.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
View divider1 = dialog_settings.findViewById(dividerTop);
divider1.setBackgroundColor(selectedFrag.getTimerColor());
}
final TextView[] settingsItems = new TextView[8];
settingsItems[0]= (TextView) dialog_settings.findViewById(R.id.timerSettings_item1);
settingsItems[1]= (TextView) dialog_settings.findViewById(R.id.timerSettings_item2);
...
final Spinner[] settingsSetting = new Spinner[7];
settingsSetting[0]= (Spinner) dialog_settings.findViewById(R.id.timerSettings_setting1);
settingsSetting[1]= (Spinner) dialog_settings.findViewById(R.id.timerSettings_setting2);
...
settingsItems[0].setText(R.string.settings_title1);
settingsItems[1].setText(R.string.settings_title2);
// Give some padding on left side:
for (int i = 0; i<8; i++) settingsItems[i].setPadding(10, 0, 0, 0);
// Create menu items:
final String[] items1 = new String[]{getString(R.string.settings_menu1_item1), getString(R.string.settings_menu1_item2), getString(R.string.settings_menu1_item3), getString(R.string.settings_menu1_item4)};
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items1);
settingsSetting[0].setAdapter(adapter1);
if (selectedFrag == null) settingsSetting[0].setSelection(defaultTimerSettings.OnTapResponseValue.ordinal());
else settingsSetting[0].setSelection(selectedFrag.getTapBehavior().ordinal());
<... MORE SAME CODE HERE ...>
SpinnerColorSelector mySpinnerColorSelector = new SpinnerColorSelector(this);
settingsSetting[6].setAdapter(mySpinnerColorSelector);
if (selectedFrag == null) settingsSetting[6].setSelection(findIntArraylist(mySpinnerColorSelector.colors, defaultTimerSettings.colorTimer));
else settingsSetting[6].setSelection(findIntArraylist(mySpinnerColorSelector.colors,selectedFrag.getTimerColor()));
dialog_settings.show();
Button cancelButtonOptions = (Button) dialog_settings.findViewById(R.id.buttonTimerCancelOptions);
int[] gradientColors = new int[3];
if (selectedFrag == null) gradientColors = generateGradientColors(getDefaultColor());
else gradientColors = generateGradientColors(selectedFrag.getTimerColor());
if (android.os.Build.VERSION.SDK_INT >= 16) {
GradientDrawable gdDefault = new GradientDrawable();
gdDefault.setColors(gradientColors);
gdDefault.setCornerRadius(10);
gdDefault.setStroke(1, Color.GRAY);
cancelButtonOptions.setBackground(gdDefault);
} else {
// Fallback for APIs under 16.
GradientDrawable newGdDefault = new GradientDrawable(Orientation.BOTTOM_TOP, gradientColors);
newGdDefault.setCornerRadius(10);
newGdDefault.setStroke(1, Color.GRAY);
cancelButtonOptions.setBackgroundDrawable(newGdDefault);
}
// if decline button is clicked, close the custom dialog
cancelButtonOptions.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Close dialog
dialog_settings.dismiss();
}
});
<...REMOVED TESTED CODE...>
}
// Close dialog
dialog_settings.dismiss();
//selectedFrag.setTimerValue(hour*60*60*1000+minutes*60*1000+seconds*1000);
}
});
}
catch (Exception e)
{
Log.e("MainActivityError: defaultSettingsMenu function: Error in dialog function.",Log.getStackTraceString(e));
}
}
mySpinnerColorSelector CLASS:
public class SpinnerColorSelector extends BaseAdapter {
ArrayList<Integer> colors;
Context context;
public SpinnerColorSelector(Context context)
{
this.context=context;
colors = new ArrayList<Integer>();
colors.add(Color.parseColor("#57b947")); // Time Boss green
colors.add(Color.parseColor("#33B5E5")); // Android blue
colors.add(Color.parseColor("#0099CC")); // blue
colors.add(Color.parseColor("#AA66CC")); // purple light
colors.add(Color.parseColor("#9933CC")); // purple
colors.add(Color.parseColor("#FFBB33")); // orange light
colors.add(Color.parseColor("#FF8800")); // orange
colors.add(Color.parseColor("#FF4444")); // red light
colors.add(Color.parseColor("#CC0000")); // red
}
@Override
public int getCount()
{
return colors.size();
}
@Override
public Object getItem(int arg0)
{
return colors.get(arg0);
}
@Override
public long getItemId(int arg0)
{
return arg0;
}
@SuppressLint({ "ViewHolder", "InflateParams" }) @Override
public View getView(int pos, View view, ViewGroup parent)
{
LayoutInflater inflater=LayoutInflater.from(context);
view=inflater.inflate(android.R.layout.simple_spinner_dropdown_item, null);
TextView txv=(TextView)view.findViewById(android.R.id.text1);
txv.setBackgroundColor(colors.get(pos));
txv.setTextSize(20f);
txv.setTextColor(colors.get(pos));
txv.setText(colors.get(pos).toString());
return view;
}
}
LOGCAT OUTPUT:
09-24 15:14:24.631: E/AndroidRuntime(1698): FATAL EXCEPTION: main
09-24 15:14:24.631: E/AndroidRuntime(1698): Process: com.xpresspe.stopwatch, PID: 1698
09-24 15:14:24.631: E/AndroidRuntime(1698): java.lang.NullPointerException
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.widget.TextView.makeNewLayout(TextView.java:6113)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.widget.TextView.onMeasure(TextView.java:6408)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.view.View.measure(View.java:16648)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.widget.Spinner.setUpChild(Spinner.java:632)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.widget.Spinner.makeView(Spinner.java:585)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.widget.Spinner.getBaseline(Spinner.java:431)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1089)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.view.View.measure(View.java:16648)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.widget.LinearLayout.forceUniformWidth(LinearLayout.java:940)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.widget.LinearLayout.measureVertical(LinearLayout.java:920)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.view.View.measure(View.java:16648)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1226)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.widget.ScrollView.onMeasure(ScrollView.java:326)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.view.View.measure(View.java:16648)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.view.View.measure(View.java:16648)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.view.View.measure(View.java:16648)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.view.View.measure(View.java:16648)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-24 15:14:24.631: E/AndroidRuntime(1698): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2321)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.view.View.measure(View.java:16648)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1959)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1145)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1340)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1032)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5657)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.view.Choreographer.doFrame(Choreographer.java:544)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.os.Handler.handleCallback(Handler.java:733)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.os.Handler.dispatchMessage(Handler.java:95)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.os.Looper.loop(Looper.java:136)
09-24 15:14:24.631: E/AndroidRuntime(1698): at android.app.ActivityThread.main(ActivityThread.java:5026)
09-24 15:14:24.631: E/AndroidRuntime(1698): at java.lang.reflect.Method.invokeNative(Native Method)
09-24 15:14:24.631: E/AndroidRuntime(1698): at java.lang.reflect.Method.invoke(Method.java:515)
09-24 15:14:24.631: E/AndroidRuntime(1698): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
09-24 15:14:24.631: E/AndroidRuntime(1698): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
09-24 15:14:24.631: E/AndroidRuntime(1698): at dalvik.system.NativeStart.main(Native Method)
请帮帮我......
答案 0 :(得分:0)
在SpinnerColorSelector类中,我更改了:
view=inflater.inflate(android.R.layout.simple_spinner_dropdown_item, null);
要:
view=inflater.inflate(android.R.layout.simple_spinner_dropdown_item, parent, false);
运行时错误消失了!它现在可以在模拟器上运行而没有任何问题...仍然很奇怪try-catch没有捕获到这个(可能是因为对话框是在稍后的时间下进行的)。无论如何,回到快乐的小狗!!
这也摆脱了@SuppressLint({&#34; InflateParams&#34;})