gpt4 book ai didi

Android Java : Fatal exception main nullpointer. 我没有明确定义的线程。它说它无法实例化 Activity

转载 作者:行者123 更新时间:2023-11-30 02:59:14 24 4
gpt4 key购买 nike

我一直在寻找有关未处理的循环异常错误的答案,但现在我遇到了一个新错误,空指针阻止我的 Activity 工作。我的运行显示,“不幸的是 [app] 已停止。”我认为我的未处理循环异常与这个新异常有关,因为我在这里读到它与线程有关:a Wordpress blog

我似乎无法理解这些错误。首先,我尝试按照说明从 Unhandled event loop exception in plugin 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();
}
}

之后没有什么重要的。我的 list 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>

在开始和结束公共(public) list 代码之间。

编辑:来自 tutorial on 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);
}
}

最佳答案

移动这些行

ImageView myImageView = (ImageView) findViewById(R.id.imageView1);
Button myCaptureButton = (Button) findViewById(R.id.button1);

在这一行之后

setContentView(R.layout.activity_main); 

在 onCreate() 方法中。

因此将您的 Activity 代码更改为如下所示,

public class MainActivity extends ActionBarActivity 
{
cameraClass cameraView;
ImageView myImageView;
Button myCaptureButton;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cameraView = new cameraClass(getApplicationContext());
myImageView = (ImageView) findViewById(R.id.imageView1);
myCaptureButton = (Button) findViewById(R.id.button1);
myImageView.setBackgroundColor(Color.BLUE);
if (savedInstanceState == null)
{
getSupportFragmentManager().beginTransaction().add(R.id.container, new
PlaceholderFragment()).commit();
}
}
}

关于Android Java : Fatal exception main nullpointer. 我没有明确定义的线程。它说它无法实例化 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22801000/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com