gpt4 book ai didi

java - 应用程序在设备上停止(无终端错误)- 不显示 ListView

转载 作者:太空宇宙 更新时间:2023-11-04 10:51:54 25 4
gpt4 key购买 nike

我尝试在Android中制作一个简单的ListView。

activity_labas.xml:

    <TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:text="@string/title_home"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/title_home2"/>

<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation" />

</android.support.constraint.ConstraintLayout>

labas.java:

package com.labas.holiday.labas;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.TextView;

public class labas extends AppCompatActivity {

private TextView mTextMessage;
private TextView mTextMessage2;

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.title_home);


return true;
case R.id.navigation_dashboard:
mTextMessage.setText(R.string.title_dashboard);
mTextMessage2.setText(R.string.title_dashboard2);
return true;
case R.id.navigation_notifications:
mTextMessage.setText(R.string.title_notifications);
return true;
}
return false;
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_labas);

mTextMessage2 = (TextView) findViewById(R.id.message);
mTextMessage = (TextView) findViewById(R.id.message);
Log.i("myTag2", "STARTINGBOOTOM");
Intent myIntent = new Intent(labas.this, ListViewLoader.class);
//myIntent.putExtra("key", value); //Optional parameters
labas.this.startActivity(myIntent);

//setContentView(R.layout);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}

}

ListViewLoader

package com.labas.holiday.labas;

import android.app.ListActivity;
import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.constraint.ConstraintLayout;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleCursorAdapter;

public class ListViewLoader extends ListActivity
implements LoaderManager.LoaderCallbacks<Cursor> {
// This is the Adapter being used to display the list's data
SimpleCursorAdapter mAdapter;

// These are the Contacts rows that we will retrieve
static final String[] PROJECTION = new String[] {ContactsContract.Data._ID,
ContactsContract.Data.DISPLAY_NAME};

// This is the select criteria
static final String SELECTION = "((" +
ContactsContract.Data.DISPLAY_NAME + " NOTNULL) AND (" +
ContactsContract.Data.DISPLAY_NAME + " != '' ))";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("myTag", "STAAAAAAAAAAAAAAAAAARRRRRRRRRRRRRRRRRTTTTTTTT");
// Create a progress bar to display while the list loads
ProgressBar progressBar = new ProgressBar(this);
progressBar.setLayoutParams(new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT,
ConstraintLayout.LayoutParams.WRAP_CONTENT));
progressBar.setIndeterminate(true);
getListView().setEmptyView(progressBar);

// Must add the progress bar to the root of the layout
ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
root.addView(progressBar);

// For the cursor adapter, specify which columns go into which views
String[] fromColumns = {ContactsContract.Data.DISPLAY_NAME};
int[] toViews = {android.R.id.text1}; // The TextView in simple_list_item_1

// Create an empty adapter we will use to display the loaded data.
// We pass null for the cursor, then update it in onLoadFinished()
mAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, null,
fromColumns, toViews, 0);
setListAdapter(mAdapter);

// Prepare the loader. Either re-connect with an existing one,
// or start a new one.
getLoaderManager().initLoader(0, null, this);
}


@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
return new CursorLoader(this, ContactsContract.Data.CONTENT_URI,
PROJECTION, SELECTION, null, null);
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
mAdapter.swapCursor(cursor);
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
}

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.labas.holiday.labas">
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".labas"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="ListViewLoader" android:label="@string/title_activity_test"/>
<activity android:name="Testing" android:label="@string/title_activity_test"/>
</application>
</manifest>

结果应该是应用程序中的联系人列表。

目前,代码在 IDE 中不会引发任何错误,但会在设备上加载应用程序时停止。在 Debug模式下,它会在“return new CursorLoader(this, ContactsContract.Data.CONTENT_URI,PROJECTION, SELECTION, null, null);”处崩溃。

我没有看到空指针(我不会打赌^^)。

最佳答案

光标无法检索数据的原因之一是您缺少代码的运行时权限,您需要在添加 list 的同时请求运行时权限

类似于:

private static final int INITIAL_REQUEST_NUMBER=1337;
private static final String[] INITIAL_PERMESSION={android.Manifest.permission.READ_CONTACTS};
ActivityCompat.requestPermissions(this, INITIAL_PERMESSION, INITIAL_REQUEST_NUMBER);

了解更多详情:https://developer.android.com/training/permissions/requesting.html

您需要确保用户在运行游标加载程序之前授予您权限,因此最好在运行加载程序之前请求 Activity 权限,这样如果用户拒绝权限(在首次运行期间),您将取消运行加载程序。

即使在第一次运行期间授予用户权限,用户也可以撤销权限,因此最好在每次运行加载程序之前检查权限是否已授予,如果撤销,您将取消加载程序。

希望许可是您需要考虑的唯一问题。

关于java - 应用程序在设备上停止(无终端错误)- 不显示 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47737258/

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