带有2个TextView和1个按钮的Android ListView

时间:2013-05-02 01:01:44

标签: java android android-listview onclicklistener

我正在尝试使用2个TextViews和1个Button创建一个ListView。当用户点击按钮时,活动将完成();并将结果传递给另一个视图。我跟着this tutorial 。但出于某种原因,当我点击开始该活动时,我的应用程序崩溃了!这是我的应用程序中的3个相关文件。

列表结构数据:

public class formulasListData {
private String formulasName;
private String formulasDefinition;

public formulasListData(String formulasName, String formulasDefinition) {
    super();
    this.formulasName = formulasName;
    this.formulasDefinition = formulasDefinition;
}

public String getFormulasName() {
    return formulasName;
}

public void setFormulasName(String formulasName) {
    this.formulasName = formulasName;
}

public String getFormulasDefinition() {
    return formulasDefinition;
}

public void setFormulasDefinition(String formulasDefinition) {
    this.formulasDefinition = formulasDefinition;
    }
}

列表适配器:

public class formulasListAdapter extends BaseAdapter implements OnClickListener {
private Context context;
private List<formulasListData> formulasList;

public formulasListAdapter(Context context, List<formulasListData> formulasList) {
    this.context = context;
    this.formulasList = formulasList;
}

@Override
public int getCount() {
    return formulasList.size();
}

@Override
public Object getItem(int position) {
    return formulasList.get(position);
}

@Override
public long getItemId(int position) {

    return position;
}

@Override
public View getView(int position, View rowView, ViewGroup parent) {
    formulasListData entry = formulasList.get(position);
    if(rowView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView = inflater.inflate(R.layout.formulas_row, null);
    }

    TextView formulasName = (TextView) rowView.findViewById(R.id.formulas_name);
    formulasName.setText(entry.getFormulasName());

    TextView formulasDefinition = (TextView) rowView.findViewById(R.id.formulas_definition);
    formulasDefinition.setText(entry.getFormulasDefinition());

    Button btnSelect = (Button) rowView.findViewById(R.id.selectFormula);
    btnSelect.setFocusableInTouchMode(false);
    btnSelect.setFocusable(false);
    btnSelect.setOnClickListener(this);

    btnSelect.setTag(entry);

    return rowView;
}
@Override

public void onClick(View view) {
    formulasListData entry = (formulasListData) view.getTag();
    }

}

应显示列表的活动。

public class FormulasList extends Activity {

private static final int FORMULA_0 = 0;
private static final int FORMULA_1 = 1;
private static final int FORMULA_2 = 2;
private static final int FORMULA_3 = 3;
private static final int FORMULA_4 = 4;
private static final int FORMULA_5 = 5;
private static final int FORMULA_6 = 6;
// private static final int FORMULA_7 = 7;

public void onCreate(Bundle savedInstance) {
    super.onCreate(savedInstance);

    ListView formulasListView = (ListView) findViewById(R.id.formulas_list);
    formulasListView.setClickable(true);
    final List<formulasListData> formulasList = new ArrayList<formulasListData>();

    final String[] formulasName = new String[] {"Circunference of a Circle:",
                                "Area of a Circle:",
                                "Volume of a Sphere:",
                                "Area Triangle:", 
                                "Area Square:",
                                "Volume of a Cube:",
                                "Pithagorean Theorem:"};
    final String[] formulasDefinition = new String[] {"2 × π × r",
                                        "π × r²",
                                        "(4/3) × π × r³",
                                        "(b × h)/2",
                                        "side²",
                                        "side³",
                                        "a² + b² = c²"};        
    // formulasList.add(new formulasListData(formulasName, formulasDefinition));
    formulasList.add(new formulasListData(formulasName[0], formulasDefinition[0]));
    formulasList.add(new formulasListData(formulasName[1], formulasDefinition[1]));
    formulasList.add(new formulasListData(formulasName[2], formulasDefinition[2]));
    formulasList.add(new formulasListData(formulasName[3], formulasDefinition[3]));
    formulasList.add(new formulasListData(formulasName[4], formulasDefinition[4]));
    formulasList.add(new formulasListData(formulasName[5], formulasDefinition[5]));
    formulasList.add(new formulasListData(formulasName[6], formulasDefinition[6]));

    SharedPreferences settings = getSharedPreferences(SettingsActivity.PREF_NAME, MODE_PRIVATE);
    SettingsActivity.newTheme = settings.getInt("themeCustom", 0);

     if(SettingsActivity.newTheme == SettingsActivity.THEME_DARK) {
            setTheme(R.style.DarkTheme);
        } else if(SettingsActivity.newTheme == SettingsActivity.THEME_LIGHT){
            setTheme(R.style.LightTheme);
        } else if(SettingsActivity.newTheme == SettingsActivity.THEME_COLORS) {
            setTheme(R.style.ColorsTheme);
        } else { 
            setTheme(R.style.AppTheme);
         }
    setContentView(R.layout.activity_formulas);

    formulasListAdapter adapter = new formulasListAdapter(this, formulasList);

    formulasListView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int position,long index) {
            Intent intent= getIntent();
            Bundle b = new Bundle();
            switch (position) {
            case FORMULA_0:
                b.putString("key0", formulasDefinition[0]);
                intent.putExtras(b);
                setResult(RESULT_OK, intent);
                finish();
                break;
            case FORMULA_1:
                b.putString("key0", formulasDefinition[1]);
                intent.putExtras(b);
                setResult(RESULT_OK, intent);
                finish();
                break;
            case FORMULA_2:
                b.putString("key0", formulasDefinition[2]);
                intent.putExtras(b);
                setResult(RESULT_OK, intent);
                finish();
                break;
            case FORMULA_3:
                b.putString("key0", formulasDefinition[3]);
                intent.putExtras(b);
                setResult(RESULT_OK, intent);
                finish();
                break;
            case FORMULA_4:
                b.putString("key0", formulasDefinition[4]);
                intent.putExtras(b);
                setResult(RESULT_OK, intent);
                finish();
                break;
            case FORMULA_5:
                b.putString("key0", formulasDefinition[5]);
                intent.putExtras(b);
                setResult(RESULT_OK, intent);
                finish();
                break;
            case FORMULA_6:
                b.putString("key0", formulasDefinition[6]);
                intent.putExtras(b);
                setResult(RESULT_OK, intent);
                finish();
                break;
            default:
                break;
            }               
        }
    });     
    formulasListView.setAdapter(adapter);   
    }
}

非常感谢!!

编辑:这是LogCat

05-01 20:06:54.436: D/libEGL(4820): loaded /system/lib/egl/libEGL_tegra.so
05-01 20:06:54.456: D/libEGL(4820): loaded /system/lib/egl/libGLESv1_CM_tegra.so
05-01 20:06:54.466: D/libEGL(4820): loaded /system/lib/egl/libGLESv2_tegra.so
05-01 20:06:54.486: D/OpenGLRenderer(4820): Enabling debug mode 0
05-01 20:06:54.536: D/dalvikvm(4820): GC_CONCURRENT freed 170K, 4% free 7509K/7808K, paused 4ms+10ms, total 38ms
05-01 20:06:59.976: D/AndroidRuntime(4820): Shutting down VM
05-01 20:06:59.976: W/dalvikvm(4820): threadid=1: thread exiting with uncaught exception (group=0x40cde930)
05-01 20:06:59.976: E/AndroidRuntime(4820): FATAL EXCEPTION: main
05-01 20:06:59.976: E/AndroidRuntime(4820): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gabilheri.marcuscalculatorv2/com.gabilheri.marcuscalculatorv2.FormulasList}: java.lang.NullPointerException
05-01 20:06:59.976: E/AndroidRuntime(4820):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-01 20:06:59.976: E/AndroidRuntime(4820):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-01 20:06:59.976: E/AndroidRuntime(4820):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-01 20:06:59.976: E/AndroidRuntime(4820):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-01 20:06:59.976: E/AndroidRuntime(4820):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-01 20:06:59.976: E/AndroidRuntime(4820):     at android.os.Looper.loop(Looper.java:137)
05-01 20:06:59.976: E/AndroidRuntime(4820):     at android.app.ActivityThread.main(ActivityThread.java:5041)
05-01 20:06:59.976: E/AndroidRuntime(4820):     at java.lang.reflect.Method.invokeNative(Native Method)
05-01 20:06:59.976: E/AndroidRuntime(4820):     at java.lang.reflect.Method.invoke(Method.java:511)
05-01 20:06:59.976: E/AndroidRuntime(4820):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-01 20:06:59.976: E/AndroidRuntime(4820):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-01 20:06:59.976: E/AndroidRuntime(4820):     at dalvik.system.NativeStart.main(Native Method)
05-01 20:06:59.976: E/AndroidRuntime(4820): Caused by: java.lang.NullPointerException
05-01 20:06:59.976: E/AndroidRuntime(4820):     at com.gabilheri.marcuscalculatorv2.FormulasList.onCreate(FormulasList.java:30)
05-01 20:06:59.976: E/AndroidRuntime(4820):     at android.app.Activity.performCreate(Activity.java:5104)
05-01 20:06:59.976: E/AndroidRuntime(4820):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-01 20:06:59.976: E/AndroidRuntime(4820):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-01 20:06:59.976: E/AndroidRuntime(4820):     ... 11 more

1 个答案:

答案 0 :(得分:0)

尝试移动 的setContentView(R.layout.activity_formulas); 到方法的开始。在超级电话之后。

您正在尝试在初始化活动布局

之前使用gui控件