Java 如何从JSONArray以编程方式添加textview

Java 如何从JSONArray以编程方式添加textview,java,android,json,android-layout,Java,Android,Json,Android Layout,我已经从json文件创建了我的活动 现在我试着在我的新活动中添加一个文本视图,但它没有 这是我的json文件: "interface":{ "View":[ { "id":"0", "type":"Simple", "Label":[ { "text":"bonjour comment ca va ?", "position_x":"200", "position_y":"400" }, {

我已经从json文件创建了我的活动

现在我试着在我的新活动中添加一个文本视图,但它没有

这是我的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扩展了活动{

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);
    }
}
我用他的id从json获取我的视图。但是我不知道如何将我的文本视图放到我的视图中

这是我的密码:

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"));
        }
当我尝试实例化我的Textview时,我的日志会显示java.lang.NullPointerException 原因:com.fchps.bya.json.JSONParser.classicSetLabel(JSONParser.java:89)

第89行=文本视图myTextView=新文本视图(classicView)

那么,谁能向我解释我的错误呢?我试了一个星期,但没有成功,所以我来这里是想知道somoby是否能帮我:)

我无法使用:`为什么:

classicView.setContentView(myTextView);
不行?我已经在classicview课上做了线性布局了`

谢谢:)

您需要此处的活动上下文:

TextView myTextView = new TextView(YourActivityName.this);


如果要垂直和动态添加TextView,请尝试使用ListView。

因为在java类中创建视图需要活动上下文。请向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);

        }
}

方法
classicSetLabel
activity
的内部,还是单独的类?classicSetLabel在我的JSONParser类中,而不是在哪个activity内部。但是我的ClassicView herite来自activity
ClassicView
继承自activity不会对您有任何帮助。您需要正在运行的activity或application con的上下文text.创建一个新的活动对象并像那样传递它将不起作用。我如何才能获取ClassicView的上下文?我不理解TextView myTextView=new TextView(ClassicView.getApplicationContext());不起作用getApplicationContext()不起作用。当我尝试新建TextView(ClassicView.this)时。它不起作用。我想从json文件以编程方式创建对象。我有视图。现在我尝试将元素放入视图中。好的,但我不想定义您声明的上下文?@user2871328:无需定义上下文,只需将参数添加到这两个方法中,并使用
MainActivity从Activity传递它。这与我在回答中所做的相同@ρцσѕρєцK这将正常工作..下一行会发生什么?
classicView.addContentView(myTextView,params);
文本视图是否可见?@user2871328:要使文本视图可见,您需要将所有文本视图添加到当前活动布局是的,但我不知道我有多少个文本视图,因为该数字正在json文件中读取。它可以是4个文本视图,也可以是10个文本视图。这取决于应用程序用户。我知道如何设置1或2个文本视图程序Maticali,但不是X文本视图。。
TextView myTextView = new TextView(YourActivityName.this);
TextView myTextView = new TextView(getApplicationContext());
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);

        }
}
ClassicView view = jParser.getClassicViewWithId(your_id,MainActivity.this);