gpt4 book ai didi

java - Android SDK 错误 (BNR)

转载 作者:行者123 更新时间:2023-12-01 12:41:55 24 4
gpt4 key购买 nike

嘿伙计们,我正在尝试学习 Android SDK,我买了 Bill Phillips 和 Brian Hardy 写的《Big Nerd on Ranch》一书,但我被困在第 9 章,到处都是我无法解决的错误。我这里有 5 个错误,它们都说“无法解析(符号)”。所有这些符号都存在于我的程序中,所以我不明白为什么它找不到它们。这是我的主类 CrimeListFragment.java。代码有点长,但我用注释标记了所有错误出现的位置(可能需要向右滚动)。

CrimeListFragment.java

package criminalintent.android.bignerdranch.com.criminalintent;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.R;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

/**
* Created by Matt on 7/28/2014.
*/
public class CrimeListFragment extends ListFragment {
private ArrayList<Crime> mCrimes;
private static final String TAG = "CrimeListFragment";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setTitle(R.string.crime_title); //ERROR here 'crime_title'
mCrimes = CrimeLab.get(getActivity()).getCrimes();
CrimeAdapter adapter = new CrimeAdapter(mCrimes);
setListAdapter(adapter);

}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Crime c = ((CrimeAdapter)getListAdapter()).getItem(position);
Log.d(TAG, c.getTitle() + " was clicked");
}

private class CrimeAdapter extends ArrayAdapter<Crime> {

public CrimeAdapter(ArrayList<Crime> crimes) {
super(getActivity(), 0, crimes);
}

@Override
public View getView(int position, View convertView, ViewGroup part) {
//If we werent given a view, inflate one
if(convertView == null) {
convertView = getActivity().getLayoutInflater()
.inflate(R.layout.list_item_crime,null);//ERROR 'list_item_crime'
}

//Configure the view for this Crime
Crime c = getItem(position);

TextView titleTextView = (TextView)convertView.findViewById(R.id.crime_list_item_titleTextView);//ERROR 'crime_list_item_titleTextView'
titleTextView.setText(c.getTitle());
TextView dateTextView = (TextView)convertView.findViewById(R.id.crime_list_item_dateTextView);//ERROR 'crime_list_item_dateTextView'
dateTextView.setText(c.getDate().toString());
CheckBox solvedCheckBox = (CheckBox)convertView.findViewById(R.id.crime_list_item_solvedCheckBox);//ERROR 'crime_list_item_solvedCheckBox'
solvedCheckBox.setChecked(c.isSolved());

return convertView;
}
}
}

这是我的 strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">CriminalIntent</string>
<string name="title_activity_crime">CrimeActivity</string>
<string name="crime_title_hint">Enter a title for this crime</string>
<string name="crime_title_label">Title</string>
<string name="crime_details_label">Details</string>
<string name="crime_solved_label">Solved?</string>
<string name="crime_title">Crimes</string>

</resources>

和list_item_crime.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/crime_list_item_solvedCheckBox"
android:gravity="center"
android:layout_alignParentRight="true"
android:enabled="false"
android:padding="4dp"
android:focusable="false"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/crime_list_item_titleTextView"
android:layout_toLeftOf="@id/crime_list_item_solvedCheckBox"
android:textStyle="bold"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:text="Crime title"
/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/crime_list_item_dateTextView"
android:layout_below="@id/crime_list_item_titleTextView"
android:layout_toLeftOf="@id/crime_list_item_solvedCheckBox"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingTop="4dp"
android:text="Crime date"/>
</RelativeLayout>

如果您需要更多资源,请告诉我。谢谢您的帮助。

最佳答案

您的R导入错误(import android.R;)。删除该行并导入属于您的项目的行

关于java - Android SDK 错误 (BNR),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25024628/

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