关于EF为Navagation属性返回null和ef添加数据返回id的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于AndroidMediaProjectionacquisitionLat
关于EF为Navagation属性返回null和ef添加数据返回id的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于Android MediaProjection acquisitionLatestImage始终为ImageReader返回Null、android – IllegalArgumentException:savedInstanceState指定为Non-Null为Null、android – IllegalStateException:NavigationDrawer活动中的Fragment内的RecyclerView为null、Application.Current
- EF为Navagation属性返回null(ef添加数据返回id)
- Android MediaProjection acquisitionLatestImage始终为ImageReader返回Null
- android – IllegalArgumentException:savedInstanceState指定为Non-Null为Null
- android – IllegalStateException:NavigationDrawer活动中的Fragment内的RecyclerView为null
- Application.Current
返回null
EF为Navagation属性返回null(ef添加数据返回id)
从上述ModelBuilder配置中,我们可以看到您错误地将Item.Id
用作ForeignKey
来绑定课程。
您应该像下面的代码一样将public int CourseRef { get; set; }
设置为ForeignKey
。
在实体框架核心中配置一对一关系
public class Course
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public ICollection<CategoryToCourse> Categories { get; set; }
public ICollection<AuthorToCourse> Authors { get; set; }
public Item Item { get; set; }
}
public class Item
{
public int Id { get; set; }
public decimal Price { get; set; }
public int CourseRef { get; set; }
public Course Course { get; set; }
}
modelBuilder.Entity<Course>()
.HasOne(a => a.Item)
.WithOne(b => b.Course)
.HasForeignKey<Item>(i => i.CourseRef);
Android MediaProjection acquisitionLatestImage始终为ImageReader返回Null
如何解决Android MediaProjection acquisitionLatestImage始终为ImageReader返回Null?
我正在编写简单的Screenshot应用程序,并为此使用MediaProjection + ImageReader。我使用some sample解决此任务。
当我单击“捕获”按钮时,我总是在日志消息中得到"image: NULL"
。
在ImageReader.newInstance
中,我将格式设置为ImageFormat.RGB_565
。默认值为0x1
,但是当我使用此值时,会显示警告消息:
必须是以下之一:ImageFormat.UNKNowN,ImageFormat.RGB_565, ImageFormat.YV12,ImageFormat.Y8,ImageFormat.NV16,ImageFormat.NV21, ImageFormat.YUY2,ImageFormat.JPEG,ImageFormat.DEPTH_JPEG, ImageFormat.YUV_420_888,ImageFormat.YUV_422_888, ImageFormat.YUV_444_888,ImageFormat.FLEX_RGB_888, ImageFormat.FLEX_RGBA_8888,ImageFormat.RAW_SENSOR, ImageFormat.RAW_PRIVATE,ImageFormat.RAW10,ImageFormat.RAW12, ImageFormat.DEPTH16,ImageFormat.DEPTH_POINT_CLOUD, ImageFormat.PRIVATE,ImageFormat.HEIC
我的问题:为什么 ImageReader 中的 Image 总是 Null ?以及如何正确解决?谢谢
我的代码:
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.ImageFormat;
import android.hardware.display.displayManager;
import android.hardware.display.Virtualdisplay;
import android.media.Image;
import android.media.ImageReader;
import android.media.projection.MediaProjection;
import android.media.projection.mediaprojectionmanager;
import android.os.Bundle;
import android.os.Environment;
import android.util.displayMetrics;
import android.util.Log;
import android.view.View;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "test_t";
private ImageReader mImageReader;
private mediaprojectionmanager mmediaprojectionmanager;
private Intent mCreateScreenCaptureIntent;
private MediaProjection mMediaProjection;
private Virtualdisplay mVirtualdisplay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View mBtnCapture = findViewById(R.id.btn_capture);
mBtnCapture.setonClickListener(this);
init();
}
@Override
protected void onDestroy() {
super.onDestroy();
tearDownMediaProjection();
}
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data) {
super.onActivityResult(requestCode,resultCode,data);
mMediaProjection = mmediaprojectionmanager.getMediaProjection(resultCode,data);
if (mMediaProjection != null) {
startScreenCapture();
takeCapture();
stopScreenCapture();
}
}
@Override
public void onClick(View v) {
startActivityForResult(mCreateScreenCaptureIntent,777);
}
private void init() {
displayMetrics displayMetrics = getResources().getdisplayMetrics();
mImageReader = ImageReader.newInstance(displayMetrics.widthPixels,displayMetrics.heightPixels,ImageFormat.RGB_565,2); // format 0x1
mmediaprojectionmanager = (mediaprojectionmanager) getSystemService(MEDIA_PROJECTION_SERVICE);
mCreateScreenCaptureIntent = mmediaprojectionmanager.createScreenCaptureIntent();
}
private void startScreenCapture() {
if (mMediaProjection == null)
return;
displayMetrics displayMetrics = getResources().getdisplayMetrics();
mVirtualdisplay = mMediaProjection.createVirtualdisplay(
"ScreenCapture",displayMetrics.widthPixels,displayMetrics.densityDpi,displayManager.VIRTUAL_disPLAY_FLAG_AUTO_MIRROR,mImageReader.getSurface(),null,null);
}
private void stopScreenCapture() {
if (mVirtualdisplay == null) {
return;
}
mVirtualdisplay.release();
mVirtualdisplay = null;
}
private void takeCapture() {
Image image = mImageReader.acquireLatestimage();
if (image == null) {
Log.d(TAG,"image: NULL");
return;
}
int width = image.getWidth();
int height = image.getHeight();
final Image.Plane[] planes = image.getPlanes();
final ByteBuffer buffer = planes[0].getBuffer();
int pixelStride = planes[0].getPixelStride();
int rowStride = planes[0].getRowStride();
int rowPadding = rowStride - pixelStride * width;
Bitmap mBitmap = Bitmap.createBitmap(width + rowPadding / pixelStride,height,Bitmap.Config.ARGB_8888);
mBitmap.copyPixelsFromBuffer(buffer);
mBitmap = Bitmap.createBitmap(mBitmap,width,height);
image.close();
saveBitmapToFile(mBitmap);
}
private void saveBitmapToFile(Bitmap bitmap) {
File directory = new File(Environment.getExternalStorageDirectory(),"SCREEN_TEMP");
if (!directory.exists())
directory.mkdirs();
String name = "shot" + new SimpleDateFormat("yyyyMMddHHmmsss").format(new Date()) + "." + Bitmap.CompressFormat.PNG.toString();
File file = new File(directory,name);
try {
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG,100,out);
out.flush();
out.close();
} catch (IOException e) {
e.printstacktrace();
}
}
private void tearDownMediaProjection() {
if (mMediaProjection != null) {
mMediaProjection.stop();
mMediaProjection = null;
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
android – IllegalArgumentException:savedInstanceState指定为Non-Null为Null
当我启动MainActivity时出现一个奇怪的错误:
06-16 16:01:05.193 2083-2083/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.android.example.github, PID: 2083
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.example.github/com.android.example.github.ui.MainActivity}: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter savedInstanceState
at android.app.ActivityThread.performlaunchActivity(ActivityThread.java:2666)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
Caused by: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter savedInstanceState
at com.android.example.github.injection.AppInjector$init$1.onActivityCreated(AppInjector.kt)
at android.app.Application.dispatchActivityCreated(Application.java:197)
at android.app.Activity.onCreate(Activity.java:961)
at android.support.v4.app.BaseFragmentActivityGingerbread.onCreate(BaseFragmentActivityGingerbread.java:54)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:319)
at com.android.example.github.ui.MainActivity.onCreate(MainActivity.kt:20)
at android.app.Activity.performCreate(Activity.java:6682)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performlaunchActivity(ActivityThread.java:2619)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
这是我的MainActivity类:
class MainActivity : LifecycleActivity(), HasSupportFragmentInjector {
lateinit var dispatchingAndroidInjector: dispatchingAndroidInjector<Fragment>
@Inject set
lateinit var navigationController: NavigationController
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
if (savedInstanceState == null) {
navigationController.navigatetoSearch()
}
}
override fun supportFragmentInjector(): AndroidInjector<Fragment> {
return dispatchingAndroidInjector
}
}
该错误表明参数savedInstanceState为null,当它被指定为非null时;但它可以为空(savedInstanceState:Bundle?),并且onCreate()方法在源中标记为@Nullable.
我在任何其他Kotlin项目中都没有遇到过这个错误.我正在使用Kotlin版本1.1.2-5;与1.1.2-3有同样的错误.
解决方法:
似乎问题不在onCreate方法中.尝试查看com.android.example.github.injection.AppInjector $init $1.onActivityCreated(AppInjector.kt).我不知道它是否是一个生成的类,但它应该让你知道下一步该做什么.
android – IllegalStateException:NavigationDrawer活动中的Fragment内的RecyclerView为null
我试图制作一个RecyclerView,它将进入我片段的两个页面之一.这些页面放在NavigationDrawer活动中.目标是创建类似Play商店应用主页的内容.
但是我在运行时的代码片段中发现了一个错误.它说:
java.lang.IllegalStateException: mainMenu must not be null
at com.example.MyApp.app.fragment.MainFragment.onCreate(MainFragment.kt:49)
我一直在看一些SO线程,他们说布局没有正确加载.这导致一些元素没有被链接起来.另一位在评论中说,问题在于上下文未正确初始化.对我来说情况并非如此(相反,它是RecyclerView).
以下是链接,希望它们可以作为参考.
> TextView must not be null
> context must not be null
在对我的代码进行多次检查后,我发誓我已将正确的布局放入.编辑:我导入了kotlinx.android.synthetic.main.^包,我把这个标志放在:^ ${layout}.这是我的一些文件(如果这个帖子太长,请原谅我):
> MainActivity.kt:AppCompatActivity()^ activity_main.*
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// preparing the app bars
setSupportActionBar(toolBar)
// getting ready for the pages
val pagerAdapter = MainPagerAdapter(
supportFragmentManager,
resources.getString(R.string.tab_main),
resources.getString(R.string.tab_chat)
)
pager.adapter = pagerAdapter
// activating tabs
tabLayout.setupWithViewPager(pager)
val toggle = ActionBarDrawerToggle(
this, mainDrawer, toolbar,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close)
mainDrawer.addDrawerListener(toggle)
navView.setNavigationItemSelectedListener(this)
toggle.syncState()
}
> MainPagerAdapter.kt(fm:FragmentManager,private val page1:String,private val page2:String):FragmentPagerAdapter(fm)
override fun getItem(position: Int): Fragment? {
return when (position) {
0 -> MainFragment()
1 -> ChatFragment()
else -> null
}
}
override fun getCount() = 2
override fun getPageTitle(position: Int): CharSequence? {
return when (position) {
0 -> page1
1 -> page2
else -> null
}
}
> MainFragment.kt:Fragment()^ content_main.*
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?)
: View? = inflater.inflate(R.layout.content_main, container)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// this is the error
mainMenu.layoutManager = linearlayoutmanager(this.context)
mainMenu.adapter = MyAdapter(itemList) {
toast("${it.name} selected")
}
}
> MyAdapter.kt:RecyclerView.Adapter< MyAdapter.MyHolder>()^ item_custom.view.*(由Antonio Leiva提供)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int)
= GoodsHolder(parent.inflate(R.layout.item_custom))
override fun getItemCount()
= itemList.size
override fun onBindViewHolder(holder: MyHolder, position: Int)
= holder.bind(itemList[position], listener)
class MyHolder(v: View): RecyclerView.ViewHolder(v){
private val item: Item? = null
private val view = v
fun bind(item: Item, listener: (Item) -> Unit)
= with (itemView) {
imgPic.setimageResource(item.pictureId)
txtName.text = item.name
txtPrice.text = item.price.toString()
setonClickListener { listener(item) }
}
}
> content_main.xml
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.MyApp.app.activity.MainActivity"
>
<!-- the RecyclerView that caused the runtime error -->
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainMenu"/>
</android.support.constraint.ConstraintLayout>
> item_custom.xml(这些代码位于CardView内的LinearLayout内)
<ImageView
android:id="@+id/imgPic"
android:layout_width="match_parent"
android:layout_height="128dp"
app:srcCompat="@drawable/ic_menu_gallery" />
<TextView
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get it while it's hot!"
android:layout_margin="@dimen/margin_small"
android:layout_marginTop="@dimen/margin_medium"
android:maxLines="2"
android:ellipsize="end"
android:text/>
<TextView
android:id="@+id/txtPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="$3.000.000"
android:layout_marginLeft="@dimen/margin_small"
android:layout_marginStart="@dimen/margin_small"
android:layout_marginRight="@dimen/margin_small"
android:layout_marginEnd="@dimen/margin_small"
android:layout_marginBottom="@dimen/margin_medium"/>
> ChatFragment.kt:Fragment()(仅包含onCreateView inflating content_main_chat.xml)
> content_main_chat.xml(仅包含TextView)
解决方法:
您尚未在MainFragment.kt文件中初始化RecyclerView.你必须在下面的行之前初始化它:
mainMenu.layoutManager = linearlayoutmanager(this.context)
您可以通过以下行初始化RecyclerView:
var mainMenu = findViewById(R.id.mainMenu) as RecyclerView
你必须根据需要改变它.
Application.Current返回null
确保已在App的构造函数中设置了 MainPage 。
type Vacancies struct {
ID int `json:"id"`
Title string `json:"title"`
Logo string `json:"logo"`
Items []string `json:"items"`
}
func (r *repository) GetVacancies(ctx context.Context) []*Vacancies {
const queryString = "select json_object('id',vd.id,'title',vd.title,'logo',vd.logo,'items',json_array((select GROUP_CONCAT(json_object('id',id,title,'description',description)) from vacancies as v where department_id = vd.id order by vd.sort))) from vacancy_departments as vd order by vd.sort"
defer utils.StartRelicDatastoreSegment(
ctx,newrelic.DatastoreMySQL,"Vacancies.GetVacancies","Select",queryString,).End()
var result []*Vacancies
if err := mysql.Client().Slave().Select(&result,queryString); err != nil {
logger.Get().Error("err while getting vacancies",zap.Error(err))
return nil
}
return result
}
如果仍然无法使用,则可以首先使用以下解决方法。
public App()
{
InitializeComponent();
MainPage = new xxxPage();
}
并引用
public static Page RootPage;
public App()
{
InitializeComponent();
MainPage = new MainPage();
App.RootPage = this.MainPage;
}
我们今天的关于EF为Navagation属性返回null和ef添加数据返回id的分享已经告一段落,感谢您的关注,如果您想了解更多关于Android MediaProjection acquisitionLatestImage始终为ImageReader返回Null、android – IllegalArgumentException:savedInstanceState指定为Non-Null为Null、android – IllegalStateException:NavigationDrawer活动中的Fragment内的RecyclerView为null、Application.Current
本文标签: