【问题标题】:How Do I Pass The Package To RemoteView in Android?如何在 Android 中将包传递给 RemoteView?
【发布时间】:2010-11-05 23:16:11
【问题描述】:

Me Doing "edu.sju.BlackJack" 不会导致稍后调用的更新发生。 我正确引用了布局,并且应该更新它的调用是正确的,那么我应该为包名称输入什么?

我应该根据清单添加我的包名称是上面的。 这是我现在拥有的代码,它当前不更新屏幕(或者我猜是正确更改值)。

RemoteViews name = new RemoteViews("edu.sju.BlackJack", R.layout.play_screen);

如果不是这样..会是这个代码吗?

name.setTextViewText(R.id.Dealer_Total, "0");

Dealer_Total 是我要更改的 TextView 的 id。但是再次没有发生更改。

提前感谢您提供的所有帮助。

这是我正在谈论的全部代码,首先是 Playscreen.java

package edu.sju.BlackJack;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.widget.ImageView;
import android.widget.RemoteViews;
import android.widget.TextView;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import java.util.*;


public class PlayScreen extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    GameEngine Engine = new GameEngine();
     @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.play_screen);
        TextView TextDealer =  (TextView)findViewById(R.id.Dealer_Total); 
        Engine.setView(TextDealer);
        //Set up click listeners for all the buttons
        View hitButton = findViewById(R.id.hit_button);
        hitButton.setOnClickListener(this);
        View standButton = findViewById(R.id.stand_button);
        standButton.setOnClickListener(this);
        //new preplay button (ML 10/24/10)
        View prePlayButton = findViewById(R.id.prePlay_button);
        prePlayButton.setOnClickListener(this);
        Thread thread = new Thread(Engine);
        thread.start();


    }   

    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.prePlay_button:
                v.setVisibility(View.GONE);
                System.out.println("Working?");
                Engine.setGameStart(1);
                break;
            case R.id.hit_button:
                Engine.gameHit(1);
                break;
            case R.id.stand_button:
                Engine.gameStand(1);
                break;
        }

            // More buttons go here (if any) ...    
        }
}

现在是 GameEngine 线程 不是全部,只是让你明白了


package edu.sju.BlackJack;

import java.util.Random;

import android.widget.RemoteViews;
import android.widget.TextView;

public class GameEngine   implements Runnable {
    static int playerCount = 0;    //keep record of which cards to change for player when hit is selected
    static int dealerCount = 0;    //keep record of which cards to change for dealer when dealer hits
    static int win = 0;     //keeps record of wins  (JV 10/01/10)
    static int lose = 0;    //keeps record of loss  (JV 10/01/10)
    static int hit = 0;     //let's engine know if hit button was selected (0 means it has not)
    static int stand = 0;    //let's engine know if stand button was selected (0 means it has not)
    static int playerTotal = 0; //tells player's total (JV 10/01/10)
    static int dealerTotal = 0; //tells dealer's total (JV 10/01/10)
    static int playerTurn = 0;  //activates buttons so that they do actions when clicked (as it's players turn)
    static int startGame = 0; //starts the game when the start game button is pressed
    TextView TextDealer;
    RemoteViews name = new RemoteViews("edu.sju.BlackJack", R.layout.play_screen);


public void run() {
    name.setTextViewText(R.id.Dealer_Total, "0");
    //main();
}

public void setView(TextView a)
{
TextDealer = a;
}

public void setGameStart(int i)
{
    startGame = i;
}

public void gameHit(int i)
{
    if(playerTurn == 1)
    hit = 1;
}

public void gameStand(int i)
{
    if(playerTurn == 1)
    stand = 1;
}


public void main()

    {//Start Game
        Deck mainDeck = new Deck();
        fillDeck(mainDeck);

        //TextView TextPlayer =  (TextView)findViewById(R.id.Player_Total); 
        //TextDealer.setText("" + dealerTotal);
        //TextPlayer.setText("" + playerTotal);

        while(true)
        {
            if(startGame == 1)
            {

                if(mainDeck.getList().size() < 15){
                    mainDeck = emptyDeck();
                    fillDeck(mainDeck);
                    }  

                //RESET CARD VIEWS TO DEFAULT
                //RESET DEALERCARD AND PLAYERCARD TOTALS TO 0
                dealerTotal = 0;
                playerTotal = 0;
                playerCount = 0;
                dealerCount = 0;

                //playHand(mainDeck);

            }
        }

    }

【问题讨论】:

  • 如果您不知道,请投票,我真的需要尽快回答。再次提前感谢

标签: android views package


【解决方案1】:

无论你的问题是什么,我都不认为它是你认为的那样。如果您的布局出现在应用小部件中,则说明包名称处理正确。如果更新(您的setTextViewText() 调用)没有效果,那么R.layout.play_screen 没有R.id.Dealer_Total 或者您没有发送包含setTextViewText() 指令的RemoteViews

【讨论】:

  • 等等,小部件必须使用远程视图吗? (我的 playscreen.java 类只包含布局,但它本身并不是一个“小部件”)。这也是显示屏幕并调用线程 GameEngine 的内容,需要更改这些内容。
  • 我真的很讨厌自动输入... R.layout.play_screen 确实包含 R.id.Dealer_Total (Eclipse 访问我的所有视图并在我放置 setTextViewText() 时将其作为选项提供给我所以它似乎可以正常访问布局)...我只有 RemoteViews 名称,这也是我“发送”的唯一东西,所以我也不相信它。
  • @John V:抱歉,到目前为止,我只在应用小部件的上下文中使用过 RemoteViews。
  • 除了让它直接接收视图并捕获抛出的异常并忽略它之外,还有其他方法可以从游戏引擎编辑我的视图吗? ERROR/AndroidRuntime(653): android.view .ViewRoot$CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能接触其视图。
  • @John V:使用AsyncTask,或Handler,或runOnUiThread(),或post()RemoteViews 不适用于普通应用程序。
猜你喜欢
  • 2018-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-26
相关资源
最近更新 更多