gpt4 book ai didi

java - 如何将ListActivity转换为AppCompatActivity

转载 作者:行者123 更新时间:2023-12-01 21:57:03 26 4
gpt4 key购买 nike

我想将我的 Activity 父级从ListActivity更改为AppCompatActivity,因为我需要使用检查权限授予并且它需要AppCompat,但我的 Activity 是ListView。我尝试了这个操作,但没有收到好的结果:(

这是我的源代码(曾经有...是为了没有添加代码):

public class RingtoneSelectActivity extends ListActivity {
...
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
mShowAll = false;

String status = Environment.getExternalStorageState();
if (status.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
showFinalAlert(getResources().getText(R.string.err_sdcard_readonly));
return;
}
if (status.equals(Environment.MEDIA_SHARED)) {
showFinalAlert(getResources().getText(R.string.err_sdcard_shared));
return;
}
if (!status.equals(Environment.MEDIA_MOUNTED)) {
showFinalAlert(getResources().getText(R.string.err_no_sdcard));
return;
}

Intent intent = getIntent();
mWasGetContentIntent = intent.getAction().equals(
Intent.ACTION_GET_CONTENT);

// Inflate our UI from its XML layout description.
setContentView(R.layout.media_select);


SplashHandler mHandler = new SplashHandler();


Message msg = new Message();
//Assign a unique code to the message.
//Later, this code will be used to identify the message in Handler class.
msg.what = 0;
// Send the message with a delay of 3 seconds(3000 = 3 sec).
mHandler.sendMessageDelayed(msg, 10000);

try {
mAdapter = new SimpleCursorAdapter(
this,
// Use a template that displays a text view
R.layout.media_select_row,
// Give the cursor to the list adatper
createCursor(""),
// Map from database columns...
new String[]{
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media._ID},
// To widget ids in the row layout...
new int[]{
R.id.row_artist,
R.id.row_album,
R.id.row_title,
R.id.row_icon,
R.id.row_options_button});

setListAdapter(mAdapter);

getListView().setItemsCanFocus(true);

// Normal click - open the editor
getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent,
View view,
int position,
long id) {
startRingdroidEditor();
}
});

} catch (SecurityException e) {
// No permission to retrieve audio?
Log.e("Ringtone", e.toString());

// todo error 1
} catch (IllegalArgumentException e) {
// No permission to retrieve audio?
Log.e("Ringtone", e.toString());

// todo error 2
}

mAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view,
Cursor cursor,
int columnIndex) {
if (view.getId() == R.id.row_options_button) {
// Get the arrow image view and set the onClickListener to open the context menu.
ImageView iv = (ImageView) view;
iv.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
openContextMenu(v);
}
});
return true;
} else if (view.getId() == R.id.row_icon) {
setSoundIconFromCursor((ImageView) view, cursor);
return true;
}

return false;
}
});

// Long-press opens a context menu
registerForContextMenu(getListView());

}
}
...
}

我改成这样:

public class RingtoneSelectActivity extends AppCompatActivity {

但是我在这行中收到了一些错误:

...
setListAdapter(mAdapter);

getListView().setItemsCanFocus(true);

getListView().setOnItemClickListener(new OnItemClickListener() { ... });
...
registerForContextMenu(getListView());

我的错误:

  • 无法解析方法“setListAdapter(android.widget.SimpleCursorAdapter)”
  • 无法解析方法“getListView()”

如何修复该错误?

[编辑]

我的LogCat:

E/ACRA: ACRA caught a NullPointerException for ir.ari.mp3cutter
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SimpleCursorAdapter.changeCursor(android.database.Cursor)' on a null object reference
at ir.ari.mp3cutter.RingtoneSelectActivity.refreshListView(RingtoneSelectActivity.java:621)
at ir.ari.mp3cutter.RingtoneSelectActivity.onOptionsItemSelected(RingtoneSelectActivity.java:314)
at android.app.Activity.onMenuItemSelected(Activity.java:2908)
at com.android.internal.policy.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1151)
at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:761)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:904)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:894)
at com.android.internal.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:200)
at android.widget.AdapterView.performItemClick(AdapterView.java:310)
at android.widget.AbsListView.performItemClick(AbsListView.java:1145)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3042)
at android.widget.AbsListView$3.run(AbsListView.java:3879)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

第 304 行到 319 行:

    @Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_about:
RingtoneEditActivity.onAbout(this);
return true;
case R.id.action_record:
onRecord();
return true;
case R.id.action_show_all_audio:
mShowAll = true;
refreshListView();
return true;
default:
return false;
}
}

第 619 至 622 行:

    private void refreshListView() {
String filterStr = mFilter.getQuery().toString();
mAdapter.changeCursor(createCursor(filterStr));
}

[注意:我很抱歉我的口语不好,因为我英语学得不好:)

最佳答案

所以不要更改父类(super class),只需使用 ContextCompat.checkSelfPermission() 检查未授予的权限即可然后通过ActivityCompat.requestPermissions()请求许可并得到结果:

@Override
public void onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}

关于java - 如何将ListActivity转换为AppCompatActivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58736983/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com