App force close thenewboston android教程
Posted
技术标签:
【中文标题】App force close thenewboston android教程【英文标题】:App force close thenewboston android tutorial 【发布时间】:2016-10-30 09:44:39 【问题描述】:我一直在阅读 thenewboston 的 android 教程,并被困在第 41 篇教程中。我一打开相机活动,我的应用就会强制关闭。
Camera.java
package com.thenewboston.akshit;
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
public class Camera extends Activity implements View.OnClickListener
ImageButton ib;
Button b;
ImageView iv;
Intent i;
final static int cameraData=0;
Bitmap bmp;
@Override
protected void onCreate(Bundle savedInstanceState)
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.photo);
initialize();
private void initialize()
iv = (ImageView) findViewById(R.id.ivReturnedPic);
b = (Button) findViewById(R.id.bSetWall);
iv = (ImageView) findViewById(R.id.ivReturnedPic);
b.setOnClickListener(this);
ib.setOnClickListener(this);
@Override
public void onClick(View v)
// TODO Auto-generated method stub
switch (v.getId())
case R.id.bSetWall:
try
getApplicationContext().setWallpaper(bmp);
catch (IOException e) e.printStackTrace();
break;
case R.id.ibTakePic:
i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i,cameraData);
break;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK)
Bundle extras =data.getExtras();
bmp = (Bitmap) extras.get("data");
iv.setImageBitmap(bmp);
菜单.java
package com.thenewboston.akshit;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Menu extends ListActivity
String classes[] = "StartingPoint", "TextPlay", "Email", "Camera";
@Override
protected void onCreate(Bundle savedInstanceState)
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, classes));
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
// TODO Auto-generated method stub
String cheese = classes[position];
super.onListItemClick(l, v, position, id);
try
Class ourClass = Class.forName("com.thenewboston.akshit." + cheese);
Intent ourIntent = new Intent(Menu.this, ourClass);
startActivity(ourIntent);
catch(ClassNotFoundException e)
e.printStackTrace();
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.thenewboston.akshit"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.thenewboston.akshit.Splash"
android:label="Splash" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.thenewboston.akshit.StartingPoint"
android:label="Starting Point" >
<intent-filter>
<action android:name="com.thenewboston.akshit.STARTINGPOINT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.thenewboston.akshit.Menu"
android:label="Starting Point" >
<intent-filter>
<action android:name="com.thenewboston.akshit.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.thenewboston.akshit.TextPlay"
android:label="Starting Point" >
</activity>
<activity
android:name="com.thenewboston.akshit.Email"
android:label="Starting Point" >
</activity>
<activity
android:name="com.thenewboston.akshit.Camera"
android:label="Starting Point" >
</activity>
</application>
日志
06-28 03:23:03.080: D/dalvikvm(871): Not late-enabling CheckJNI (already on)
06-28 03:23:06.280: D/gralloc_goldfish(871): Emulator without GPU emulation detected.
06-28 03:23:09.520: D/dalvikvm(871): GC_FOR_ALLOC freed 55K, 5% free 3032K/3172K, paused 124ms, total 130ms
06-28 03:23:09.520: I/dalvikvm-heap(871): Grow heap (frag case) to 3.646MB for 635812-byte allocation
06-28 03:23:09.590: D/dalvikvm(871): GC_FOR_ALLOC freed 6K, 4% free 3646K/3796K, paused 63ms, total 63ms
06-28 03:23:10.210: I/Choreographer(871): Skipped 72 frames! The application may be doing too much work on its main thread.
06-28 03:23:15.810: D/AndroidRuntime(871): Shutting down VM
06-28 03:23:15.810: W/dalvikvm(871): threadid=1: thread exiting with uncaught exception (group=0xb3a36b90)
06-28 03:23:15.850: E/AndroidRuntime(871): FATAL EXCEPTION: main
06-28 03:23:15.850: E/AndroidRuntime(871): Process: com.thenewboston.akshit, PID: 871
06-28 03:23:15.850: E/AndroidRuntime(871): java.lang.RuntimeException: Unable to start activity ComponentInfocom.thenewboston.akshit/com.thenewboston.akshit.Camera: java.lang.NullPointerException
06-28 03:23:15.850: E/AndroidRuntime(871): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
06-28 03:23:15.850: E/AndroidRuntime(871): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
06-28 03:23:15.850: E/AndroidRuntime(871): at android.app.ActivityThread.access$700(ActivityThread.java:135)
06-28 03:23:15.850: E/AndroidRuntime(871): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
06-28 03:23:15.850: E/AndroidRuntime(871): at android.os.Handler.dispatchMessage(Handler.java:102)
06-28 03:23:15.850: E/AndroidRuntime(871): at android.os.Looper.loop(Looper.java:137)
06-28 03:23:15.850: E/AndroidRuntime(871): at android.app.ActivityThread.main(ActivityThread.java:4998)
06-28 03:23:15.850: E/AndroidRuntime(871): at java.lang.reflect.Method.invokeNative(Native Method)
06-28 03:23:15.850: E/AndroidRuntime(871): at java.lang.reflect.Method.invoke(Method.java:515)
06-28 03:23:15.850: E/AndroidRuntime(871): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
06-28 03:23:15.850: E/AndroidRuntime(871): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
06-28 03:23:15.850: E/AndroidRuntime(871): at dalvik.system.NativeStart.main(Native Method)
06-28 03:23:15.850: E/AndroidRuntime(871): Caused by: java.lang.NullPointerException
06-28 03:23:15.850: E/AndroidRuntime(871): at com.thenewboston.akshit.Camera.initialize(Camera.java:37)
06-28 03:23:15.850: E/AndroidRuntime(871): at com.thenewboston.akshit.Camera.onCreate(Camera.java:29)
06-28 03:23:15.850: E/AndroidRuntime(871): at android.app.Activity.performCreate(Activity.java:5243)
06-28 03:23:15.850: E/AndroidRuntime(871): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-28 03:23:15.850: E/AndroidRuntime(871): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
06-28 03:23:15.850: E/AndroidRuntime(871): ... 11 more
06-28 03:23:19.850: I/Process(871): Sending signal. PID: 871 SIG: 9
【问题讨论】:
仔细查看您的initialize()
方法。如果您的布局包含预期的View
s,那么您只是缺少那里的初始化。 stack trace 告诉你哪一行有问题。
【参考方案1】:
在为它设置点击侦听器之前,您必须在初始化函数上设置对 ib 的引用。
ib = (ImageButton) findViewById(R.id.ib) //Or the name that the ImageButton has
ib.setOnClickListener(this);
【讨论】:
以上是关于App force close thenewboston android教程的主要内容,如果未能解决你的问题,请参考以下文章
Mysql报错Forcing close of thread 139 user: 'root'
adb shell am force-stop杀不死某些app的原因