应用程序无法启动

时间:2014-01-30 05:29:26

标签: android runtime-error

我写了一个简单的应用程序来显示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版本问题。我没有使用任何硬件,所以也不应该有许可问题。 任何的想法?

2 个答案:

答案 0 :(得分:2)

onCreate之后setContentView初始化。你可能有NullPointerException。如果你的应用程序在堆栈跟踪后仍有问题。 findViewById在当前的infalted布局中查找id为id的视图。因此,您需要先将布局内容设置为活动,然后初始化视图。

 ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); // fisrt
    listView = (ListView) findViewById(R.id.times_list); // initialize listview
    listView.setAdapter(timeTrackerAdapter);
}

getView中使用视图对象来初始化视图。

 TextView timeTextView = (TextView) view.findViewById(R.id.time_view);
 TextView noteTextView = (TextView) view.findViewById(R.id.notes_view);

此外,您应该考虑使用ViewHolder模式

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

答案 1 :(得分:0)

ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); // fisrt
    listView = (ListView) findViewById(R.id.times_list); // initialize listview
    listView.setAdapter(timeTrackerAdapter);
}


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)view. findViewById(R.id.time_view);//view. here
        timeTextView.setText(time.getTime());
        TextView noteTextView = (TextView)view.findViewById(R.id.notes_view);//view. here
        noteTextView.setText(time.getNotes());
        return view;
    }