在这个活动中,我有一个AppCompatCallback的实现和一个设置我的工具栏的委托,我有其他2个活动的这个东西,并且工作正常,但在这里我得到一个错误的行delegate1.setSupportActionBar(工具栏)..我不明白为什么......
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatCallback;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.view.ActionMode;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
public class AlbumActivity extends Activity implements AppCompatCallback {
private final int REQUEST_CODE_CAMERA_IMAGE = 1000;
private final int REQUEST_CODE_EXTERNAL_IMAGE = 2000;
private AppCompatDelegate delegate1;
String nameAlbum;
// Declare variables
private String[] FilePathStrings;
private String[] FileNameStrings;
private File[] listFile;
GridView grid;
GridViewAdapter adapter;
File file;
boolean deleted;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
delegate1 = AppCompatDelegate.create(this, this);
//call the onCreate() of the AppCompatDelegate
delegate1.onCreate(savedInstanceState);
//use the delegate to inflate the layout
delegate1.setContentView(R.layout.album_activity);
Toolbar toolbar = (Toolbar) findViewById(R.id.mytoolbarr);
delegate1.setSupportActionBar(toolbar);
delegate1.setTitle("Your Pictures");
Button btnChoosePicture = (Button) findViewById(R.id.addimage);
Intent intent = getIntent();
nameAlbum = intent.getStringExtra("nameAlbum");
// Check for SD Card
if (!Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
Toast.makeText(this, "Error! No SDCARD Found!", Toast.LENGTH_LONG)
.show();
} else {
// Locate the image folder in your SD Card
file = new File(Environment.getExternalStorageDirectory()
+ File.separator + nameAlbum);
if (file.isDirectory()) {
listFile = file.listFiles();
// Create a String array for FilePathStrings
FilePathStrings = new String[listFile.length];
// Create a String array for FileNameStrings
FileNameStrings = new String[listFile.length];
for (int i = 0; i < listFile.length; i++) {
// Get the path of the image file
FilePathStrings[i] = listFile[i].getAbsolutePath();
// Get the name image file
FileNameStrings[i] = listFile[i].getName();
}
}
// Locate the GridView in gridview_main.xml
grid = (GridView) findViewById(R.id.gridview);
// Pass String arrays to LazyAdapter Class
adapter = new GridViewAdapter(this, FilePathStrings, FileNameStrings);
// Set the LazyAdapter to the GridView
grid.setAdapter(adapter);
// Capture gridview item click
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent i = new Intent(AlbumActivity.this, ViewImage.class);
// Pass String arrays FilePathStrings
i.putExtra("filepath", FilePathStrings);
// Pass String arrays FileNameStrings
i.putExtra("filename", FileNameStrings);
// Pass click position
i.putExtra("position", position);
startActivity(i);
}
});
grid.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, final View view, final int position, final long id) {
AlertDialog.Builder builder = new AlertDialog.Builder(AlbumActivity.this);
builder.setCancelable(true);
builder.setMessage("Are you sure you want to delete this picture ?");
builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
File filepath = Environment.getExternalStorageDirectory();
File dir5 = new File(filepath.getAbsolutePath()
+ nameAlbum+FileNameStrings[position]);
File file3 = new File(String.valueOf(dir5));
deleted = file3.delete();
adapter.notifyDataSetChanged();
finish();
startActivity(getIntent());
dialog.dismiss();
}
});
builder.setTitle("Delete Picture");
AlertDialog dialog = builder.create();
dialog.show();
return true;
}
});
}
//select picture from external storage
btnChoosePicture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// choose picture from gallery
Intent PhotoPickerIntent = new Intent(
Intent.ACTION_PICK);
File pictureDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String pictureDirectoryPath = pictureDirectory.getPath();
Uri data = Uri.parse(pictureDirectoryPath);
PhotoPickerIntent.setDataAndType(data, "image/*");
startActivityForResult(PhotoPickerIntent,
REQUEST_CODE_EXTERNAL_IMAGE);
}
});
}
@Override
答案 0 :(得分:0)
但是这里我遇到了一行代码:delegate1.setSupportActionBar(toolbar)
这可能是两件不同的事情:
您已导入android.widget.Toolbar
而不是 android.support.v7.widget.Toolbar
如果您想使用Activity
AppCompatActivity
必须展开setSupportActionBar(toolbar)
如果您延长AppCompatActivity
,则必须使用Theme.AppCompat.Light.NoActionBar
答案 1 :(得分:0)
@Skizo这里一切正常,并扩展了活动......
public class EnterDataActivity extends Activity implements AppCompatCallback {
private AppCompatDelegate delegate;
EditText editTextPersonName;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
delegate = AppCompatDelegate.create(this, this);
//call the onCreate() of the AppCompatDelegate
delegate.onCreate(savedInstanceState);
//use the delegate to inflate the layout
delegate.setContentView(R.layout.enter_data);
editTextPersonName = (EditText) findViewById(R.id.et_person_name);
Toolbar toolbar= (Toolbar) findViewById(R.id.mytoolbar2);
delegate.setSupportActionBar(toolbar);
delegate.setTitle("Enter Album Name");
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}else if(id==android.R.id.home){
finish();
}
return super.onOptionsItemSelected(item);
}
public void onClickAdd (View btnAdd) {
String personName = editTextPersonName.getText().toString();
if ( personName.length() != 0 ) {
Intent newIntent = getIntent();
newIntent.putExtra("tag_person_name", personName);
this.setResult(RESULT_OK, newIntent);
finish();
}
}
@Override
public void onSupportActionModeStarted(ActionMode mode) {
}
@Override
public void onSupportActionModeFinished(ActionMode mode) {
}
@Nullable
@Override
public ActionMode onWindowStartingSupportActionMode(ActionMode.Callback callback) {
return null;
}
}
答案 2 :(得分:0)
我确实解决了我的问题,我必须在我的活动布局中包含我的工具栏布局...感谢大家有兴趣解决我的问题!!