我是本机android开发的新手。我正在使用android studio
来开发应用。我正在使用tabbed-activity
并使用3个标签。所有3个标签都有单独的classes
和layouts
,其名称如下图所示。
类和布局名称与图像中显示的相同。
我的标签包含以下功能。
google map v2
,它显示了我当前的gps位置,上面有坐标和标记。camera
,其中我放置了一个按钮和一个image view
。按下按钮,相机将显示,当我拍照时,它显示我的图像视图。此图片也会保存到我的SD CARD
中。 gird view
,它将从SD CARD
中获取这些图像并将其显示在网格上。 third tab
我跟随此tutorial,因此我分别制作了2个helper
和1个adapter
课程。 2个辅助类是AppConstant
和Utils
,适配器名为GridViewImageAdapter
,扩展了BaseAdapter
。
以上3个(2个辅助和1个适配器)类都在我的pictures
类中导入。
现在,当我第一次运行应用程序时,我能够看到我当前的位置。但是,当我点击camera
标签时,它会转到Util
类,当它找不到任何文件时应用程序崩溃。这不应该发生,因为我的Util类只导入pictures
片段,而且与camera
片段无关。以下是代码部分
Camera.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.camera, container, false);
button = (Button)rootView.findViewById(R.id.getpicture);
imageView = (ImageView)rootView.findViewById(R.id.imageView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Check permission for CAMERA
if (ActivityCompat.checkSelfPermission(getActivity(), CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
// Check Permissions Now
// Callback onRequestPermissionsResult interceptado na Activity MainActivity
ActivityCompat.requestPermissions(getActivity(),
new String[]{CAMERA},
Camera.REQUEST_CAMERA);
}
else {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE );
}
}
});
return rootView;
}
//For saving image(s)
private void SaveImage(Bitmap finalBitmap) {
String root = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
Log.v(getTag(), root);
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-" + n + ".jpg";
File file = new File(myDir,fname);
if (file.exists())file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG,90,out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
//Toast.makeText(getContext(), "Image is saved", Toast.LENGTH_LONG).show();
}
以下是我的图片代码
Pictures.java
import com.example.accurat.faisal.adapter.GridViewImageAdapter;
import com.example.accurat.faisal.helper.Utils;
import com.example.accurat.faisal.helper.AppConstant;
//other imports
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.pictures, container, false);
gridView = (GridView)rootView.findViewById(R.id.grid_view);
utils = new Utils(getContext());
// Initilizing Grid View
InitilizeGridLayout();
// loading all image paths from SD card
imagePaths = utils.getFilePaths();
// Gridview adapter
adapter = new GridViewImageAdapter(getActivity(), imagePaths, columnWidth);
/*adapter = new GridViewImageAdapter(Pictures.this, imagePaths,
columnWidth);*/
// setting grid view adapter
gridView.setAdapter(adapter);
return rootView;
}
private void InitilizeGridLayout() {
Resources r = getResources();
float padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
AppConstant.GRID_PADDING, r.getDisplayMetrics());
columnWidth = (int) ((utils.getScreenWidth() - ((AppConstant.NUM_OF_COLUMNS + 1) * padding)) / AppConstant.NUM_OF_COLUMNS);
gridView.setNumColumns(AppConstant.NUM_OF_COLUMNS);
gridView.setColumnWidth(columnWidth);
gridView.setStretchMode(GridView.NO_STRETCH);
gridView.setPadding((int) padding, (int) padding, (int) padding,
(int) padding);
gridView.setHorizontalSpacing((int) padding);
gridView.setVerticalSpacing((int) padding);
}
上述课程将访问helper
和adapter
方法。请参阅下面的帮助方法
Utils.java
public class Utils {
// Reading file paths from SDCard
public ArrayList<String> getFilePaths() {
ArrayList<String> filePaths = new ArrayList<String>();
File directory = new File(
android.os.Environment.getExternalStorageDirectory(),AppConstant.PHOTO_ALBUM);
// check for directory
if (directory.isDirectory()) {
// getting list of file paths
File[] listFiles = directory.listFiles();
// Check for count
if (listFiles.length > 0) {
// loop through all files
for (int i = 0; i < listFiles.length; i++) {
// get file path
String filePath = listFiles[i].getAbsolutePath();
// check for supported file extension
if (IsSupportedFile(filePath)) {
// Add image path to array list
filePaths.add(filePath);
}
}
} else {
// image directory is empty
Toast.makeText(
_context,
AppConstant.PHOTO_ALBUM
+ " is empty. Please load some images in it !",
Toast.LENGTH_LONG).show();
}
} else {
AlertDialog.Builder alert = new AlertDialog.Builder(_context);
alert.setTitle("Error!");
alert.setMessage(AppConstant.PHOTO_ALBUM
+ " directory path is not valid! Please set the image directory name AppConstant.java class");
alert.setPositiveButton("OK", null);
alert.show();
}
return filePaths;
}
}
此类中还有其他方法,但我只发布了上述方法,因为当我转到我的pictures
标签时,此方法首先被调用,它适用于图片标签,但是当我转到我的{ {1}}选项卡再次调用上面的方法并调用它,因为我的相机类中没有任何东西可以访问辅助类。我不知道为什么会发生这种情况,因为所有课程都是分开的所以它不应该像这样工作,我得到camera
有关清单,请参阅下文
null exception
以下是错误
<uses-permission android:name="com.example.accurat.faisal.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.hardware.camera"/>
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
FATAL EXCEPTION: main
Process: com.example.accurat.faisal, PID: 25624
java.lang.NullPointerException: Attempt to get length of null array
at com.example.accurat.faisal.helper.Utils.getFilePaths(Utils.java:42)
at com.example.accurat.faisal.Pictures.onCreateView(Pictures.java:45)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2184)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1298)
at android.support.v4.app.FragmentManagerImpl.moveFragmentsToInvisible(FragmentManager.java:2323)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2136)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2092)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1969)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:620)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1268)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:668)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:630)
at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:611)
at android.support.design.widget.TabLayout$ViewPagerOnTabSelectedListener.onTabSelected(TabLayout.java:2178)
at android.support.design.widget.TabLayout.dispatchTabSelected(TabLayout.java:1155)
at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1148)
at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1118)
at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1413)
at android.support.design.widget.TabLayout$TabView.performClick(TabLayout.java:1523)
at android.view.View$PerformClick.run(View.java:22596)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
是
Utlis.java:42
// check for directory
if (directory.isDirectory()) {
// getting list of file paths
File[] listFiles = directory.listFiles();
// Check for count
if (listFiles.length > 0) {// do the needful
为Pictures.java:42
更新1
在// loading all image paths from SD card
imagePaths = utils.getFilePaths();
和Mike M
我向每个帮助程序和适配器类导入Ruben2112
的建议之后,还在READ_EXTERNAL_STORAGE
中检查了listFiles
课程如下
Utils
现在我的应用程序没有崩溃,但它仍然显示警报框上的错误,如下图所示
点击确定后,应用仍会运行
我坚持不懈,不知道该怎么做。任何帮助都将受到高度赞赏。
答案 0 :(得分:0)
这不应该发生,因为我的Util类只导入了图片片段,而且与相机片段无关。
使用标签时要注意的一件重要事情是,正在打开的标签旁边的标签也会被加载。这样就可以加载标签,这样转换就更顺畅了,这是怎么回事可以滑动以转到下一个标签。