小编Chr*_*ell的帖子

如何处理未经检查的投射警告?

Eclipse正在向我发出以下形式的警告:

类型安全:从Object到HashMap的未选中转换

这是来自对我无法控制的API的调用返回Object:

HashMap<String, String> getItems(javax.servlet.http.HttpSession session) {
  HashMap<String, String> theHash = (HashMap<String, String>)session.getAttribute("attributeKey");
  return theHash;
}
Run Code Online (Sandbox Code Playgroud)

如果可能的话,我想避免Eclipse警告,因为理论上它们至少表明存在潜在的代码问题.但是,我还没有找到一种消除这种方法的好方法.我可以将涉及的单行提取到方法中并添加@SuppressWarnings("unchecked")到该方法,从而限制了我忽略警告的代码块的影响.有更好的选择吗?我不想在Eclipse中关闭这些警告.

在我来到代码之前,它更简单,但仍然引发了警告:

HashMap getItems(javax.servlet.http.HttpSession session) {
  HashMap theHash = (HashMap)session.getAttribute("attributeKey");
  return theHash;
}
Run Code Online (Sandbox Code Playgroud)

当您尝试使用哈希时,问题出在其他地方,您会收到警告:

HashMap items = getItems(session);
items.put("this", "that");

Type safety: The method put(Object, Object) belongs to the raw type HashMap.  References to generic type HashMap<K,V> should be parameterized.
Run Code Online (Sandbox Code Playgroud)

java generics warnings

583
推荐指数
12
解决办法
42万
查看次数

没有HttpClient的提供者

从角度4.4升级到5.0并且在将所有HttpModule和Http更新为HttpClientModule后,我开始出现此错误.

我还再次添加了HttpModule,以确保它不是由于一些依赖,但它没有解决问题

在app.module中,我已经正确设置了

import { HttpModule } from '@angular/http';
import { HttpClientModule, HttpClient } from '@angular/common/http';
.
.
.
@NgModule({
    imports: [
        BrowserModule,
        HttpClientModule,
        HttpModule,
        BrowserAnimationsModule,
        FormsModule,
        AppRoutingModule,
.
.
.
Run Code Online (Sandbox Code Playgroud)

我不知道这个错误来自何处,或者我不知道如何弄清楚它的内在.我也有一个警告(也把它放在下面)也许是相关的.

Error: StaticInjectorError[HttpClient]: 
  StaticInjectorError[HttpClient]: 
    NullInjectorError: No provider for HttpClient!
    at _NullInjector.get (vendor.js?v=mekBM8IVBK72-MIZOVSTJizGi_TD_xK3uhPOCRlEHwg:5665)
    at resolveToken (vendor.js?v=mekBM8IVBK72-MIZOVSTJizGi_TD_xK3uhPOCRlEHwg:5953)
    at tryResolveToken (vendor.js?v=mekBM8IVBK72-MIZOVSTJizGi_TD_xK3uhPOCRlEHwg:5895)
    at StaticInjector.get (vendor.js?v=mekBM8IVBK72-MIZOVSTJizGi_TD_xK3uhPOCRlEHwg:5766)
    at resolveToken (vendor.js?v=mekBM8IVBK72-MIZOVSTJizGi_TD_xK3uhPOCRlEHwg:5953)
    at tryResolveToken (vendor.js?v=mekBM8IVBK72-MIZOVSTJizGi_TD_xK3uhPOCRlEHwg:5895)
    at StaticInjector.get (vendor.js?v=mekBM8IVBK72-MIZOVSTJizGi_TD_xK3uhPOCRlEHwg:5766)
    at resolveNgModuleDep (vendor.js?v=mekBM8IVBK72-MIZOVSTJizGi_TD_xK3uhPOCRlEHwg:15328)
    at _createClass (vendor.js?v=mekBM8IVBK72-MIZOVSTJizGi_TD_xK3uhPOCRlEHwg:15373)
    at _createProviderInstance$1 (vendor.js?v=mekBM8IVBK72-MIZOVSTJizGi_TD_xK3uhPOCRlEHwg:15339)
Run Code Online (Sandbox Code Playgroud)

警告信息:

./node_modules/@angular/Common/esm5/http.js
There are multiple modules with names that only …
Run Code Online (Sandbox Code Playgroud)

angular

257
推荐指数
20
解决办法
28万
查看次数

如何使用__doPostBack()

我正在尝试使用ASP.NET创建一个异步回发__doPostBack(),但我不知道如何做到这一点.我想使用vanilla JavaScript.

像按钮单击这样简单的东西会导致__doPostBack()事件触发.我只是想了解这种机制是如何运作的.

javascript c# asp.net postback

154
推荐指数
5
解决办法
38万
查看次数

指定的子项已有父项.你必须首先在孩子的父母上调用removeView()(Android)

在我的应用程序中,我必须经常在两个布局之间切换.错误发生在下面发布的布局中.

第一次调用我的布局时,没有发生任何错误,一切都很好.当我再调用一个不同的布局(一个空白的布局),然后再次调用我的布局时,它会给我以下错误:

> FATAL EXCEPTION: main
>     java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Run Code Online (Sandbox Code Playgroud)

我的布局代码如下所示:

    tv = new TextView(getApplicationContext()); // are initialized somewhere else
    et = new EditText(getApplicationContext()); // in the code


private void ConsoleWindow(){
        runOnUiThread(new Runnable(){

     @Override
     public void run(){

        // MY LAYOUT:
        setContentView(R.layout.activity_console);
        // LINEAR LAYOUT
        LinearLayout layout=new LinearLayout(getApplicationContext());
        layout.setOrientation(LinearLayout.VERTICAL);
        setContentView(layout);

        // TEXTVIEW
        layout.addView(tv); //  <==========  ERROR IN THIS LINE DURING 2ND RUN
        // EDITTEXT
        et.setHint("Enter Command");
        layout.addView(et); …
Run Code Online (Sandbox Code Playgroud)

java android parent-child textview android-edittext

137
推荐指数
11
解决办法
14万
查看次数

在Android中为视图设置全局样式

假设我希望TextView我的应用中的所有实例都有textColor="#ffffff".有没有办法在一个地方设置它而不是为每个地方设置它TextView

android

118
推荐指数
3
解决办法
8万
查看次数

?:?? 运算符而不是IF | ELSE

public string Source
{
    get
    {
        /*
        if ( Source == null ){
            return string . Empty;
        } else {
            return Source;
        }
        */
        return Source ?? string.Empty;
    }
    set
    {
        /*
        if ( Source == null ) {
            Source = string . Empty;
        } else {
            if ( Source == value ) {
                Source = Source;
            } else {
                Source = value;
            }
        }
        */
        Source == value ? Source : value ?? string.Empty;
        RaisePropertyChanged ( "Source" );
    } …
Run Code Online (Sandbox Code Playgroud)

c# ternary-operator

84
推荐指数
3
解决办法
23万
查看次数

Android Google Maps API V2缩放到当前位置

我正在尝试使用Maps API V2来熟悉它,我正在尝试以用户当前位置为中心启动地图.使用该map.setMyLocationEnabled(true);语句,我可以在地图上显示我当前的位置.这也会将按钮添加到UI中,该按钮将地图置于当前位置的中心.

我想在我的代码中模拟按下按钮.我熟悉LocationManagerLocationListener类,并意识到使用它们是一个可行的替代方案,但是中心和放大用户位置的功能似乎已经通过按钮内置.

如果API有一个方法来显示用户的当前位置,那么肯定必须有一个更容易的方法来定位该位置而不是使用LocationManager/ LocationListenerclasses,对吧?

android location google-maps google-maps-android-api-2

71
推荐指数
6
解决办法
12万
查看次数

使用RecyclerView时出错:指定的子项已有父项

我正在尝试使用新的RecyvlerView适配器创建一个listView.我已经按照Android开发人员资源上的确切指南进行了操作.但这给了我一个奇怪的错误:指定的孩子已经有了父母.您必须首先在孩子的父母上调用removeView(). 我有最新的SDK.我还在gradle中定义依赖项.

MyActivity(主要活动):

public class MyActivity extends Activity {

    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

        // use this setting to improve performance if you know that changes
        // in content do not change the layout size of the RecyclerView
        mRecyclerView.setHasFixedSize(true);

        // use a linear layout manager
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());

        // specify an adapter (see also next example)
        mAdapter = new MyAdapter(new String[]{"Zain", …
Run Code Online (Sandbox Code Playgroud)

android listview android-recyclerview

53
推荐指数
3
解决办法
2万
查看次数

ASP.NET核心访问静态类的配置

我想要一个访问Configuration对象的简单静态类.所有配置信息都已从Startup类中的appsettings.json文件中读取.我只需要一种简单的方法来访问它.这可能吗?

namespace MyNamespace
{
    public static class Config
    {
        public string Username => Configuration["Username"];
        public string Password => Configuration["Password"];
    }
}
Run Code Online (Sandbox Code Playgroud)

应用中的其他任何地方:

string username = Config.Username;
string password = Config.Password;
Run Code Online (Sandbox Code Playgroud)

asp.net-core-mvc

47
推荐指数
9
解决办法
2万
查看次数

如何在Android Studio中使用Google Maps Api v2创建Android应用程序?

尝试使用GoogleMap创建应用时,我在Android Studio中遇到了很多麻烦.

之前我已经按照以下指南(几乎)没有使用Eclipse作为我的IDE的问题:

https://developers.google.com/maps/documentation/android/start

我之前从未使用过Android Studio,而且整个项目/模块范例都遇到了困难.

我无法成功配置Google Play服务SDK http://developer.android.com/google/play-services/setup.html

这是我得到的一个奇怪的错误:

Gradle: 
FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':MyMapApp:compileDebug'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

android google-maps google-play-services android-studio

41
推荐指数
3
解决办法
7万
查看次数