- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 java 和使用 Android Studio 的新手。我的应用程序安装后无法启动。检查源代码和XML,我似乎找不到任何错误。请帮我检查下面的代码
MainActivity.java
public class MainActivity extends AppCompatActivity {
ArrayList<File> list;
GridView gridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView)findViewById(R.id.image_grid);
list = imageReader(Environment.getExternalStorageDirectory());
gridView.setAdapter(new gridAdapter());
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(MainActivity.this, FullImageActivity.class);
intent.putExtra("img", list.get(i).toString());
startActivity(intent);
}
});
}
public class gridAdapter extends BaseAdapter{
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View convertView = null;
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.row_layout, viewGroup, false);
ImageView myImage = (ImageView) convertView.findViewById(R.id.my_image);
myImage.setImageURI(Uri.parse(list.get(i).toString()));
}
return convertView;
}
}
private ArrayList<File> imageReader(File externalStorageDirectory) {
ArrayList<File> b = new ArrayList<>();
File[] files = externalStorageDirectory.listFiles();
for (int i =0; i<files.length; i++){
if (files[i].isDirectory()){
b.addAll(imageReader(files[i]));
}else {
if (files[i].getName().endsWith(".jpg")) {
b.add(files[i]);
}
}
}
return b;
}
}
这是activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<GridView
android:id="@+id/image_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit"
android:columnWidth="100dp"
android:verticalSpacing="100dp"
android:horizontalSpacing="10dp"
android:stretchMode="spacingWidthUniform" />
</RelativeLayout>
这是 FullImageActivity.java
public class FullImageActivity extends AppCompatActivity {
ImageView fullImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full_image);
fullImage = (ImageView)findViewById(R.id.full_image);
String data = getIntent().getExtras().getString("img");
fullImage.setImageURI(Uri.parse(data));
}
}
activity_full_image.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".FullImageActivity">
<ImageView
android:id="@+id/full_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/wp"
/>
</RelativeLayout>
row_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/my_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/wp"
/>
</RelativeLayout>
如果我的问题得到关注,我会很高兴,因为它深深地困扰着我。
这是 LOGCAT
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
at com.jkjworks.galleryextra.FullImageActivity.onCreate(FullImageActivity.java:19)
at android.app.Activity.performCreate(Activity.java:7030
#2 更新 list 和 BUILD.GRADLE
这是 list
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"/>
<activity android:name=".FullImageActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是APP级别的Build.gradle
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.domain.galleryextra"
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"
multiDexEnabled true
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
compile 'com.android.support:multidex:1.0.3'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
implementation 'com.google.android.gms:play-services-ads:12.0.1'
implementation 'com.android.support:support-vector-drawable:27.0.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
最佳答案
您确定 list.get(i)
不是 null
吗?我会放置一个断点并分析 list.get(i)
的返回值,以确保您传递的数据是有效的数据。
此外,使用 intent.getStringExtra("img")
而不是 getIntent().getExtras().getString("img")
您的问题是 getIntent()
正在接收 NULL
,并且您试图访问它,从而导致发生 NullPointerException
。
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
at com.merizekworks.galleryextra.FullImageActivity.onCreate(FullImageActivity.java:19)
编辑(如何调试的快速分割)您能做的最好的事情就是通过调试器单步执行代码。如果您从未这样做过,那么实际上很容易做到,并且可以帮助您了解问题所在。
在MainActivity
中更改此:
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(MainActivity.this, FullImageActivity.class);
intent.putExtra("img", list.get(i).toString());
startActivity(intent);
}
为此,为了帮助我们更轻松地调试:
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(MainActivity.this, FullImageActivity.class);
File selectedFile = list.get(i); //PUT BREAKPOINT ON THIS LINE
String filePath = selectedFile.toString();
intent.putExtra("img", filePath );
startActivity(intent);
}
注意:如果您不熟悉调试代码,那么我强烈建议您花几分钟阅读本文。在我看来,它是程序员拥有的最有值(value)的工具。 Click here to learn how to debug in Android Studio
左边的红圈是断点,代码会在这里停止执行,你可以分析每个变量,如上图所示。您甚至可以在应用程序运行且执行被断点暂停时“评估表达式”。这样做可以让您抓取内存中当前的任何对象并对其进行测试。
例如,通过上面设置的断点,我们可以使用“调试”选项卡中的“评估表达式”按钮:
以下是有关如何评估表达式的快速屏幕截图,摘自有关如何使用 AndroidStudio 调试器 ( Full guide here ) 的文章:
<小时/>更新#2
请注意,如果您使用的是 API 23 或更高版本,那么您必须确保拥有从 Android 设备读取/写入的权限。
在 AndroidManifest.xml
内的 Manifest
标记中包含以下内容:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
这是我在项目中使用的解决方案,它对我来说非常有效,特别是如果您计划请求多个权限。
public class MainActivity extends AppCompatActivity {
private final String TAG = "DEBUG";
private Activity activity;
private ArrayList<File> list;
private GridView gridView;
private final int REQUEST_PERMISSIONS_CODE = 1;
private String[] PERMISSIONS = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
};
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
int totalGrantedCount = 0;
for(int x=0; x < grantResults.length; x++){
if (grantResults[x] == PackageManager.PERMISSION_GRANTED){
totalGrantedCount++;
}
}
if(totalGrantedCount == grantResults.length){
startApp();
}else{
boolean somePermissionsForeverDenied = false;
for(String permission: permissions){
if(ActivityCompat.shouldShowRequestPermissionRationale(this, permission)){
//denied
Log.d(TAG, "PERMISSIONS: DENIED " + permission);
}else{
if(ActivityCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED){
//allowed
Log.d(TAG, "PERMISSIONS: ALLOWED " + permission);
} else{
//set to never ask again
somePermissionsForeverDenied = true;
}
}
}
if(somePermissionsForeverDenied){
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle("Permissions Required")
.setMessage("You have forcefully denied some of the required permissions " +
"for this action. Please open settings, go to permissions and allow them manually.")
.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.fromParts("package", getPackageName(), null));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setCancelable(false)
.create()
.show();
}else{
new AlertDialog.Builder(this)
.setTitle("PERMISSIONS DENIED")
.setMessage("Unable to proceed, this application requires that permissions be accepted to operate correctly. \r\n\r\n YOU MUST APPROVE ALL PERMISSIONS BEFORE CONTINUING.")
.setPositiveButton("TAP HERE TO CLOSE AND TRY AGAIN.", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
activity.finish();
System.exit(0);
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.setCancelable(false)
.show();
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.activity = this;
//REQUIRED FOR API 23+ - REQUEST FOR PERMISSIONS - ONLY REQUIRED TO BE DONE ONCE PER APP INSTALL.
int permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
this,
PERMISSIONS,
REQUEST_PERMISSIONS_CODE);
}else{
startApp();
}
}
public void startApp(){
gridView = (GridView)findViewById(R.id.image_grid);
list = imageReader(Environment.getExternalStorageDirectory());
gridView.setAdapter(new gridAdapter());
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(MainActivity.this, FullImageActivity.class);
intent.putExtra("img", list.get(i).toString());
startActivity(intent);
}
});
}
public class gridAdapter extends BaseAdapter {
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View convertView = null;
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.row_layout, viewGroup, false);
ImageView myImage = (ImageView) convertView.findViewById(R.id.my_image);
myImage.setImageURI(Uri.parse(list.get(i).toString()));
}
return convertView;
}
}
private ArrayList<File> imageReader(File externalStorageDirectory) {
ArrayList<File> b = new ArrayList<>();
File[] files = externalStorageDirectory.listFiles();
for (int i =0; i<files.length; i++){
if (files[i].isDirectory()){
b.addAll(imageReader(files[i]));
}else {
if (files[i].getName().endsWith(".jpg")) {
b.add(files[i]);
}
}
}
return b;
}
}
关于java - 由于 Intent 中的 NullPointerException,Android 图库应用程序在安装后无法启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56024840/
这是一个链接:http://lomakincello.net/etu/sehen.php 我正在使用 HTML 和 CSS 创建一个网站。我不熟悉 JavaScript,所以有人帮助我制作了一个运行
大家好。我的目标是为自己的新爱好摄影创建一个个人作品集网站。 目前一切就绪。 (这个可以看here。) 问题是,尽我所能,我无法在 CSS 中替换图像。我花了很长时间试图移动图像等,但它只是悲惨地失败
我对画廊有疑问。我无法通过触摸滚动。通过 DPAD 或轨迹球滚动效果很好。 这是一些代码:xml布局: 适配器: package de.goddchen.android.advent.tem
是否有一个社区站点包含一组自定义控件或他们如何调用它为 View ,人们可以在其中获取和重用或发布比标准 UI 组件集更高级的东西? 最佳答案 OpenIntents.org has a bunch
我看了很多论坛,也试过很多东西,就是无法添加PS图库我在公司代理后面,但我已经设置了我的个人资料来使用它。我正在尝试使用这些命令注册 PS 存储库 [Net.ServicePointManager]:
我目前正在尝试在现有的 C++ 项目中使用 boost 图形库。我想将自定义类的对象存储在 boost 图中。下面是一个小示例,其中包含具有两个成员(一个字符串和一个整数)的自定义类定义及其相应的 g
需要从路径获取图像。我已经尝试了所有方法,但似乎没有得到图像。 我的两个图片路径: /storage/emulated/0/DCIM/Camera/20161025_081413.jpg conten
我正在使用 Turbolinks 开发 Android 和 iOS 网络应用程序。 我正在尝试使用 native View /进程实现拍摄新照片或从图库中选择一张照片。 我的表单中有这一行 当我通过
我正在尝试实现诸如适用于 Android 的 Airbnb 应用程序的效果,列出每行中水平滚动画廊的位置(并且每行都有带标题的顶层)。我在每个单元格中使用带有 FrameLayout 和 ViewPa
好吧,我是编码初学者。我下载了 Galereya Jquery 脚本。它有效,但它与代码中它下面的所有其他内容重叠。我尝试更改溢出和位置,但似乎没有任何效果。当我将它添加到另一个 div 中时,网格停
这是我在 stackoverflow 上的第一篇文章。因此,对于这篇文章中的一些不典型内容,我深表歉意。 我编写了一个带有画廊 slider 的 Django 网站,但我不知道为什么图像太大。看这里:
我正在处理一个网站的图库,它由显示所有图片的缩略图 block 组成以及用于显示特定图像的部分。 这是我用来以实际大小显示缩略图和图像的代码。 问题是:我想不出任何东西来设置打开图像的大小。设
我使用 jquery 制作了一个简单的画廊,但是左右按钮不起作用?? 我使用了 jquery 函数 first().appendTo 和 last().prependTo 请检查下面的链接 Mydem
好的伙计们,请不要杀了我,这是我的第一个问题,我对编程知之甚少:)所以我想用灯箱画廊创建一个简单的网站,你可以看看它here . 所以我无法解决的问题是,当您向下滚动页面时,照片后面没有黑色背景。 我
我正在编写自己的图形库 Graph++ ,我对接口(interface)应该返回什么有疑问。例如,我的 BFS 应该返回什么,我很困惑它是否应该返回按顺序访问的一组 vertices ,或者我应该有
我一直在使用图库控件来显示照片,但在滑动照片时遇到问题。我需要一直滑动才能更改照片,否则它会弹回到上一张照片。 在互联网上查询后,我听说画廊已被弃用。下一个可以与画廊控件执行相同操作的控件是什么? 最
为了上大学,我必须在 android 上开发一个带有面部检测的应用程序。为此,我必须在我的画廊中保存各种照片。问题是保存照片后,图库不会更新。更准确地说,如果我删除了我要保存图像的目录,我打开应用程序
我正在这样使用图库 但是当我运行代码时,我发现画廊从中间开始,我想从左边开始。我该怎么办,请帮助我。 最佳答案 描述有关显示的一般信息的结构,例如其大小、密度和字体缩放。要访问 DisplayMe
我试图让用户从他的设备中选择图像或视频,目前它只显示视频或图像,具体取决于以下代码中首先写入的内容: Intent galleryIntent = new Intent(Intent.ACTION_
我正在创建一个 cover flow通过扩展 Gallery 类来处理图像。 画廊 View 显示正常,但图像从右向左滚动的速度非常快,反之亦然。 有什么方法可以调节图像在水平方向上从右到左移动的速度
我是一名优秀的程序员,十分优秀!