未处理的异常类型ParseException - Parse.com示例实现

时间:2013-04-17 13:37:00

标签: java android parsing rest

我正在尝试扩展我当前的源代码以包含额外的数据行(它只使用textview工作正常:空)但是当我尝试添加empty2以允许显示更多数据时 - 我正在运行进入错误:

“Empty2无法解析或不是字段”

即使我创建了一个名为empty2的文本视图

知道为什么会这样吗?

todo_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5sp"
    android:textSize="25sp" >
    <TextView
        android:id="@+id/android:empty2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/empty" />

<TextView
        android:id="@+id/android:empty3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/empty" />
<TextView
        android:id="@+id/android:empty4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/empty" />
<TextView
        android:id="@+id/android:empty5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/empty" />

</TextView>



package com.parse.demo;

import java.util.List;

import android.app.Dialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;

public class ToDoListActivity extends ListActivity {
    private static final int ACTIVITY_CREATE = 0;
    private static final int ACTIVITY_EDIT = 1;

    public static final int INSERT_ID = Menu.FIRST;
    private static final int DELETE_ID = Menu.FIRST + 1;

    private List<ParseObject> todos;
    private Dialog progressDialog;

    private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
        // Override this method to do custom remote calls
        protected Void doInBackground(Void... params) {
            // Gets the current list of todos in sorted order
            ParseQuery query = new ParseQuery("TestObject");
            query.orderByDescending("_created_at");

            try {
                todos = query.find();
            } catch (ParseException e) {

            }
            return null;
        }

        @Override
        protected void onPreExecute() {
            ToDoListActivity.this.progressDialog = ProgressDialog.show(ToDoListActivity.this, "",
                    "Loading...", true);
            super.onPreExecute();
        }

        @Override
        protected void onProgressUpdate(Void... values) {

            super.onProgressUpdate(values);
        }

        @Override
        protected void onPostExecute(Void result) {
            // Put the list of todos into the list view
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(ToDoListActivity.this,
                    R.layout.todo_row);
            for (ParseObject todo : todos) {
                adapter.add((String) todo.get("DataI"));
                adapter.add((String) todo.get("DataO"));
            }
            setListAdapter(adapter);
            ToDoListActivity.this.progressDialog.dismiss();
            TextView empty = (TextView) findViewById(android.R.id.empty);
            TextView empty2 = (TextView) findViewById(android.R.id.empty2);
            empty.setVisibility(View.VISIBLE);
            empty2.setVisibility(View.VISIBLE);
        }
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView empty = (TextView) findViewById(android.R.id.empty);
        empty.setVisibility(View.INVISIBLE);
        TextView empty2 = (TextView) findViewById(android.R.id.empty2);
        empty2.setVisibility(View.INVISIBLE);

        new RemoteDataTask().execute();
        registerForContextMenu(getListView());
    }

    private void createTodo() {
        Intent i = new Intent(this, CreateTodo.class);
        startActivityForResult(i, ACTIVITY_CREATE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (intent == null) {
            return;
        }
        final Bundle extras = intent.getExtras();

        switch (requestCode) {
        case ACTIVITY_CREATE:
            new RemoteDataTask() {
                protected Void doInBackground(Void... params) {
                    String DataI = extras.getString("DataI");
                    String DataO = extras.getString("DataO");
                    ParseObject todo = new ParseObject("Todo");
                    todo.put("DataI", DataI);
                    todo.put("DataO", DataO);
                    try {
                        todo.save();
                    } catch (ParseException e) {
                    }

                    super.doInBackground();
                    return null;
                }
            }.execute();
            break;
        case ACTIVITY_EDIT:
            // Edit the remote object
            final ParseObject todo;
            todo = todos.get(extras.getInt("position"));
            todo.put("DataI", extras.getString("DataI"));
            todo.put("DataO", extras.getString("DataO"));

            new RemoteDataTask() {
                protected Void doInBackground(Void... params) {
                    try {
                        todo.save();
                    } catch (ParseException e) {
                    }
                    super.doInBackground();
                    return null;
                }
            }.execute();
            break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        boolean result = super.onCreateOptionsMenu(menu);
        menu.add(0, INSERT_ID, 0, R.string.menu_insert);
        return result;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, DELETE_ID, 0, R.string.menu_delete);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case DELETE_ID:
            AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

            // Delete the remote object
            final ParseObject todo = todos.get(info.position);


            new RemoteDataTask() {
                protected Void doInBackground(Void... params) {
                    try {
                        todo.delete();
                    } catch (ParseException e) {
                    }
                    super.doInBackground();
                    return null;
                }
            }.execute();
            return true;
        }
        return super.onContextItemSelected(item);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case INSERT_ID:
            createTodo();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Intent i = new Intent(this, CreateTodo.class);

        i.putExtra("DataI", todos.get(position).getString("DataI").toString());
        i.putExtra("DataO", todos.get(position).getString("DataO").toString());
        i.putExtra("position", position);


        startActivityForResult(i, ACTIVITY_EDIT);
    }

}

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ListView
        android:id="@+id/android:list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/android:empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/empty" />
<TextView
        android:id="@+id/android:empty2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/empty" />

<TextView
        android:id="@+id/android:empty3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/empty" />
<TextView
        android:id="@+id/android:empty4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/empty" />
<TextView
        android:id="@+id/android:empty5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/empty" />
</LinearLayout>

答案后的消息来源:

import java.util.List;

import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;

import android.app.Dialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;


public class ToDoListActivity extends ListActivity {


    TextView empty;
    TextView empty2;
    private static final int ACTIVITY_CREATE = 0;
    private static final int ACTIVITY_EDIT = 1;

    public static final int INSERT_ID = Menu.FIRST;
    private static final int DELETE_ID = Menu.FIRST + 1;

    private List<ParseObject> todos;
    private Dialog progressDialog;

    private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
        // Override this method to do custom remote calls
        public void setVisibility() {
               empty.setVisibility(View.VISIBLE);
               empty2.setVisibility(View.VISIBLE);  

        }
        protected void doInBackground(Void... params) {
            // Gets the current list of todos in sorted order
            ParseQuery query = new ParseQuery("TestObject");
            query.orderByDescending("_created_at");

            try {
                todos = query.find();
            } catch (ParseException e) {

                return;

            }




            runOnUiThread(new Runnable() {
                   public void run() {


            //      
                   }});
        }

        @Override
        protected void onPreExecute() {
            ToDoListActivity.this.progressDialog = ProgressDialog.show(ToDoListActivity.this, "",
                    "Loading...", true);
            super.onPreExecute();
        }

        @Override
        protected void onProgressUpdate(Void... values) {

            super.onProgressUpdate(values);
        }

        @Override
        protected void onPostExecute(Void result) {
            // Put the list of todos into the list view
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(ToDoListActivity.this,
                    R.layout.todo_row);
            for (ParseObject todo : todos) {
                adapter.add((String) todo.get("DataI"));
                adapter.add((String) todo.get("DataO"));
                adapter.add((String) todo.get("DataRSSI"));
                adapter.add((String) todo.get("DataSSID"));
                adapter.add((String) todo.get("DataTIME"));
                adapter.add((String) todo.get("DataRESTRICTED"));
            }
            setListAdapter(adapter);
            ToDoListActivity.this.progressDialog.dismiss();
        //  TextView empty = (TextView) findViewById(android.R.id.empty);
    //      empty.setVisibility(View.VISIBLE);

        }
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_new);

            empty = (TextView) findViewById(android.R.id.empty);
            empty.setVisibility(View.INVISIBLE);
        //    empty2 = (TextView) findViewById(android.R.id.empty2);
         //   empty2.setVisibility(View.INVISIBLE);

        new RemoteDataTask().execute();
        registerForContextMenu(getListView());
    }

    private void createTodo() {
        Intent i = new Intent(this, CreateTodo.class);
        startActivityForResult(i, ACTIVITY_CREATE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (intent == null) {
            return;
        }
        final Bundle extras = intent.getExtras();

        switch (requestCode) {
        case ACTIVITY_CREATE:
            new RemoteDataTask() {
                protected void doInBackground(Void... params) {
                    String DataI = extras.getString("DataI");
                    String DataO = extras.getString("DataO");
                    String DataRSSI = extras.getString("DataRSSI");
                    String DataSSID = extras.getString("DataSSID");
                    String DataTIME = extras.getString("DataTIME");
                    String DataRESTRICTED = extras.getString("DataRESTRICTED");
                    ParseObject todo = new ParseObject("Todo");
                    todo.put("DataI", DataI);
                    todo.put("DataO", DataO);
                    todo.put("DataRSSI", DataRSSI);
                    todo.put("DataSSID", DataSSID);
                    todo.put("DataTIME", DataTIME);
                    todo.put("DataRESTRICTED", DataRESTRICTED);
                    try { todo.save(); } catch (ParseException e) {
                    }

                    super.doInBackground();
                    return;
                }
            }.execute();
            break;
        case ACTIVITY_EDIT:
            // Edit the remote object
            final ParseObject todo;
            todo = todos.get(extras.getInt("position"));
            todo.put("DataI", extras.getString("DataI"));
            todo.put("DataO", extras.getString("DataO"));
            todo.put("DataRSSI", extras.getString("DataRSSI"));
            todo.put("DataSSID", extras.getString("DataSSID"));
            todo.put("DataTIME", extras.getString("DataTIME"));
            todo.put("DataRESTRICTED", extras.getString("DataRESTRICTED"));

            new RemoteDataTask() {
                protected void doInBackground(Void... params) {
                    try {
                        todo.save();
                    } catch (ParseException e) {
                    }
                    super.doInBackground();
                    return;
                }
            }.execute();
            break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        boolean result = super.onCreateOptionsMenu(menu);
        menu.add(0, INSERT_ID, 0, R.string.menu_insert);
        return result;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, DELETE_ID, 0, R.string.menu_delete);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case DELETE_ID:
            AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

            // Delete the remote object
            final ParseObject todo = todos.get(info.position);


            new RemoteDataTask() {
                protected void doInBackground(Void... params) {
                    try {
                        todo.delete();
                    } catch (ParseException e) {
                    }
                    super.doInBackground();
                    return;
                }
            }.execute();
            return true;
        }
        return super.onContextItemSelected(item);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case INSERT_ID:
            createTodo();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Intent i = new Intent(this, CreateTodo.class);

        i.putExtra("DataI", todos.get(position).getString("DataI").toString());
        i.putExtra("DataO", todos.get(position).getString("DataO").toString());
        i.putExtra("DataRSSI", todos.get(position).getString("DataRSSI").toString());
        i.putExtra("DataSSID", todos.get(position).getString("DataSSID").toString());
        i.putExtra("DataTIME", todos.get(position).getString("DataTIME").toString());
        i.putExtra("DataRESTRICTED", todos.get(position).getString("DataRESTRICTED").toString());
        i.putExtra("position", position);


        startActivityForResult(i, ACTIVITY_EDIT);
    }

}

问题

Description Resource    Path    Location    Type
Void methods cannot return a value  ToDoListActivity.java   /NFC Linking Manager/src/com/nfc/linkingmanager line 56 Java Problem
The return type is incompatible with AsyncTask<Void,Void,Void>.doInBackground(Void[])   ToDoListActivity.java   /NFC Linking Manager/src/com/nfc/linkingmanager line 47 Java Problem

1 个答案:

答案 0 :(得分:1)

由于您要更新用户界面,因此需要在runOnUiThread(...)中使用AsyncTask方法。

runOnUiThread(new Runnable() {
   public void run() {
      setVisibility();
   }
});

您需要在setVisibility()活动中定义ToDoListActivity方法。 例如

setVisibility() {
   empty.setVisibility(View.VISIBLE);
   empty2.setVisibility(View.VISIBLE);
}

此外,最好在全局范围内声明UI对象,然后在onCreate(...)方法中实例化。 例如

public class ToDoListActivity extends ListActivity {
   ...
   TextView empty;
   TextView empty2;
   ...
   @Override
   public void onCreate(Bundle savedInstanceState) {
    ...
    empty = (TextView) findViewById(android.R.id.empty);
    empty.setVisibility(View.INVISIBLE);
    empty2 = (TextView) findViewById(android.R.id.empty2);
    empty2.setVisibility(View.INVISIBLE);
    ...
}

修改

您需要修改AsyncTask中的onPostExecute(...)方法,如下所示:

protected void onPostExecute(Void result) {
// Put the list of todos into the list view
ArrayAdapter<String> adapter = new ArrayAdapter<String>(ToDoListActivity.this,
        R.layout.todo_row);
for (ParseObject todo : todos) {
    adapter.add((String) todo.get("DataI"));
    adapter.add((String) todo.get("DataO"));
    adapter.add((String) todo.get("DataRSSI"));
    adapter.add((String) todo.get("DataSSID"));
    adapter.add((String) todo.get("DataTIME"));
    adapter.add((String) todo.get("DataRESTRICTED"));
}
setListAdapter(adapter);
ToDoListActivity.this.progressDialog.dismiss();
/* REMOVE BELOW LINES */
//TextView empty = (TextView) findViewById(android.R.id.empty);
//empty.setVisibility(View.VISIBLE);
//TextView empty2 = (TextView) findViewById(android.R.id.empty2);
//empty2.setVisibility(View.VISIBLE);

}

以及runOnUiThread(...)方法

runOnUiThread(new Runnable() {
   public void run() {
  setVisibility();
  /* REMOVE BELOW LINES */
  //empty.setVisibility(View.VISIBLE);
  //empty2.setVisibility(View.VISIBLE);
}
});

我在setVisibility()课程中没有看到ToDoListActivity方法。

<强>更新

protected Void doInBackground(Void... params) {
        // Gets the current list of todos in sorted order
        ParseQuery query = new ParseQuery("TestObject");
        query.orderByDescending("_created_at");

        try {
            todos = query.find();
        } catch (ParseException e) {

        }

        runOnUiThread(new Runnable() {
               public void run() {
                  setVisibility();
               }});

        return null; // RETURN STATEMENT HERE
    }