Java mCamera无法解析为变量(Android、Eclipse)
我对安卓开发非常陌生。我正在关注Google的Android“类”,并且在Eclipse中收到了一个错误代码:Java mCamera无法解析为变量(Android、Eclipse),java,android,eclipse,variables,Java,Android,Eclipse,Variables,我对安卓开发非常陌生。我正在关注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.
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无法解析”
如果你需要更多的信息,请告诉我
谢谢 您需要先声明
mCamera
,然后才能引用它:
public class MainActivity extends Activity {
Camera mCamera;
然后需要初始化它,可能是在onResume()中
确保您添加了适当的权限,以显示:
<uses-permission android:name="android.permission.CAMERA" />
是的,我现在明白了。他们只是展示了onPause()方法的一个示例。这并不意味着要包括在内。我想我会把这个问题留给下一个没有仔细阅读教程的可怜的sap哈,我想知道你是否真的想用这个相机。。。我还添加了一条关于声明
mTextView
的注释,如果您已经这样做了。
<uses-permission android:name="android.permission.CAMERA" />
public class MainActivity extends Activity {
Camera mCamera;
TextView mTextView;