zoukankan      html  css  js  c++  java
  • Android开发今日报错以及解决办法

    问题①

    我在另外一个项目里写了一个注册和登录功能的几个文件,当我想把这些代码和另一个文件合起来的时候就出现了问题。

    首先不可以直接把另外一个项目的某个文件直接复制过来,

    最好的办法是重新新建一个相同名字的文件,然后去另一个文件里复制代码(除了包名不要复制,其他都可以)。

    然后所有的activity文件都要在AndroidManifest文件里面注册

     1 <application
     2         android:allowBackup="true"
     3         android:icon="@mipmap/ic_launcher"
     4         android:label="@string/app_name"
     5         android:roundIcon="@mipmap/ic_launcher_round"
     6         android:supportsRtl="true"
     7         android:theme="@style/Theme.HelloWorld">
     8         <meta-data
     9             android:name="com.baidu.lbsapi.API_KEY"
    10             android:value="SmUC55wktbFHpYaLz7DLNeOdy4ScE5qH">
    11         </meta-data>
    12         <service
    13             android:name="com.baidu.location.f"
    14             android:enabled="true"
    15             android:process=":remote">
    16         </service>
    17 
    18         <activity android:name=".MainActivity">
    19         </activity>
    20         <activity android:name=".SecondActivity"></activity>

    21    <activity android:name=".loginActivity"> 22 <intent-filter> 23 <action android:name="android.intent.action.MAIN" /> 24 25 <category android:name="android.intent.category.LAUNCHER" /> 26 </intent-filter> 27 </activity>
    <!--
      这一部分代码 <intent-fillter> 标签里面的部分就是说当这个软件开始运行的时候,哪一个activity是最先被运行的,
      这一段在哪个activity里面被注册就是哪个最先运行。
    -->
    28 <activity android:name=".RegisterActivity"></activity> 29 </application>

    问题② 数据库帮助类的使用

      1 package com.example.helloworld;
      2 
      3 import android.content.Context;
      4 import android.database.Cursor;
      5 import android.database.sqlite.SQLiteDatabase;
      6 import android.database.sqlite.SQLiteOpenHelper;
      7 import android.util.Log;
      8 
      9 import java.util.ArrayList;
     10 
     11 public class DBOpenHelper extends SQLiteOpenHelper {
     12     /**
     13      * 声明一个AndroidSDK自带的数据库变量db
     14      */
     15     private SQLiteDatabase db;
     16 
     17     /**
     18      * 写一个这个类的构造函数,参数为上下文context,所谓上下文就是这个类所在包的路径
     19      * 指明上下文,数据库名,工厂默认空值,版本号默认从1开始
     20      * super(context,"db_test",null,1);
     21      * 把数据库设置成可写入状态,除非内存已满,那时候会自动设置为只读模式
     22      * 不过,以现如今的内存容量,估计一辈子也见不到几次内存占满的状态
     23      * db = getReadableDatabase();
     24      */
     25     public DBOpenHelper(Context context){
     26         super(context,"db_test",null,1);
     27         db = getReadableDatabase();
     28     }
     29 
     30     /**
     31      * 重写两个必须要重写的方法,因为class DBOpenHelper extends SQLiteOpenHelper
     32      * 而这两个方法是 abstract 类 SQLiteOpenHelper 中声明的 abstract 方法
     33      * 所以必须在子类 DBOpenHelper 中重写 abstract 方法
     34      * 想想也是,为啥规定这么死必须重写?
     35      * 因为,一个数据库表,首先是要被创建的,然后免不了是要进行增删改操作的
     36      * 所以就有onCreate()、onUpgrade()两个方法
     37      * @param db
     38      */
     39     @Override
     40     public void onCreate(SQLiteDatabase db){
     41         db.execSQL("CREATE TABLE IF NOT EXISTS user(" +
     42                 "_id INTEGER PRIMARY KEY AUTOINCREMENT," +
     43                 "username TEXT," +
     44                 "userid TEXT,"+
     45                 "userphone TEXT,"+
     46                 "useraddress TEXT)"
     47         );
     48     }
     49     @Override
     50     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
     51         db.execSQL("DROP TABLE IF EXISTS user");
     52         onCreate(db);
     53     }
     54     /**
     55      * 接下来写自定义的增删改查方法
     56      * 这些方法,写在这里归写在这里,以后不一定都用
     57      * add()
     58      * delete()
     59      * update()
     60      * getAllData()
     61      */
     62     public void add(String username,String userid,String userphone,String useraddress){
     63         db.execSQL("INSERT INTO user (username,userid,userphone,useraddress) VALUES(?,?,?,?)",new Object[]{username,userid,userphone,useraddress});
     64     }
     65     public void delete(String userphone){
     66         db.execSQL("DELETE FROM user WHERE userphone = "+userphone);
     67     }
     68     public void updata(String userphone){
     69         db.execSQL("UPDATE user SET userphone = ?",new Object[]{userphone});
     70     }
     71 
     72     /**
     73      * 前三个没啥说的,都是一套的看懂一个其他的都能懂了
     74      * 下面重点说一下查询表user全部内容的方法
     75      * 我们查询出来的内容,需要有个容器存放,以供使用,
     76      * 所以定义了一个ArrayList类的list
     77      * 有了容器,接下来就该从表中查询数据了,
     78      * 这里使用游标Cursor,这就是数据库的功底了,
     79      * 在Android中我就不细说了,因为我数据库功底也不是很厚,
     80      * 但我知道,如果需要用Cursor的话,第一个参数:"表名",中间5个:null,
     81      *                                                     最后是查询出来内容的排序方式:"name DESC"
     82      * 游标定义好了,接下来写一个while循环,让游标从表头游到表尾
     83      * 在游的过程中把游出来的数据存放到list容器中
     84      * @return
     85      */
     86     public ArrayList<User> getAllData(){
     87 
     88         ArrayList<User> list = new ArrayList<User>();
     89         Cursor cursor = db.query("user",null,null,null,null,null,"username DESC");
     90         while(cursor.moveToNext()){
     91             String username = cursor.getString(cursor.getColumnIndex("username"));
     92             String userid = cursor.getString(cursor.getColumnIndex("userid"));
     93             String userphone = cursor.getString(cursor.getColumnIndex("userphone"));
     94             String useraddress = cursor.getString(cursor.getColumnIndex("useraddress"));
     95             list.add(new User(username,userid,userphone,useraddress));
     96         }
     97         Log.v("输出数据库查询结果:",list.toString());
     98         return list;
     99     }
    100 }

    添加public void add(String username,String userid,String userphone,String useraddress){

        db.execSQL("INSERT INTO user (username,userid,userphone,useraddress) VALUES(?,?,?,?)"
      ,new Object[]{username,userid,userphone,useraddress});

    }
    查询方法
    public ArrayList<User> getAllData()
    ArrayList<User> list = new ArrayList<User>(); Cursor cursor = db.query("user",null,null,null,null,null,"username DESC" while(cursor.moveToNext()){ String username = cursor.getString(cursor.getColumnIndex("username"));
            String userid = cursor.getString(cursor.getColumnIndex("userid"));
    String userphone = cursor.getString(cursor.getColumnIndex("userphone"));
    String useraddress = cursor.getString(cursor.getColumnIndex("useraddress"));
    list.add(new User(username,userid,userphone,useraddress));
    }
    Log.v("输出数据库查询结果:",list.toString());
    return list;
    }
    1、close()  关闭游标,释放资源
    2、getColumnCount()返回所有列的总数
    3、getColumnIndexOrThrow(String columnName)
      从零开始返回指定列名称,如果不存在将抛出IllegalArgumentException 异常。
    4、moveToNext()    
      移动光标到下一行
    5、moveToFirst()
      移动光标到第一行
    6、moveToPosition(int position)
    移动光标到一个绝对的位置
    7、moveToPrevious() 移动光标到上一行

    数据库构造函数
    public DBOpenHelper(Context context){
    super(context,"db_test",null,1);
    db = getReadableDatabase();
    }

    onCreate()方法

    public void onCreate(SQLiteDatabase db){
    db.execSQL("CREATE TABLE IF NOT EXISTS user(" +
    "_id INTEGER PRIMARY KEY AUTOINCREMENT," +
    "username TEXT," +
    "userid TEXT,"+
    "userphone TEXT,"+
    "useraddress TEXT)"
    );
    }
    注意不要丢掉_id 主键
    每一段定义后面都有一个英文的逗号、

    ③实现页面跳转

    Intent intent2 = new Intent(this, MainActivity.class);
    startActivity(intent2);
    自动获取时间

    public void autoTimeAndDate(View view)
    {
    text2=(EditText)findViewById(R.id.tv_text2);
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// HH:mm:ss
    //获取当前时间
    Date date = new Date(System.currentTimeMillis());
    text2.setText(simpleDateFormat.format(date));
    }

    //获取当前地址
    
    
     1    public LocationClient mLocationClient = null;
     2     private MyLocationListener myListener = new MyLocationListener();
     3     //获取地址
     4     public class MyLocationListener extends BDAbstractLocationListener {
     5         @Override
     6         public void onReceiveLocation(BDLocation location){
     7             //此处的BDLocation为定位结果信息类,通过它的各种get方法可获取定位相关的全部结果
     8             //以下只列举部分获取地址相关的结果信息
     9             //更多结果信息获取说明,请参照类参考中BDLocation类中的说明
    10             String addr = location.getAddrStr();    //获取详细地址信息
    11             String country = location.getCountry();    //获取国家
    12             String province = location.getProvince();    //获取省份
    13             String city = location.getCity();    //获取城市
    14             String district = location.getDistrict();    //获取区县
    15             String street = location.getStreet();    //获取街道信息
    16             String town = location.getTown();
    17             //获取乡镇信息
    18             text4=(EditText)findViewById(R.id.tv_text4);
    19             text4.setText(country+" " +province+" "+city+" "+district+" "+town+" "+street+" ");
    20         }
    21     }
    22     //自动获取地址
    23     public void autoAddress(View view)
    24     {
    25         mLocationClient = new LocationClient(getApplicationContext());
    26         //声明LocationClient类
    27         mLocationClient.registerLocationListener(myListener);
    28         LocationClientOption option = new LocationClientOption();
    29         option.setIsNeedAddress(true);
    30         option.setNeedNewVersionRgc(true);
    31         mLocationClient.setLocOption(option);
    32         //注册监听函数
    33         mLocationClient.start();
    34     }
    所以大概是这样的
    LocationClient这个类定义的是用户使用的类
    MyLocationLisenter 这个类定义一个监听的工具,BDLocation这个类用来获取具体的地址字符串
    mLocationClient.registerLocationListener(myListener);  这句话就是把用户的类和监听类绑定在一起

    28-30行的内容是用户对这个地图的选项设置

    LocationClientOption lcOption = new LocationClientOption();

    //设置定位模式:高精度,低功耗,仅设备
    lcOption.setLocationMode(LocationMode.Hight_Accuracy);
    //设置坐标系
    lcOption.setCoorType("bd09ll");
    //设置GPS打开
    lcOption.setOpenGps(true);
    //设置需要地址信息
    lcOption.setIsNeedAddress(true);
    //设置每秒更新一次位置信息
    lcOption.setScanSpan(1000);
    //设置需要位置描述信息
    lcOption.setIsNeedLocationDescribe(true);

    mBdLocationManager.setLocOption(lcOption);
    ————————————————
    版权声明:本文为CSDN博主「秋天该很好」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/qq_36444585/article/details/78152765

  • 相关阅读:
    Cygwin一些设置总结!
    【补题】牛客58矩阵消除【数据水的一匹,算法:二进制枚举】
    【补题】牛客58E题(数学)
    [补题]牛客练习56,迷宫【orz】
    【补题】牛客58E题(数学)
    判断两个二叉树是否相同
    判断两个二叉树是否相同
    利用费马小定理求逆元
    [补题]牛客练习56,迷宫【orz】
    【补题】牛客58矩阵消除【数据水的一匹,算法:二进制枚举】
  • 原文地址:https://www.cnblogs.com/rainbow-1/p/14486145.html
Copyright © 2011-2022 走看看