我一直在寻找有关 Unhandled loop Exception 错误的答案,但现在我遇到了一个新错误,即 nullpointer,它阻止了我的 Activity 工作。我的跑步说,“不幸的是 [app] 已经停止。” 我认为我的未处理循环异常与这个新异常有关,因为我在这里读到它与线程相关:一个 Wordpress 博客
我似乎无法理解这些错误。首先,我尝试按照说明从插件 org.eclipse.ui 中的未处理事件循环异常中删除 jar 插件和功能文件,但这似乎根本没有改变这种情况。这是我的堆栈跟踪:
FATAL EXCEPTION: main
Process: erinmargotmasterson.expandingthink.alpha, PID: 1273
java.lang.RunTimeException: Unable to instantiate activity ComponentInfo(erinmargotmasterson.expandingthink.alpha/erinmargotmasterson.expandingthink.alpha.MainActivity): java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
at android.app.ActivityThread.HandleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by java.lang.NullPointerException
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)`
.....等等等等 注意:我实际上是把所有的都打出来了。我不知道如何将整个内容保存到日志文件中......
我的 MainActivity 中的代码:
package erinmargotmasterson.expandingthink.alpha;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends ActionBarActivity {
cameraClass cameraView = new cameraClass(getApplicationContext());
ImageView myImageView = (ImageView) findViewById(R.id.imageView1);
Button myCaptureButton = (Button) findViewById(R.id.button1);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myImageView.setBackgroundColor(Color.BLUE);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new
PlaceholderFragment()).commit();
}
}
之后就没什么意义了。我的清单 xml 是:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-feature
android:name="android.hardware.camera"
/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="erinmargotmasterson.expandingthink.alpha.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
在开始和结束的公共清单代码之间。
编辑:我提到的youtube教程中的cameraClass.java如下:
package erinmargotmasterson.expandingthink.alpha;
import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
public class cameraClass extends SurfaceView implements Callback {
private SurfaceHolder sHolder;
public Camera camera = null;
public cameraClass(Context context) {
super(context);
sHolder.getSurface();
sHolder.addCallback(this);
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int format, int width, int height) {
Camera.Parameters params = camera.getParameters();
params.setPreviewSize(width, height);
camera.setParameters(params);
camera.startPreview();
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
camera = Camera.open();
try {
camera.setPreviewDisplay(sHolder);
} catch (Exception e) { //remember the exception parameter!
e.printStackTrace();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
camera.stopPreview();
camera = null;
}
public void capture(Camera.PictureCallback jpegHandler) {
camera.takePicture(null, null, jpegHandler);
}
}