【问题标题】:Passing values from android activity to custom view将值从 android 活动传递到自定义视图
【发布时间】:2014-04-16 05:55:10
【问题描述】:

我正在尝试将值从活动传递到自定义视图,然后我可以用它来绘制。认为这应该很容易做到,但是在运行时它不断抛出 NullPointerException 错误。 有人可以提供的任何帮助将不胜感激。提前致谢。

活动代码:

public class PlayGame_Activity extends ActionBarActivity {
private Game game;
private GameView gameView;
private int cardNumber;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_play_game_);

    game = new Game();
    cardNumber = 1;

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();

    }

    gameView = (GameView) findViewById(R.id.gameView1);
}


@Override
protected void onResume() {
    int[] testarray = {1,3,5,7,9}; //Real array values will come from game
    gameView.setCardNumbers(testarray);
    super.onResume();
}

public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_play_game_,
                container, false);
        return rootView;
    }
}

查看代码:

public class GameView extends View {
    private Paint paint;
    private int[] cardNumbers;

    public GameView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
    }

    public void setCardNumbers(int[] array) {
        cardNumbers = array;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

}

布局代码

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.numberguessgame.PlayGame_Activity"
    tools:ignore="MergeRootFrame" />

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.numberguessgame.PlayGame_Activity$PlaceholderFragment" >

    <com.example.numberguessgame.GameView
        android:id="@+id/gameView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>

所以 findviewbyid(gameview1) 似乎返回 null。关于我应该将那行代码放在哪里的任何想法?

【问题讨论】:

  • 您在activity_play_game_.xml 中有一个GameView@+id/gameView1 的XML 元素?
  • 发帖activity_play_game_.xml

标签: android view android-activity


【解决方案1】:

原来是移动

 gameView = (GameView) findViewById(R.id.gameView1);

使用 onResume() 而不是 onCreate() 解决了这个问题。

【讨论】:

    【解决方案2】:

    您可以使用EventBus framework 将对象从一个活动发送到另一个活动(或类的其他实例)。它对我帮助很大,甚至允许对象在独立于发布线程的线程中传递。只需遵循这四个步骤

    • 实现处理方式:public void onEvent(MyEventType event)

    • 注册订阅者:EventBus.getDefault().register(this)

    • 发布活动:EventBus.getDefault().post(event)

    • 注销:EventBus.getDefault().unregister(this)

    【讨论】:

      【解决方案3】:

      我们可以访问片段中的自定义视图变量,因此 v 可以将值从片段传递到自定义视图.. 示例:如果自定义视图“Drawview”包含值“Value”,v可以在片段中设置Value的值如下:(假设Drawview的id为drawview)

      Drawview drawview = (Drawview)view.findValueById(R.id.drawview); drawview.Value=值;

      这对我有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-12
        • 2019-10-27
        • 1970-01-01
        • 1970-01-01
        • 2017-03-17
        • 1970-01-01
        相关资源
        最近更新 更多