2

我对 Android 开发非常陌生。我正在关注 Google 的 Android“类”,并在 Eclipse 中收到此代码的错误:

package com.feistie.myfirstapp;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Initialize member TextView so we can manipulate it later
        mTextView = (TextView) findViewById(R.id.text_message);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // For the main activity, make sure the app icon in the action bar
            //does not behave as a buutton
            ActionBar actionBar = getActionBar();
            actionBar.setHomeButtonEnabled(false);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();  // Always call the superclass

        // Stop method tracing that the activity started during onCreate()
        android.os.Debug.stopMethodTracing();
    }

    @Override
    public void onPause() {
        super.onPause();  // Always call the superclass method first

        // Release the Camera because we don't need it when paused
        // and other activities might need to use it.
        if (mCamera != null) {
            mCamera.release();
            mCamera = null;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    /** Called when the user clicks the Send button */
    public void sendMessage (View view) {
        // Do Something in response to button
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }
}

这些行中的每一行都有一个错误:

if (mCamera != null) {
                mCamera.release();
                mCamera = null;
            }

第一行和第三行的错误是“mCamera 无法解析为变量”。第二行的错误只是说“无法解决 mCamera”。

如果您需要更多信息,请告诉我。

谢谢!

4

1 回答 1

2

您需要先声明mCamera,然后才能引用它:

public class MainActivity extends Activity {
    Camera mCamera;

然后你需要初始化它,可能在 onResume()

@Override
protected void onResume() {
    super.onResume();
    mCamera = Camera.open()
}

确保您添加了您清单的适当权限:

<uses-permission android:name="android.permission.CAMERA" />

添加

在您尝试在 Java 中使用它之前,您需要声明每个变量。我也看不到你在哪里声明mTextView

public class MainActivity extends Activity {
    Camera mCamera;
    TextView mTextView;
于 2012-08-04T20:00:00.210 回答