Android Java:致命异常主空指针.我没有明确定义的线程.它说它无法实例化活动



我一直在寻找有关未处理循环异常错误的答案,但现在我收到一个新错误,即阻止我的活动工作的空指针。我的跑步说,"不幸的是[应用程序]已经停止了。我认为我的Unhandle循环异常与这个新的异常有关,因为我在这里读到它与线程有关:一个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)`

.....注意:我实际上把所有这些都打出来了。我不知道如何将整个事情保存到日志文件中......

来自我的主活动的代码:

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);
    }
}

移动这些行

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

在此行之后

setContentView(R.layout.activity_main); 

在 onCreate() 方法中。

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

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();
        }
    }
}

问题出在本节中:

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);  
}

注意:为了简洁起见,我没有输入其余的方法和关闭类括号。如果相关,请在我最初的问题中参考上面的内容。

sHolder 未定义。我想你不能总是相信教程。然后,MainActivity 中的行出现错误:

myImageView.setBackgroundColor(Color.BLUE);

我不知道为什么会这样,但我最近确实遇到了与 Eclipse 中的某些主题和样式不兼容的问题。也许有人可以就此事发表评论并启发我。

相关内容

  • 没有找到相关文章

最新更新