AndroidStudio3.3版本的一个问题

丶Chao 2019-03-15 09:37:46
2048游戏 :

gameview.java
public class GameView extends GridLayout {
public GameView(Context context) {
super(context);
initGameView();
}

public GameView(Context context, AttributeSet attrs) {
super(context, attrs);
initGameView();
}

public GameView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initGameView();
}
private void initGameView(){
setColumnCount(4);
setOnTouchListener(new OnTouchListener() {
private float downX,downY,offsetX,offsetY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
downX=event.getX();
downY=event.getY();
break;
case MotionEvent.ACTION_UP:
offsetX=event.getX()-downX;
offsetY=event.getY()-downY;
if(Math.abs(offsetX)>Math.abs(offsetY)){
if(offsetX<-5){
swipeLeft();
}else if (offsetX>5){
swipeRight();
}
}else{
if(offsetY<-5){
swipeUp();
}else if (offsetY>5){
swipeDown();
}
}
break;
default:
break;
}
return true;
}
});
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);

int cardWith=(Math.min(w,h)-10)/4;
addCards(cardWith,cardWith);
}
private void addCards(int cardWith,int cardHeight){
Card c;
for(int y=0;y<4;y++){
for(int x=0;x<4;x++){
c=new Card(getContext());
c.setNum(2);
addView(c,cardWith,cardHeight);
}
}
}
}


card.java
public class Card extends FrameLayout {
private int num=0;
private TextView lable;
public Card(@NonNull Context context) {
super(context);
lable = new TextView(getContext());
lable.setTextSize(30);
LayoutParams lp = new LayoutParams(-1, -1);
addView(lable, lp);
setNum(0);
}
public int getNum(){
return this.num;
}
public void setNum(int num){
this.num=num;
lable.setText(""+num);
}
public boolean equeals(Card o){
return getNum()==o.getNum();
}
}

在gameview.java中添加card 没有文本显示,只有gameview的框架

debug结果:

2019-03-15 09:28:07.223 1640-1651/? W/android.os.Debug: failed to get memory consumption info: -1
2019-03-15 09:28:07.482 1640-2100/? E/memtrack: Couldn't load memtrack module
...全文
339 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_44177415 2019-03-17
  • 打赏
  • 举报
回复
失败求救:读写安卓手机的随机文件

想读取安卓手机 HTC U11+ 中的随机文件(由电脑拷入),但总是失败,调试结果是文件不存在(通过手机将我的程序数据权限改为无限制访问数据权限,访问、修改、删除外储的权限都打开了)

主要通过 TextView edit1 来调试(菜鸟陋习)

读写权限加了如下语句:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

相关随机文件代码:
if(Environment.getExternalStorageState().equals(MEDIA_MOUNTED)) {
//SD卡可用
edit1.setText(Environment.getExternalStorageDirectory().getPath()); //用edit1是否显示路径来判断,实测结果是显示 /storage/emulated/0
File file = new File(Environment.getExternalStorageDirectory().getPath()+"/day/010011.day");
if (file.exists()) { edit1.setText("文件存在"));//实测结果是未显示 文件存在 }
RandomAccessFile raf = new RandomAccessFile(file, "rw");
edit1.setText(String.valueOf(raf.length())); //实测无任何显示
}

翻看网上很多资料,也找不出原因所在。即使修改代码,改为向一个新文件如 888.day 的随机文件写入数据,结果也是文件没有生成。

请教大神,问题出在哪里?
丶Chao 2019-03-15
  • 打赏
  • 举报
回复
引用 4 楼 usecf的回复:
把android.support.constraint.ConstraintLayout改成LinearLayout试试
布局改过了,线性布局
usecf 2019-03-15
  • 打赏
  • 举报
回复
把android.support.constraint.ConstraintLayout改成LinearLayout试试
SemiraChen 2019-03-15
  • 打赏
  • 举报
回复
引用 2 楼 盲侠z 的回复:
[quote=引用 1 楼 SemiraChen的回复:]额,试了下代码,四行四列的2显示出来了啊。。。不太懂了
能发一下activity_main和mainFest的代码吗?[/quote] <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".game.Game2048"> <com.example.csy.activitypractice.game.GameView android:layout_width="match_parent" android:layout_height="match_parent"/> </android.support.constraint.ConstraintLayout> mainFest我也没干啥,就是我之前写的乱七八糟的东西 话说你的问题也不是你发的logcat那几句把,问题不是这个把。。 跟as3.3又有什么关系呢 ,我用的就是as3.3。。。 是不是找问题的方向错了呢。。仔细思考下。。
丶Chao 2019-03-15
  • 打赏
  • 举报
回复
引用 1 楼 SemiraChen的回复:
额,试了下代码,四行四列的2显示出来了啊。。。不太懂了
能发一下activity_main和mainFest的代码吗?
SemiraChen 2019-03-15
  • 打赏
  • 举报
回复
额,试了下代码,四行四列的2显示出来了啊。。。不太懂了
丶Chao 2019-03-15
  • 打赏
  • 举报
回复
引用 6 楼 SemiraChen的回复:
假如可以的话,可以把项目分享出来。 人多集思广益就解决了,现在信息量有点小。。。
多谢提醒,以后会注意的。
丶Chao 2019-03-15
  • 打赏
  • 举报
回复
相同问题的一个解决办法。。。
https://blog.csdn.net/a591243801/article/details/67640114?tdsourcetag=s_pcqq_aiomsg
SemiraChen 2019-03-15
  • 打赏
  • 举报
回复
假如可以的话,可以把项目分享出来。 人多集思广益就解决了,现在信息量有点小。。。

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧