鲸鱼的抽屉  

遇到的问题及解决过程

  • 「问题1」Android Studio-R文件出错
  • 解决:参考Android Studio-R文件错误的解决办法步骤如下:
    • 第一步:检查xml文件,R文件错误通常是由于我们的xml文件有问题,首先把xml文件的错误排除掉
    • 第二步:MainActivity.Java同包下新建一个类,这个类起名为R.class
    • 第三步:运行项目。这时会提示R类重复。
    • 第四步:删除刚才我们自己建的R.class类,代码可以正常运行。
  • 「问题2」getMenuInflater().inflate(R.menu.menu_main, menu);中menu出错。
  • 解决:原因在于我的res目录下没有menu文件夹,所以要先创建。书中第24、25章没有对如何写menu中的xml布局文件的介绍,自己百度了menu的格式,并参考狄惟佳同学的menu,完成任务。步骤如下:
    • 第一步:在res目录下新建一个Android resource dictionary , 如下图在Resource type下拉栏选择menu

    • 第二步:在menu文件夹下右键,新建一个Menu resource file,并为其命名。

  • Toast的makeText()方法报错
  • 解决,参考Android应用开发学习—Toast使用方法大全,最终解决方法是在主函数里利用事件处理器,设置了一个按钮来调用这个提示。
  • 「问题3」任务五中提示R类未知,用之前创建的方法也无法解决。
  • 解决:问题在于AndroidManifest.xml中的包名与实际不符,要将书中代码示例中的报名替换成自己的。在尝试解决这个问题的过程中,我找到了自动生成的R类,发现它的文件夹名字是布局文件中包的名称。此外清单文件中 android:name="com.example.dell1.helloworld.MainActivity"也要替换成自己的包名,否则无法识别MainActivity。

码云链接

实验成果

任务一

重点在于修改res目录下的layout文件夹中的activity_main.xml布局文件。代码如下

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.dell1.helloworld.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="20155312!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

android:text="20155312!"这条设定使屏幕显示了我的学号,截图如下:

任务二

主要工作是在MainActivity.java中利用Intent类,调用 startActivity方法,使其启动ThirdActivity ,搞清楚ThirdAvtivity是如何利用其它文件设定的“message”显示自己学号的。此外,还要根据代码中出现的错误,如menu等,自己创建相应的菜单文件、布局文件。

  • MainActivity.java中代码如下:
package com.example.dell1.activitydemo;

//import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;


public class MainActivity extends Activity implements
    OnTouchListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tv = (TextView) findViewById(R.id.textView1);
        tv.setOnTouchListener(this);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it
// is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public boolean onTouch(View arg0, MotionEvent event) {
        Intent intent = new Intent(this, ThirdActivity.class);
        intent.putExtra("message", "20155312张竞予");
        startActivity(intent);
        return true;
    }
}
  • 运行截图如下:

任务三

完成该任务时,出现了虽然导入了Toast包,但是不识别makeText、show等方法的问题。主要工作在于解决这个问题,让虚拟机显示默认位置的Toast消息。

  • MainActivity.java中的代码如下:
package com.example.dell1.basiccomponents;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.util.AttributeSet;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btnshow1=(Button) findViewById(R.id.btn1);
        btnshow1.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v){
                Toast toast = Toast.makeText(MainActivity.this,"20155312张竞予", Toast.LENGTH_LONG);
                toast.show();

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it
        // is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }


   /* @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId())
        {
            case R.id. id_action_add:
                //事件
                break;
            case R.id. id_action_delete:
                //事件
                break;
        }
        return true;

    }*/

}

在完成这个任务的过程中,我顺便对menu进行了学习和尝试,大概了解了怎么创建菜单栏,并触发不同的事件。

  • 运行截图如下:

任务四

这个任务比较轻松,我采用了直接在图形化界面拖拽,改变其界面的方式。

  • activity_main.xml 中代码如下:
<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:paddingLeft="2dp"
    android:paddingRight="2dp">
    <Button
        android:id="@+id/cancelButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="20155312"
        android:layout_marginTop="70dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
    <Button
        android:id="@+id/saveButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="张竞予"
        android:layout_below="@+id/cancelButton"
        android:layout_alignLeft="@+id/cancelButton"
        android:layout_alignStart="@+id/cancelButton"
        android:layout_marginTop="23dp" />
    <ImageView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_marginTop="45dp"
        android:padding="4dp"
        android:src="@android:drawable/ic_dialog_email"
        android:id="@+id/imageView"
        android:layout_below="@+id/saveButton"
        android:layout_centerHorizontal="true" />
    <LinearLayout
        android:id="@+id/filter_button_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="center|bottom"
        android:background="@android:color/white"
        android:orientation="horizontal" >
        <Button
            android:id="@+id/filterButton"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="Filter" />
        <Button
            android:id="@+id/shareButton"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="Share" />
        <Button
            android:id="@+id/deleteButton"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="Delete" />
    </LinearLayout>
</RelativeLayout>
  • 运行结果如下:

任务五

主要解决了编译无法通过的问题。

  • AndroidManifest.xml文件代码如下
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.dell1.helloworld"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

        <activity
            android:name="com.example.dell1.helloworld.MainActivity"
            android:label="@string/app_name">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

代码中编译出错的原因在于书中代码的package与自己实际项目的名称不符。

  • 运行结果如下:

posted on 2017-05-21 22:46  鲸鱼的抽屉  阅读(239)  评论(0编辑  收藏  举报