在我开发的Android应用中,当我转移到某个活动时,该应用会崩溃。我看到很多人以前遇到过类似的问题,但前面的答案都没有解决我的问题。
以下是相关代码:
代码在此代码段的最后一行崩溃
public class StillsActivity extends SpatialFilteringActivity {
private static final int SELECT_PICTURE = 1;
private static final String TAG = "Stills";
private Uri mURI;
private Bitmap mBitmap;
private ImageView mImageView;
private Mat mImToProcess = new Mat();
private Mat mImGray = new Mat();
private Mat mFilteredImage = new Mat();
private SeekBar mSeekBarSpatial;
private SeekBar mSeekBarIntensity;
private SeekBar mSeekBarAlpha;
private SeekBar mSeekBarBeta;
private TextView mTextViewSpatial;
private TextView mTextViewIntensity;
private TextView mTextViewAlpha;
private TextView mTextViewBeta;
private MenuItem mSelectedItem;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_PICTURE) {
if (resultCode == RESULT_OK) {
mURI = data.getData();
if (mURI != null) {
try {
mBitmap = Util.getBitmap(this, mURI);
mImageView.setImageBitmap(Util.getResizedBitmap(mBitmap,
1000));
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stills);
// APP CRASHES HERE
mImageView = (ImageView) findViewById(R.id.imageView1);
Button loadButton = (Button) findViewById(R.id.loadButton);
loadButton.setOnClickListener(new View.OnClickListener() {
@SuppressLint("SimpleDateFormat")
@Override
public void onClick(View v) {
Log.i(TAG, "onClick event");
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select an image"), SELECT_PICTURE);
}
});
mSeekBarSpatial = (SeekBar) findViewById(R.id.seekBarSpatial);
mTextViewSpatial = (TextView) findViewById(R.id.sigmaSpatialTextView);
setSeekBar(mSeekBarSpatial, mTextViewSpatial, getResources().getString(R.string.stringSpatial), MyImageProc.SIGMA_SPATIAL_MAX);
mSeekBarIntensity = (SeekBar) findViewById(R.id.seekBarIntensity);
mTextViewIntensity = (TextView) findViewById(R.id.sigmaIntensityTextView);
setSeekBar(mSeekBarIntensity, mTextViewIntensity, getResources().getString(R.string.stringIntensity), MyImageProc.SIGMA_INTENSITY_MAX);
mSeekBarAlpha = (SeekBar) findViewById(R.id.seekBarAlpha);
mTextViewAlpha = (TextView) findViewById(R.id.alphaTextView);
setSeekBar(mSeekBarAlpha, mTextViewAlpha, getResources().getString(R.string.stringAlpha), MyImageProc.ALPHA_MAX);
mSeekBarBeta = (SeekBar) findViewById(R.id.seekBarBeta);
mTextViewBeta = (TextView) findViewById(R.id.betaTextView);
setSeekBar(mSeekBarBeta, mTextViewBeta, getResources().getString(R.string.stringBeta), MyImageProc.BETA_MAX);
}
// etc.
public class SpatialFilteringActivity extends AppCompatActivity {
private static final String TAG = "SpatialFiltering";
//menu members
private SubMenu mResolutionSubMenu;
private SubMenu mCameraSubMenu;
//flags
private Boolean mSettingsMenuAvailable =false;
protected static final int SETTINGS_GROUP_ID = 1;
protected static final int RESOLUTION_GROUP_ID = 2;
protected static final int CAMERA_GROUP_ID = 3;
protected static final int DEFAULT_GROUP_ID = 4;
protected static final int COLOR_GROUP_ID = 5;
protected static final int FILTER_GROUP_ID = 6;
protected static final int STILL_GROUP_ID = 7;
private MyJavaCameraView mOpenCvCameraView;
private CameraListener mCameraListener = new CameraListener();
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
Log.i(TAG, "OpenCV loaded successfully");
mOpenCvCameraView.enableView();
break;
default:
super.onManagerConnected(status);
break;
}
}
};
private String[] mCameraNames = {"Front", "Rear"};
private int[] mCameraIDarray = {CameraBridgeViewBase.CAMERA_ID_FRONT, CameraBridgeViewBase.CAMERA_ID_BACK};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spatial_filtering);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
mOpenCvCameraView = (MyJavaCameraView) findViewById(R.id.Java_Camera_View);
mOpenCvCameraView.setCameraIndex(CameraBridgeViewBase.CAMERA_ID_ANY);
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
mOpenCvCameraView.setCvCameraViewListener(mCameraListener);
Button saveButton = (Button) findViewById(R.id.saveButton);
saveButton.setOnClickListener(new View.OnClickListener() {
@SuppressLint("SimpleDateFormat")
@Override
public void onClick(View v) {
Log.i(TAG, "onClick event");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
String currentDateandTime = sdf.format(new Date());
String fileName = Environment.getExternalStorageDirectory().getPath() +
"/sample_picture_" + currentDateandTime + ".jpg";
mOpenCvCameraView.takePicture(fileName);
addImageToGallery(fileName, SpatialFilteringActivity.this);
Toast.makeText(SpatialFilteringActivity.this, fileName + " saved",
Toast.LENGTH_SHORT).show();
}
});
}
//etc.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="il.ac.tau.adviplab.iplab2_hn.StillsActivity">
<ImageView
android:contentDescription="@string/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView1"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:src="@drawable/loadimage"
android:adjustViewBounds="true"
android:layout_centerVertical="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/load_button"
android:id="@+id/loadButton"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/seekBarSpatial"
android:layout_marginTop="13dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignLeft="@+id/sigmaSpatialTextView"
android:layout_alignStart="@+id/sigmaSpatialTextView"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:id="@+id/sigmaSpatialTextView"
android:text="@string/stringSpatial"
android:layout_below="@+id/seekBarSpatial"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/seekBarIntensity"
android:layout_marginTop="15dp"
android:layout_below="@+id/sigmaSpatialTextView"
android:layout_alignLeft="@+id/sigmaSpatialTextView"
android:layout_alignStart="@+id/sigmaSpatialTextView"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:id="@+id/sigmaIntensityTextView"
android:text="@string/stringIntensity"
android:layout_below="@+id/seekBarIntensity"
android:layout_alignLeft="@+id/seekBarIntensity"
android:layout_alignStart="@+id/seekBarIntensity"/>
<SeekBar
android:id="@+id/seekBarAlpha"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_below="@+id/sigmaIntensityTextView"
android:layout_alignLeft="@+id/alphaTextView"
android:layout_alignStart="@+id/alphaTextView"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/alphaTextView"
android:text="@string/stringAlpha"
android:layout_marginTop="12dp"
android:layout_below="@+id/seekBarAlpha"
android:layout_alignLeft="@+id/sigmaIntensityTextView"
android:layout_alignStart="@+id/sigmaIntensityTextView"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/seekBarBeta"
android:layout_marginTop="8dp"
android:layout_below="@+id/alphaTextView"
android:layout_alignLeft="@+id/alphaTextView"
android:layout_alignStart="@+id/alphaTextView"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/betaTextView"
android:text="@string/stringBeta"
android:layout_below="@+id/seekBarBeta"
android:layout_alignLeft="@+id/seekBarBeta"
android:layout_alignStart="@+id/seekBarBeta"/>
</RelativeLayout>
注意:@ drawable / loadimage是1000 * 978像素,如果重要的话
java.lang.RuntimeException: Unable to start activity ComponentInfo{il.ac.tau.adviplab.iplab2_hn/il.ac.tau.adviplab.iplab2_hn.StillsActivity}: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class <unknown>
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
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:5466)
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: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class <unknown>
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at il.ac.tau.adviplab.iplab2_hn.StillsActivity.onCreate(StillsActivity.java:73)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
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:5466)
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: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:645)
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at il.ac.tau.adviplab.iplab2_hn.StillsActivity.onCreate(StillsActivity.java:73)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
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:5466)
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.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at android.view.LayoutInflater.createView(LayoutInflater.java:619)
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at il.ac.tau.adviplab.iplab2_hn.StillsActivity.onCreate(StillsActivity.java:73)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
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:5466)
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.UnsupportedOperationException: Can't convert to dimension: type=0x1
at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:666)
at android.view.View.<init>(View.java:3969)
at android.view.ViewGroup.<init>(ViewGroup.java:573)
at android.widget.RelativeLayout.<init>(RelativeLayout.java:248)
at android.widget.RelativeLayout.<init>(RelativeLayout.java:244)
at android.widget.RelativeLayout.<init>(RelativeLayout.java:240)
at java.lang.reflect.Constructor.newInstance(Native Method)
at android.view.LayoutInflater.createView(LayoutInflater.java:619)
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at il.ac.tau.adviplab.iplab2_hn.StillsActivity.onCreate(StillsActivity.java:73)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
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:5466)
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)
我很感激任何帮助。
谢谢!
答案 0 :(得分:0)
请遵循背景图片尺寸
对于支持的设备
LDPI:
Portrait: 200x320px
Landscape: 320x200px
MDPI:
Portrait: 320x480px
Landscape: 480x320px
HDPI:
Portrait: 480x800px
Landscape: 800x480px
XHDPI:
Portrait: 720px1280px
Landscape: 1280x720px