0

我发布了一个类似的版本,但它在不同的主题下,所以我重新发布(我希望没关系,如果不是,请告诉我)。

无论如何,我不能让它为我的生活工作。我知道这可能很明显,但我就是不明白。我已经为此奋斗了好几个小时。希望有人可以提供帮助!

我正在使用 Log.d 显示存储在 Message 中的内容,它显示为 null。当我将它放入 messageQueue 时,它​​具有我期望的数据。当我从我的 handleMessage 方法调用 gainMessage 时,我得到 null。我不知道为什么。我将永远感激任何能拯救我的人!谢谢!!!

这是我的代码:

package jschuler.cs211d.hw07;

import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
import android.os.Handler;
import android.content.Intent;
import android.os.Message;
import android.util.Log;
import android.app.Service;
import android.os.IBinder;

public class HW07 extends Activity implements View.OnKeyListener
{
    String howMany, upper, lower, values;
    TextView tv_howMany, tvUpper, tvLower, test;
    EditText howManyInput, upperInput, lowerInput;

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

        tv_howMany = (TextView) findViewById(R.id.how_many);
        tv_howMany.setText("How many primes would you like to generate?");

        howManyInput = (EditText) findViewById(R.id.input1);
        howManyInput.setOnKeyListener(this);        

        tvUpper = (TextView) findViewById(R.id.upper);
        tvUpper.setText("Upper limit:");

        upperInput = (EditText) findViewById(R.id.input2);
        upperInput.setOnKeyListener(this);        

        tvLower = (TextView) findViewById(R.id.lower);
        tvLower.setText("Lower limit:");

        lowerInput = (EditText) findViewById(R.id.input3);
        lowerInput.setOnKeyListener(this);        

        test = (TextView) findViewById(R.id.test);
    }

    Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            Log.d("handleMessage","message handled!");
            Toast.makeText(getApplicationContext(), "in handleMessage", Toast.LENGTH_LONG).show();
            String primes = msg.obj.toString();
            Log.d("handleMessage!","Here are the primes: " + primes);
            super.handleMessage(msg);

        }
    };

    public boolean onKey(View v, int keyCode, KeyEvent ke)
    {
        if( (ke.getAction() == KeyEvent.ACTION_DOWN) &&
            (keyCode == KeyEvent.KEYCODE_ENTER) )
        {
            values = howManyInput.getText().toString() + " " +
                upperInput.getText().toString() + " " +
                lowerInput.getText().toString();
            test.setText(values);

            Intent intent = new Intent(this, PrimeService.class);
            intent.putExtra("parameters",values);
            startService(intent);

            return true;
        }

        return false;
    }    
}

package jschuler.cs211d.hw07;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
import java.net.*;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import java.util.StringTokenizer;

public class PrimeService extends Service
{
    String str;

    int lower = 1, upper = 100;
    int numPrimes = 5;

    @Override
    public IBinder onBind(Intent args)
    {
        return null;
    }

    @Override
    public int onStartCommand(Intent i, int flags, int startId)
    {
        Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();

        String parameters = i.getExtras().getString("parameters");
        Log.d("onStartCommand","parameters: " + parameters);

        parseParameters(parameters);

        Thread t = new Thread( new Runnable() 
        {
            public void run() 
            {
                String result = generatePrimes();
                Log.d("result","result: "+result);
                Message primeMessage = receiveHandler.obtainMessage(0, result);
                receiveHandler.sendMessage(primeMessage);
                Log.d("primeMessage","primeMessage.toString(): "+ primeMessage.toString()); 
            }

        });
        t.start();

        return START_STICKY;
    }        

    Handler receiveHandler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            //SOMETHING IS WRONG HERE
            Message primeMsg = receiveHandler.obtainMessage();
            Log.d("primeMsg","primeMsg.toString(): "+ primeMsg.toString()); 

            String primes = (String)primeMsg.obj;

            Log.d("handleMessage","here are the primes: "+primes); 
        }
    };


    public void parseParameters(String p)
    {
        int i = 1;
        StringTokenizer st = new StringTokenizer(p);
        while (st.hasMoreTokens()) 
        {
            switch(i)
            {
                case 1: 
                    numPrimes = Integer.parseInt(st.nextToken());
                    break;
                case 2: 
                    upper = Integer.parseInt(st.nextToken());
                    break;
                case 3: 
                    lower = Integer.parseInt(st.nextToken());
                    break;
            }

            i++;
        }    %0
4

1 回答 1

2

我想到了。很明显!!

希望这会对其他人有所帮助。

这就是 receiveHandler 的样子:

Handler receiveHandler = new Handler()
{
    @Override
    public void handleMessage(Message msg)
    {                        
        String primes = (String)msg.obj;

        Log.d("handleMessage","here are the primes: "+primes); 
    }
};
于 2012-05-24T23:42:12.623 回答