03-05 11:31:07.042 1897-1897 / com.example.project.a D / AndroidRuntime:关闭VM ---------崩溃的开始 03-05 11:31:07.050 1897-1897 / com.example.project.a E / AndroidRuntime:FATAL
例外:主要 处理:com.example.project.a,PID:1897 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.project.a / com.example.project.a.MainActivity}:java.lang.NullPointerException:尝试调用虚方法' void android.widget .LinearLayout.removeAllViews()'在null对象引用上 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
public void onItemSelected(AdapterView arg0,View arg1,int arg2,long arg3){
// Toast.makeText(getApplicationContext(),"The day qqq"+arg0.getItemAtPosition(arg2).toString(),Toast.LENGTH_LONG).show();
String control2=arg0.getItemAtPosition(arg2).toString();
int noOfBulb= (int) spinner1.getSelectedItem();
final TextView tryText2=(TextView)findViewById(R.id.textView);
tryText2.setText("No"+noOfBulb);
mdynamiclayout = (LinearLayout) findViewById(R.id.dynamiclayout);
mdynamiclayout.removeAllViews();//need to remove the view before it create
for (int i = 0; i < noOfBulb; i++) {//6 is your selected item from spinner it can be any nymber
TextView t = new TextView(MainActivity.this);
t.setText("value" + i);
// t.setTextSize(20);
// t.setTextColor(Color.BLUE);
mdynamiclayout.addView(t);
}
}
public void onNothingSelected(AdapterView<?> arg0){}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SelectBulbs();
rl = (RelativeLayout) findViewById(R.id.rl);
tv1 = new TextView (MainActivity.this);
tv1.setText("Dynamic TextView");
tv1.setTextColor(Color.RED);
tv1.setTextSize(20);
RelativeLayout.LayoutParams params1=new RelativeLayout.LayoutParams
((int)LayoutParams.WRAP_CONTENT,(int)LayoutParams.WRAP_CONTENT);
params1.leftMargin=115;
params1.topMargin=120;
rl.addView(tv1);
public class Unit {
private String _bulbNo;
public Unit(String bulbNo) {
_bulbNo = bulbNo;
}
public String getBulbNo(){
return _bulbNo;
}
}
// I created this class
private void addBulbsFans(String bulb)
{
Units.add(new Unit(bulb));
}
//我将此方法添加到MainActivity.java。之后,我需要知道如何从buttonclick事件传递微调器值。
答案 0 :(得分:0)
您可以通过以下
简单地将所选字符串转换为整数 int value = Integer.valueOf(spiner.getselecteditem);
答案 1 :(得分:0)
您使用Integer.valueOf()
,在您的情况下就是这样:
String[] bulbs = {"0","1","2","3","4","5"};
Integer[] bulbsConvert = new Integer[6];
//Array to int
for(String item : bulbs){
bulbsConvert.add(Integer.valueOf(item));
}
答案 2 :(得分:0)
如果要以垂直顺序创建动态文本视图,请在xml中创建LinearLayout,并将方向设置为垂直,如下所示
<LinearLayout
android:id="@+id/dynamiclayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/registerlayout"
android:layout_below="@+id/registerlayout"
android:layout_marginLeft="16dp"
android:layout_marginTop="34dp"
android:orientation="vertical" >
</LinearLayout>
然后在您的活动中添加以下代码行
private LinearLayout mdynamiclayout;
mdynamiclayout = (LinearLayout) findViewById(R.id.dynamiclayout);
mdynamiclayout.removeAllViews();//need to remove the view before it create
for (int i = 0; i < 6; i++) {//6 is your selected item from spinner it can be any nymber
TextView t = new TextView(MainActivity.this);
t.setText("value" + i);
mdynamiclayout.addView(t);
}
将一项活动传递给另一项活动:
Intent intent_value = new Intent(Mainactivity.this,
yoursecondactivity.class);
intent_value.putExtra("key", value);
intent_value.putExtra("key2", value2);
startActivity(intent_value);
检索值:
Intent getValues = getIntent();
String value1 = getValues.getStringExtra("key");
如果您需要任何帮助,请告诉我