[英]How to add textview programmatically from JSONArray
我已经从我的json文件创建了Activity。
现在,我尝试将TextView放入新的Activity中,但事实并非如此。
这是我的json文件:
"interface":{
"View":[
{
"id":"0",
"type":"Simple",
"Label":[
{
"text":"bonjour comment ca va ?",
"position_x":"200",
"position_y":"400"
},
{
"text":"coucou les amis",
"position_x":"200",
"position_y":"200"
}
],
等等
我的活动效果很好。
公共类ClassicView扩展Activity {
public int myid;
public ClassicView() {
}
public ClassicView(int id){
super();
myid = id;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.linearlayout);
LinearLayout ll = (LinearLayout) findViewById(R.id.linearlayout2);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( new ViewGroup.MarginLayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
setContentView(ll);
}
}
我从json获取带有他的id的View。 但是我不知道如何在视图中放入textview。
这是我的代码:
public ClassicView getClassicViewWithId(int id) throws JSONException {
JSONArray myView = myjsonobj.getJSONObject("interface").getJSONArray("View");
for (int i = 0; i < myView.length(); i++) {
if (myView.getJSONObject(i).getInt("id") == id) {
Log.e("IDVIEW", myView.getJSONObject(i).getString("id"));
ClassicView myClassicView = new ClassicView(myView.getJSONObject(i).getInt("id"));
classicSetLabel(myView.getJSONObject(i).getJSONArray("Label"), myClassicView);
return myClassicView;
}
}
return null;
}
public void classicSetLabel(JSONArray myArrayLabel, ClassicView classicView) throws JSONException {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.BOTTOM;
Log.e("@@@@@WARNING TEXTLABEL :", "classicSetLabelErreur1");
Log.e("@@@@@WARNING TEXTLABEL :", "classicSetLabelErreur2");
for (int i = 0; i < myArrayLabel.length(); i++) {
Log.e("CLASSICVIEW", classicView.toString());
TextView myTextView = new TextView(classicView);
myTextView.append(myArrayLabel.getJSONObject(i).getString("text"));
classicView.addContentView(myTextView, params);
Log.e("@@@@@WARNING TEXTLABEL :", myArrayLabel.getJSONObject(i).getString("text"));
}
}
我的MainActivity.java:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
JSONParser jParser = null;
try {
jParser = new JSONParser("JsonTest.txt");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
ClassicView view = jParser.getClassicViewWithId(0);
//Log.e("idVIEWMAIN :", msg)
Intent intent = new Intent(MainActivity.this, view.getClass());
startActivity(intent);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
当我尝试实例化Textview时,我的LogCat说java.lang.NullPointerException由引起:com.fchps.bya.json.JSONParser.classicSetLabel(JSONParser.java:89)
第89行= TextView myTextView =新的TextView(classicView);
那么谁能向我解释我的错误是什么。 我尝试了1周,但没有成功,所以我来这里了解somoby是否可以帮助我:)
我不能通过以下方式将TextView添加到视图中:为什么:
classicView.setContentView(myTextView);
不行吗? 我的classicview类中已经有LinearLayout。
谢谢 :)
TextView myTextView = new TextView(classicView);
您在这里需要活动上下文:
TextView myTextView = new TextView(YourActivityName.this);
要么
TextView myTextView = new TextView(getApplicationContext());
如果要垂直和动态添加TextView,请尝试使用ListView。
因为要在java类中创建View,您将需要Activity Context。 向getClassicViewWithId方法添加一个上下文参数,如下所示:
public ClassicView getClassicViewWithId(int id,Context context)
throws JSONException {
classicSetLabel(myView.getJSONObject(i).getJSONArray("Label"),
myClassicView,context);
return null;
}
public void classicSetLabel(JSONArray myArrayLabel, ClassicView classicView,
Context context) throws JSONException {
for (int i = 0; i < myArrayLabel.length(); i++) {
Log.e("CLASSICVIEW", classicView.toString());
TextView myTextView = new TextView(context);
}
}
现在将具有ID的Activity从Activity上下文传递给getClassicViewWithId
方法:
ClassicView view = jParser.getClassicViewWithId(your_id,MainActivity.this);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.