我正在学习Spring框架,但我无法理解@Configuration
注释的确切含义以及应该注释哪些类.在Spring Boot文档中,据说Application类应该是@Configuration
class.
Spring Boot支持基于Java的配置.尽管可以使用XML源调用SpringApplication.run(),但我们通常建议您的主要源是@Configuration类.
试图了解@Configuration
我发现用类注释一个类@Configuration
表明该类可以被Spring IoC容器用作bean定义的来源.
如果是这样,那么这个应用程序类如何成为bean定义的来源?
@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
public class App
{
public static void main(String[] args) throws Exception {
SpringApplication.run(App.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
我几乎了解了关于Spring的大多数其他基本概念,但是我无法理解类的目的@Configuration
或哪些类应该是@Configuration
类?有人可以请帮助.谢谢 !!
我的应用程序中有一个导航抽屉,其中包含标题和一些列表项.标题有一个textview,我想让它可点击,但我无法做到.
为了获得这个textview的id,我使用了下面的代码,因为它与onCreate中setContentView中的那个相比在不同的布局文件中.
final LayoutInflater factory = getLayoutInflater();
final View textEntryView = factory.inflate(R.layout.header, null);
TextView home = (TextView) textEntryView.findViewById(R.id.home);
home.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(curr_context, "SHOW", Toast.LENGTH_LONG).show();
}
});
Run Code Online (Sandbox Code Playgroud)
header.xml包含导航抽屉的标题.它有一个名为home的项目.我需要让它可点击.上面的代码在onCreate方法中.
所以从我读到的,Dagger还没有支持注入Worker.但是人们建议有一些解决方法.我尝试过多种方式在线跟踪示例,但它们都不适用于我.
当我不尝试向Worker类注入任何东西时,代码工作正常,只是我不能做我想要的,因为我需要访问一些DAO和服务.如果我对这些依赖项使用@Inject,则依赖项为null或者worker永远不会启动,即调试器甚至不进入Worker类.
例如,我尝试这样做:
@Component(modules = {Module.class})
public interface Component{
void inject(MyWorker myWorker);
}
@Module
public class Module{
@Provides
public MyRepository getMyRepo(){
return new myRepository();
}
}
Run Code Online (Sandbox Code Playgroud)
在我的工人
@Inject
MyRepository myRepo;
public MyWorker() {
DaggerAppComponent.builder().build().inject(this);
}
Run Code Online (Sandbox Code Playgroud)
但随后执行从未到达工人.如果我删除构造函数,myRepo依赖项将保持为null.
我尝试过很多其他事情,但没有工作.有没有办法做到这一点?谢谢!!
所以我在我的应用程序中有通知.通知有一些文本和图像,我从一开始就填充的LruCache中获取.问题是在我的情况下,在Kit-Kat设备(Moto E,XIomi)上,如果通知已经更新了固定的次数,则通知会停止更新,假设为n(这个n通常对每个设备都是固定的,并且是< 10).这是我的更新代码
String title = CommonUtils.getTitleFromID(id, context);
Bitmap bitmap = DataProvider.getInstance(context).diskLruImageCache.getBitmap(id + "");
if (playbackPaused) {
bigView.setImageViewResource(R.id.pause, R.drawable.pause_noti);
smallView.setImageViewResource(R.id.pause1, R.drawable.pause_noti);
remoteViews.setImageViewResource(R.id.pause2, R.drawable.pause_noti);
playbackPaused = false;
Log.d(TAG, "noti-false");
}
bigView.setTextViewText(R.id.title, title + "");
bigView.setImageViewBitmap(R.id.img, bitmap);
smallView.setTextViewText(R.id.title1, title);
smallView.setImageViewBitmap(R.id.img1, bitmap);
mNotificationManager.notify(NOTIFY_ID, notification);
Run Code Online (Sandbox Code Playgroud)
我尝试了一些调试和所有,我发现如果我没有设置位图,(并且只是做
bitmap = null
)然后一切正常.
我不明白为什么会这样.这个问题是特定于Android版本,因为我已经在Nexus 5(Lollipop)和其他Android 5手机上进行了测试,但是这种情况不会发生.有人知道这背后的原因是什么吗?
我并不期待确切的情况.即使是一些正确方向的想法也会非常有用.谢谢 !!
我也在为可能需要的情况下添加DiskLruCache的代码,
public class DiskLruImageCache {
private DiskLruCache mDiskCache;
private Bitmap.CompressFormat mCompressFormat = Bitmap.CompressFormat.JPEG;
private int mCompressQuality = 70;
private static final int APP_VERSION = 1; …
Run Code Online (Sandbox Code Playgroud) 更新:以前我找不到一个定义良好的模式,以确定我的前台服务何时被杀死.在发现这种情况后,对设备进行了更多调试(并未发生任何事情).
1.)很多时候,当我打开chrome加载网站时,前台服务被杀死了.有时即使我使用whatsapp,也会发生这种情况.
2.)没有例外,堆栈跟踪没有显示任何有用的东西.
原始问题如下:
StackOverflow上有很多这样的问题,但到目前为止我已阅读的答案主要是说它取决于Android,我们没有100%保证前台服务不会被杀死.一些答案建议START_STICKY,但这对我的情况没有多大帮助.
在我的情况下,我有一个音乐播放器应用程序,它具有前台服务.这项服务在某些设备上被杀死,主要是Xiomi的某些版本(Android版本为5.1.1).现在我明白android可能内存不足,所以我的前台服务被杀了,但是为什么其他音乐播放器应用程序永远不会经历这样的终止.什么是他们做得对,我不是?
我使用了我的服务前台服务startForeground
.此外,我返回START_STICKY
onStartCommand,虽然这没有帮助,因为服务在4-5秒后重新启动,如果被杀死.要使用我的活动绑定我的服务
bindService(playIntent, musicConnection, Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT );
Run Code Online (Sandbox Code Playgroud)
那么究竟我可以在我的应用程序中改进/更改以防止这种情况发生,如果其他应用程序正常工作,那么我的情况肯定会出现问题.有人可以请帮助.提前致谢 !!
编辑:
这就是我调用startForeground()的方法
public void sendNotification() {
Intent notIntent = new Intent(this, MainActivity.class);
notIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendInt = PendingIntent.getActivity(this, 0,
notIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Bitmap bitmap = null;
if (!notificationShowing || !forwarded) {
Log.i(TAG, "present");
String title = CommonUtils.getSongFromID(songIndex, this);
bigView.setTextViewText(R.id.title, title);
bigView.setImageViewBitmap(R.id.img, bitmap);
smallView.setTextViewText(R.id.title1, title);
smallView.setImageViewBitmap(R.id.img1, bitmap);
if (pauseButton == 1) {
bigView.setImageViewResource(R.id.pause, R.drawable.pause_noti);
smallView.setImageViewResource(R.id.pause1, R.drawable.pause_noti);
} else {
bigView.setImageViewResource(R.id.pause, R.drawable.play_noti);
smallView.setImageViewResource(R.id.pause1, R.drawable.play_noti); …
Run Code Online (Sandbox Code Playgroud) 我在Android MarshMellow,OnePlus上的均衡器应用程序中收到以下错误.
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.app.name/com.my.app.name.activity.MainActivity}: java.lang.RuntimeException: Cannot initialize effect engine for type: 0bed4300-ddd6-11db-8f34-0002a5d5c51b Error: -3
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.RuntimeException: Cannot initialize effect engine for type: 0bed4300-ddd6-11db-8f34-0002a5d5c51b Error: -3
at android.media.audiofx.AudioEffect.<init>(AudioEffect.java:411)
at android.media.audiofx.Equalizer.<init>(Equalizer.java:139)
at com.my.app.name.activity.MainActivity.onCreate(Unknown Source)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
... 9 more
Run Code Online (Sandbox Code Playgroud)
它发生在我尝试初始化均衡器时,如下所示:
Equalizer eq = new Equalizer(0, 0);
Run Code Online (Sandbox Code Playgroud)
虽然均衡器类抛出此异常,但它为什么会发生,如何修复?这也发生在其他一些设备上.但在Nexus和大多数设备上运行良好.
有人可以提出任何想法吗?我的清单中也有以下许可.
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
Run Code Online (Sandbox Code Playgroud)
谢谢 !!
好的,我知道这是一个微不足道的问题,但由于某种原因,它不适合我.我在其他答案中做了很多建议但是徒劳无功.我的drawable文件夹有白色图标.我甚至尝试从styles.xml更改它,但这也不起作用.我在我的Lollipop设备上测试它.任何帮助将不胜感激.提前致谢.
这是我的清单文件的一部分.
<application
android:allowBackup="true"
android:icon="@drawable/ic_drawer"
android:label="@string/app_name" >
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"
tools:replace="android:value" />
<activity
android:name=".Activity_Splash"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Activity_test"
android:launchMode="singleInstance"
android:theme="@style/AppTheme.Base"
android:windowSoftInputMode="stateHidden" />
Run Code Online (Sandbox Code Playgroud)
最后这是我的style.xml.类似于v-21.
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:actionMenuTextColor">#fff</item>
<item name="colorPrimary">@color/ColorPrimary</item>
<item name="colorPrimaryDark">@color/ColorPrimaryDark</item>
<item name="drawerArrowStyle">@style/MyDrawerArrowToggle</item>
<item name="windowActionBar">false</item>
<item name="colorAccent">#fff</item>
<item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
<item name="android:windowSharedElementExitTransition">@android:transition/move</item>
</style>
Run Code Online (Sandbox Code Playgroud) 我正在使用本教程从官方spring文档到使用手动配置OAuth2客户端@EnableOAuth2Client
.由于某种原因,它无法正常工作.当我运行应用程序并访问时,http://localhost:8080/login
我会看到基本表单登录而不是Google登录选项.(因为我的用例,我需要使这个手动配置工作.)
但是,@EnableOauth2Sso
在我不使用任何手动配置的情况下,代码工作正常OAuth2AuthenticationProcessingFilters
.在这种情况下,我在访问我的登录页面时获得谷歌登录选项.有人可以帮帮我吗.我添加了以下代码:
这是@EnableOAuth2Sso
有效的,完美的
@Configuration
@EnableWebSecurity
@EnableOAuth2Sso
@PropertySource({ "classpath:/oauth2.properties" })
@EnableGlobalMethodSecurity(prePostEnabled = true, jsr250Enabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
OAuth2ClientContext oauth2ClientContext;
@Value("${security.oauth2.resource.userInfoUri}")
String userInfoUri;
@Value("${security.oauth2.client.clientId}")
String clientId;
@Bean
public RequestContextListener requestContextListener() {
return new RequestContextListener();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
// http.antMatcher("/**").addFilterBefore(ssoFilter(), BasicAuthenticationFilter.class);
}
}
Run Code Online (Sandbox Code Playgroud)
这是@EnableOAuth2Client
,但不起作用,我得到表单登录
@Configuration
@EnableWebSecurity
@EnableOAuth2Client
@PropertySource({ "classpath:/oauth2.properties" })
@EnableGlobalMethodSecurity(prePostEnabled = true, jsr250Enabled = true) …
Run Code Online (Sandbox Code Playgroud) 我正在尝试实现导航抽屉,但由于某些原因我得到这个空指针异常.
我花了很多时间,却徒劳无功.这是我的代码的一部分.我无法理解为什么它返回空指针异常.
我是否需要导入任何库?
提前致谢.
package com.motobeans.productions.aloha;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import java.util.Locale;
import android.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.widget.ImageView;
import android.widget.ListView;
public class Activity_test extends ActionBarActivity {
private String[] mPlanetTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mTitle;
private CharSequence mDrawerTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar); …
Run Code Online (Sandbox Code Playgroud) android nullpointerexception navigation-drawer android-actionbaractivity
我正在构建一个Android应用程序,帮助用户在两点之间导航.这是doc的链接.这是我想要提供给用户的确切功能,但我希望导航发生在我的应用中,而不是通过这种方式启动意图
Uri gmmIntentUri = Uri.parse("google.navigation:q=Taronga+Zoo,+Sydney+Australia");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
Run Code Online (Sandbox Code Playgroud)
我在我的应用程序中嵌入了谷歌地图,但无法想到如何实现此导航.有人可以指导我或提供有关相同的任何提示或教程吗?提前致谢 !!