1

这是我的问题,我正在尝试从 ListView 中设置文本。问题是,我会收到一个错误,因为(根据我的日志)我的主要活动的 setContentView 与我想从另一个 .xml (contactList.xml)设置文本的 TextView 不匹配

工作活动(主要)

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.jobs);
    vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    TextView assigned = (TextView) findViewById(R.id.assigned);



    new ParseJobs().execute();

ParseJobs() 方法

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);



        if (ACKNOWLEDGED.contains("False")) {


            /** Updating parsed JSON data into ListView */
            ListAdapter adapter = new SimpleAdapter(Jobs.this, contactList, R.layout.list_item, new String[] { job, pays, ready_at }, new int[] { R.id.job1, R.id.pays1, R.id.ready_at1 });
            setListAdapter(adapter);        

            //TextView readyORdue = (TextView) findViewById(R.id.readyORdue);
            //readyORdue.setText("Ready At:");
        }

        if (ACKNOWLEDGED.contains("True")) {

            /** Updating parsed JSON data into ListView */
            ListAdapter adapter = new SimpleAdapter(Jobs.this, contactList, R.layout.list_item, new String[] { job, pays, due_by }, new int[] { R.id.job1, R.id.pays1, R.id.due_by1 });
            setListAdapter(adapter);    

            //TextView readyORdue = (TextView) findViewById(R.id.readyORdue);
            //readyORdue.setText("Due By:");
        }


        VerifyDriver.dismiss();

    }
}

请注意,在我的 ParseJobs 方法中,我正在尝试更新 TextView “readyORdue”,但我会收到错误消息,因为 TextView 属于 SimpleAdapter contactList.xml

所以我的问题是,我将如何从我的contactList XML 布局中设置文本视图?

这是我的日志:

 07-17 15:55:16.703: W/dalvikvm(4387): threadid=1: thread exiting with uncaught exception (group=0x40015578)
 07-17 15:55:16.703: E/AndroidRuntime(4387): FATAL EXCEPTION: main
 07-17 15:55:16.703: E/AndroidRuntime(4387): java.lang.NullPointerException
 07-17 15:55:16.703: E/AndroidRuntime(4387):    at com.jetdelivery.mobile.Jobs$ParseJobs.onPostExecute(Jobs.java:287)
 07-17 15:55:16.703: E/AndroidRuntime(4387):    at com.jetdelivery.mobile.Jobs$ParseJobs.onPostExecute(Jobs.java:1)
 07-17 15:55:16.703: E/AndroidRuntime(4387):    at android.os.AsyncTask.finish(AsyncTask.java:417)
 07-17 15:55:16.703: E/AndroidRuntime(4387):    at android.os.AsyncTask.access$300(AsyncTask.java:127)
 07-17 15:55:16.703: E/AndroidRuntime(4387):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
 07-17 15:55:16.703: E/AndroidRuntime(4387):    at android.os.Handler.dispatchMessage(Handler.java:99)
 07-17 15:55:16.703: E/AndroidRuntime(4387):    at android.os.Looper.loop(Looper.java:130)
 07-17 15:55:16.703: E/AndroidRuntime(4387):    at android.app.ActivityThread.main(ActivityThread.java:3687)
 07-17 15:55:16.703: E/AndroidRuntime(4387):    at java.lang.reflect.Method.invokeNative(Native Method)
 07-17 15:55:16.703: E/AndroidRuntime(4387):    at java.lang.reflect.Method.invoke(Method.java:507)
07-17 15:55:16.703: E/AndroidRuntime(4387):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
07-17 15:55:16.703: E/AndroidRuntime(4387):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
07-17 15:55:16.703: E/AndroidRuntime(4387):     at dalvik.system.NativeStart.main(Native Method)

第 287 行是“readyORdue.setText("Ready At:");"

4

3 回答 3

0

setListAdapter 与 ListActivity 相关,而 ListActivity 有她自己的布局内容,所以......你不能在 ListActivity 中设置内容视图 你可以做的是创建自己的布局,其中包含列表和 TextView readyORdue & TextView 分配,只有一件事为什么你使用两个 if ?你可以设置 if(firstCondition) { .. }else ...

于 2012-07-17T23:09:57.903 回答
0

这些是同一个班吗?如果是这样,我会readyORdue在你的MainActivity. 如果可以避免的话,你的线程不应该定义用户界面对象;这就是为什么你有onCreate()

public class MainActivity {
    private TextView readyORDue;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.jobs);
        vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        TextView assigned = (TextView) findViewById(R.id.assigned);

        readyORDue = (TextView) findViewById(R.id.readyORdue);

        new ParseJobs().execute();
    }
}

然后在你的解析器中

protected void onPostExecute(Void result) {
    super.onPostExecute(result);
    if (ACKNOWLEDGED.contains("False")) {


        /** Updating parsed JSON data into ListView */
        ListAdapter adapter = new SimpleAdapter(Jobs.this, contactList, R.layout.list_item, new String[] { job, pays, ready_at }, new int[] { R.id.job1, R.id.pays1, R.id.ready_at1 });
        setListAdapter(adapter);        

        readyORdue.setText("Ready At:");
    }

    if (ACKNOWLEDGED.contains("True")) {

        /** Updating parsed JSON data into ListView */
        ListAdapter adapter = new SimpleAdapter(Jobs.this, contactList, R.layout.list_item, new String[] { job, pays, due_by }, new int[] { R.id.job1, R.id.pays1, R.id.due_by1 });
        setListAdapter(adapter);    

        readyORdue.setText("Due By:");
    }


    VerifyDriver.dismiss();

}

注意:您可能必须使用runOnUiThreadsetText() 调用。

于 2012-07-17T23:12:57.057 回答
0

如果您想按照您的描述使用另一个布局文件“contactList.xml”,首先您需要对其进行膨胀。

LayoutInflater inflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE); 
View contactList= inflater.inflate(R.layout.contactList, false);

现在

TextView readyORdue = (TextView)contactList.findViewById(R.id.readyORdue);

will give you needed TextView, add this view to some container in your R.layout.jobs to make it visible.

于 2012-07-17T23:14:21.930 回答