启动模拟器一直报错提示unfortunately 项目名 has stopped
package com.example.borrowbookproj; import java.util.ArrayList; import java.util.HashMap; //import java.util.Iterator; import java.util.List; import java.util.Map; //import java.util.Map.Entry; import java.util.Set; import android.support.v7.app.ActionBarActivity; //import android.graphics.drawable.Drawable; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.ImageView; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; import android.widget.Toast; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; public class MainActivity extends ActionBarActivity { private EditText et_reader, et_borrowoutime, et_backtime; private RadioGroup rg_sex; private RadioButton rb_man, rb_woman; private CheckBox cb_history, cb_suspense, cb_literature; private SeekBar sb_age; private Button btn_find, btn_next; private TextView tv1, tv_books, tv_age, book_name, book_type, book_age; private int age; private ImageView iv_out; private int bookssize; Map<String, String> massagemap; List<Book> book_choice; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //initMethod(); tv1 = (TextView) findViewById(R.id.showmassage); tv1.setText("测试"); //workMethod(); rg_sex.clearCheck(); } // private void initMethod() { // et_reader = (EditText) findViewById(R.id.reader); // et_borrowoutime = (EditText) findViewById(R.id.borrowtime); // et_backtime = (EditText) findViewById(R.id.backtime); // rb_man = (RadioButton) findViewById(R.id.man); // rb_woman = (RadioButton) findViewById(R.id.woman); // cb_history = (CheckBox) findViewById(R.id.history); // cb_suspense = (CheckBox) findViewById(R.id.suspense); // cb_literature = (CheckBox) findViewById(R.id.literature); // sb_age = (SeekBar) findViewById(R.id.seekBarage); // btn_find = (Button) findViewById(R.id.findbutton); //// btn_next = (Button) findViewById(R.id.nextbutton); // tv1 = (TextView) findViewById(R.id.showmassage); // tv_books = (TextView) findViewById(R.id.books); // tv_age = (TextView) findViewById(R.id.age); // iv_out = (ImageView) findViewById(R.id.bookimage); // book_name = (TextView) findViewById(R.id.bookname); // book_type = (TextView) findViewById(R.id.type); // book_age = (TextView) findViewById(R.id.applyage); // initData(); // } // private void workMethod() { // rg_sex.setOnCheckedChangeListener(new SexRadioGroup()); // btn_find.setOnClickListener(new myOnClickListener()); // //btn_next.setOnClickListener(new myOnClickListener()); // cb_history.setOnClickListener(new myOnClickListener()); // sb_age.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { // @Override // public void onStopTrackingTouch(SeekBar seekBar) { // // 将在结束拖拽进度条时被触发 // Log.i("progress", "结束 " + seekBar.getProgress() + ""); // age = seekBar.getProgress(); // tv_age.setText("" + age); // } // // @Override // public void onStartTrackingTouch(SeekBar seekBar) { // } // // @Override // public void onProgressChanged(SeekBar seekBar, int progress, // boolean fromUser) { // } // }); // } // class SexRadioGroup implements OnCheckedChangeListener { // // @Override // public void onCheckedChanged(RadioGroup arg0, int arg1) { // String str = null; // switch (arg1) { // case R.id.man: // str = rb_man.getText().toString(); // break; // case R.id.woman: // str = rb_woman.getText().toString(); // break; // } // massagemap.put("性别", str); // } // // } // // class myOnClickListener implements OnClickListener { // // @Override // public void onClick(View arg0) { // // switch (arg0.getId()) { // case R.id.findbutton: // addAllMassage(); // // Toast.makeText(MainActivity.this, str.toString(), // // 5000).show(); // break; // // case R.id.nextbutton: // // if ((bookssize + 1) > book_choice.size()) // // bookssize = 0; // // iv_out.setImageResource(book_choice.get(bookssize).getImage()); // // bookname.setText(book_choice.get(bookssize).getBookname()); // // booktype.setText(book_choice.get(bookssize).getBooktype()); // // bookage.setText(book_choice.get(bookssize).getBookage()); // // bookssize++; // // break; // // } // // } // // } // // private void addAllMassage() { // List<String> str_allmassage = new ArrayList<String>(); // List<String> lists = new ArrayList<String>(); // massagemap.put("读者", et_reader.getText().toString()); // massagemap.put("借出时间", et_borrowoutime.getText().toString()); // if (cb_history.isChecked()) // lists.add(cb_history.getText().toString()); // if (cb_suspense.isChecked()) // lists.add(cb_suspense.getText().toString()); // if (cb_literature.isChecked()) // lists.add(cb_literature.getText().toString()); // massagemap.put("爱好", lists.toString()); // Set<String> keys = massagemap.keySet(); // for (String key : keys) { // str_allmassage.add(key + ":" + massagemap.get(key)); // } // tv1.setText(str_allmassage.toString()); // // for (Book b : book_choice) { // // if (b.getBookage() > age) { // // book_choice.remove(b); // // } // // } // // } // // private void initData() { // massagemap = new HashMap<String, String>(); // book_choice = new ArrayList<Book>(); // bookssize = 0; // book_choice.add(new Book(R.drawable.aa, "人生感悟", "郑志国", 20)); // book_choice.add(new Book(R.drawable.bb, "边城", "沈从文", 12)); // book_choice.add(new Book(R.drawable.cc, "Sapir", "PROFESSIONAL", 12)); // book_choice.add(new Book(R.drawable.dd, "光辉岁月", "看不清", 50)); // book_choice.add(new Book(R.drawable.ee, "边城", "沈从文", 6)); // book_choice.add(new Book(R.drawable.f, "无", "无", 99)); // book_choice.add(new Book(R.drawable.ff, "中国古代文学", "赵东栓 刘富伟", 16)); // book_choice.add(new Book(R.drawable.gg, "无花果", "张婉丽", 16)); // book_choice.add(new Book(R.drawable.hh, "古镇记忆", "南方", 16)); // } }
package com.example.borrowbookproj; import android.graphics.drawable.Drawable; public class Book { private int image,bookage; private String bookname,booktype; private String[] aaa; public Book(int image, String bookname, String booktype, int bookage) { super(); this.image = image; this.bookname = bookname; this.booktype = booktype; this.bookage = bookage; } public int getImage() { return image; } public void setImage(int image) { this.image = image; } public String getBookname() { return bookname; } public void setBookname(String bookname) { this.bookname = bookname; } public String getBooktype() { return booktype; } public void setBooktype(String booktype) { this.booktype = booktype; } public int getBookage() { return bookage; } public void setBookage(int bookage) { this.bookage = bookage; } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.borrowbookproj" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".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> <activity android:name=".Book" android:label="@string/app_name" > </activity> </application> </manifest>
<LinearLayout 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:background="#FFE1FF" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/borrowbook" android:textColor="#EEAEEE" android:textSize="20dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/reader" /> <EditText android:id="@+id/reader" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:inputType="text" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/sex" /> <RadioGroup android:id="@+id/sex" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioButton android:id="@+id/man" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/man" /> <RadioButton android:id="@+id/woman" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/woman" /> </RadioGroup> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/borrowouttime" /> <EditText android:id="@+id/borrowtime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:inputType="date" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/backtime" /> <EditText android:id="@+id/backtime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@null" android:inputType="date" android:text="2019-01-06" android:textSize="15dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hobby" /> <CheckBox android:id="@+id/history" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/history" /> <CheckBox android:id="@+id/suspense" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/suspense" /> <CheckBox android:id="@+id/literature" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/literature" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/age" /> <TextView android:id="@+id/age" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="right" android:text="18" /> <SeekBar android:id="@+id/seekBarage" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:max="100" android:progress="18" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="left" android:text="100" /> </LinearLayout> <LinearLayout android:layout_width="300dp" android:layout_height="100dp" android:layout_gravity="center" android:orientation="horizontal" > <LinearLayout android:layout_width="200dp" android:layout_height="100dp" android:orientation="vertical" > <ImageView android:id="@+id/bookimage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/aa" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/bookname" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" android:text="@string/bookname" /> <TextView android:id="@+id/type" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" android:text="@string/type" /> <TextView android:id="@+id/applyage" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" android:text="@string/applyage" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/findbutton" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#A066D3" android:text="@string/findbutton" /> <TextView android:id="@+id/books" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="left" android:text="符合条件的书有3本" /> <Button android:id="@+id/nextbutton" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginRight="50dp" android:layout_weight="1" android:background="#A066D3" android:text="@string/nextbutton" /> <TextView android:id="@+id/n" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="left" android:text="" /> </LinearLayout> <TextView android:id="@+id/showmassage" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
机器执行后以下为log日志,加粗的为红色
01-09 03:55:00.722: I/Process(885): Sending signal. PID: 885 SIG: 9
01-09 03:55:07.152: D/dalvikvm(1089): GC_FOR_ALLOC freed 75K, 5% free 3176K/3324K, paused 24ms, total 26ms
01-09 03:55:07.172: I/dalvikvm-heap(1089): Grow heap (frag case) to 5.925MB for 2889448-byte allocation
01-09 03:55:07.202: D/dalvikvm(1089): GC_FOR_ALLOC freed 9K, 3% free 5989K/6148K, paused 29ms, total 29ms
01-09 03:55:07.342: D/AndroidRuntime(1089): Shutting down VM
01-09 03:55:07.342: W/dalvikvm(1089): threadid=1: thread exiting with uncaught exception (group=0xb3a65ba8)
01-09 03:55:07.362: E/AndroidRuntime(1089): FATAL EXCEPTION: main
01-09 03:55:07.362: E/AndroidRuntime(1089): Process: com.example.borrowbookproj, PID: 1089
01-09 03:55:07.362: E/AndroidRuntime(1089): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
01-09 03:55:07.362: E/AndroidRuntime(1089): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
01-09 03:55:07.362: E/AndroidRuntime(1089): at android.app.ActivityThread.access$800(ActivityThread.java:135)
01-09 03:55:07.362: E/AndroidRuntime(1089): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
01-09 03:55:07.362: E/AndroidRuntime(1089): at android.os.Handler.dispatchMessage(Handler.java:102)
01-09 03:55:07.362: E/AndroidRuntime(1089): at android.os.Looper.loop(Looper.java:136)
01-09 03:55:07.362: E/AndroidRuntime(1089): at android.app.ActivityThread.main(ActivityThread.java:5017)
01-09 03:55:07.362: E/AndroidRuntime(1089): at java.lang.reflect.Method.invokeNative(Native Method)
01-09 03:55:07.362: E/AndroidRuntime(1089): at java.lang.reflect.Method.invoke(Method.java:515)
01-09 03:55:07.362: E/AndroidRuntime(1089): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-09 03:55:07.362: E/AndroidRuntime(1089): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-09 03:55:07.362: E/AndroidRuntime(1089): at dalvik.system.NativeStart.main(Native Method)
正在回答
下图红框处的代码在使用之前没有使用findViewById()方法进行初始化
相似问题
登录后可查看更多问答,登录/注册
- 参与学习 人
- 提交作业 5461 份
- 解答问题 7238 个
此次推出的专题为Android攻城狮培养计划的第一部分语法与界面基础篇,将带大家从0开始学习Android开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星