mCamera不能被解析为一个变量(Android的是,Eclipse)的是、变量、mCamera、Eclipse

2023-09-04 04:59:16 作者:寂寞粉碎年华

我很新的Andr​​oid开发。我下面谷歌的Andr​​oid类和我在Eclipse中收到一条错误此code:

I am very new to Android development. I am following Google's Android "classes" and am receiving an error for this code in 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);
    }
}

有一个错误的这些行:

There is an error for each of these lines:

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

有第一和第三行中的错误表示,mCamera不能解析为一个变量。第二行的错误只是说mCamera解决不了的。

The error for the first and third lines says "mCamera cannot be resolved to a variable." The error for the second line just says "mCamera cannot be resolved."

如果您需要了解更多信息,请让我知道。

If you need more information please let me know.

谢谢!

推荐答案

您需要声明 mCamera 之前,你可以参考吧:

You need to declare mCamera before you can reference it:

public class MainActivity extends Activity {
    Camera mCamera;

然后你需要初始化它,大概在onResume()

And then you need to initialize it, probably in onResume()

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

请确保你补充说,你表现出适当的权限:

Make sure you added the appropriate permission that you manifest:

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

添加

您需要在您尝试使用它在Java中declare_every_变量。我没有看到你声明 mTextView 无论是。

You need to declare_every_ variable before you try use it in Java. I don't see where you declare mTextView either.

public class MainActivity extends Activity {
    Camera mCamera;
    TextView mTextView;