我寫了一個簡單的應用程序來顯示ListView
中的數據。該應用程序沒有錯誤,但在運行時停止並且不運行。應用程序無法啓動
以下是main.java文件:
public class MainActivity extends Activity {
ArrayList<TimeRecord> times = new ArrayList<MainActivity.TimeRecord>();
ListView listView = (ListView) findViewById(R.id.times_list);
TimeTrackerAdapter timeTrackerAdapter = new TimeTrackerAdapter();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView.setAdapter(timeTrackerAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class TimeRecord{
private String time;
private String notes;
public TimeRecord(String time, String notes){
this.time = time;
this.notes = notes;
}
public String getTime() {return this.time;}
public void setTime(String time){this.time = time;}
public String getNotes(){return this.notes;}
public void setNotes(String notes){this.notes = notes;}
}
public class TimeTrackerAdapter extends BaseAdapter{
TimeTrackerAdapter(){
times.add(new TimeRecord("38:23", "Feeling good!"));
times.add(new TimeRecord("49:01", "Tired. Needed more caffeine"));
times.add(new TimeRecord("26:21", "I’m rocking it!"));
times.add(new TimeRecord("29:42", "Lost some time on the hills, but pretty good."));
}
@Override
public int getCount() {
return times.size();
}
@Override
public Object getItem(int index) {
return times.get(index);
}
@Override
public long getItemId(int index){
return index;
}
@Override
public View getView(int index, View view, ViewGroup parent) {
if (view == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
view = inflater.inflate(R.layout.time_list_item, parent, false);
}
TimeRecord time = times.get(index);
TextView timeTextView = (TextView) findViewById(R.id.time_view);
timeTextView.setText(time.getTime());
TextView noteTextView = (TextView) findViewById(R.id.notes_view);
noteTextView.setText(time.getNotes());
return view;
}
}
}
,這是清單的內容:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.timelistrecords"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="2"
android:targetSdkVersion="18" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.timelistrecords.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我嘗試添加任何代碼之前運行的項目,它正在運行,所以我不認爲這是android版本問題。我沒有使用任何硬件,所以不應該有權限問題。 有什麼想法?
初始化列表視圖... –
什麼是 「不跑」 呢?你有錯誤嗎?請發佈logcat。 –
@JerryWattre它沒有幫助 –