当我开始打算去下一个活动时。我收到上述错误。我无法进行下一个活动(请参阅下面的 SecondActivity.java)。
我的主要活动是:
package com.example.testflashfile;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.view.MotionEvent;
public class MainActivity extends Activity
{
Button nextButton;
Button playButton;
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
GestureDetector gestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
setContentView(R.layout.activity_main);
gestureDetector = new GestureDetector(this.getApplicationContext(),new MyGestureDetector());
View mainview = (View) findViewById(R.id.mainView);
// Set the touch listener for the main view to be our custom gesture listener
mainview.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
});
playButton=(Button)findViewById(R.id.play);
playButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent startAnimation=new Intent(MainActivity.this,PlayAnimationActivity.class);
startAnimation.putExtra("SWF_NAME","a");
startActivity(startAnimation);
}
});
TextView helloTxt = (TextView)findViewById(R.id.displaytext);
helloTxt.setText(readTxt());
nextButton=(Button)findViewById(R.id.next);
nextButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private String readTxt(){
InputStream inputStream = getResources().openRawResource(R.raw.textone);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int i;
try {
i = inputStream.read();
while (i != -1)
{
byteArrayOutputStream.write(i);
i = inputStream.read();
}
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return byteArrayOutputStream.toString();
}
class MyGestureDetector extends SimpleOnGestureListener
{
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
return false;
}
// right to left swipe
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Intent i= new Intent(MainActivity.this,SecondActivity.class);
startActivity(i);
// left to right swipe
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Intent i= new Intent(MainActivity.this,FifthActivity.class);
startActivity(i);
}
return false;
}
@Override
public boolean onDown(MotionEvent e) {
return true;
}
}
}
第二个活动如下:
package com.example.testflashfile;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class SecondActivity extends Activity {
Button nextButton2;
Button backButton2;
Button playButton2;
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
GestureDetector gestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
setContentView(R.layout.second);
gestureDetector = new GestureDetector(this.getApplicationContext(),new MyGestureDetector());
View mainview = (View) findViewById(R.id.mainView);
// Set the touch listener for the main view to be our custom gesture listener
mainview.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
});
TextView helloTxt = (TextView)findViewById(R.id.displaytexttwo);
helloTxt.setText(readTxt());
nextButton2=(Button)findViewById(R.id.nexttwo);
nextButton2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(SecondActivity.this,ThirdActivity.class);
startActivity(intent);
}
});
playButton2=(Button)findViewById(R.id.play);
playButton2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent startAnimation=new Intent(SecondActivity.this,PlayAnimationActivity.class);
startAnimation.putExtra("SWF_NAME","b");
startActivity(startAnimation);
}
});
backButton2=(Button)findViewById(R.id.backtwo);
backButton2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(SecondActivity.this,MainActivity.class);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private String readTxt(){
InputStream inputStream = getResources().openRawResource(R.raw.texttwo);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int i;
try {
i = inputStream.read();
while (i != -1)
{
byteArrayOutputStream.write(i);
i = inputStream.read();
}
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return byteArrayOutputStream.toString();
}
class MyGestureDetector extends SimpleOnGestureListener
{
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
return false;
}
// right to left swipe
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Intent i= new Intent(SecondActivity.this,ThirdActivity.class);
startActivity(i);
// left to right swipe
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Intent i= new Intent(SecondActivity.this,MainActivity.class);
startActivity(i);
}
return false;
}
@Override
public boolean onDown(MotionEvent e) {
return true;
}
}
}
我不明白这里哪里出错了。我已经检查了与此错误相关的堆栈溢出的类似线程。但解释并不那么清楚。任何帮助/建议表示赞赏。提前感谢您抽出宝贵时间阅读如此冗长的帖子。
日志猫是:
03-11 10:03:18.599: D/dalvikvm(336): GC_EXTERNAL_ALLOC freed 66K, 52% free 2585K/5379K, external 2032K/2137K, paused 92ms
03-11 10:03:18.729: D/AndroidRuntime(336): Shutting down VM
03-11 10:03:18.729: W/dalvikvm(336): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-11 10:03:18.749: E/AndroidRuntime(336): FATAL EXCEPTION: main
03-11 10:03:18.749: E/AndroidRuntime(336): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testflashfile/com.example.testflashfile.SecondActivity}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
03-11 10:03:18.749: E/AndroidRuntime(336): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-11 10:03:18.749: E/AndroidRuntime(336): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-11 10:03:18.749: E/AndroidRuntime(336): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-11 10:03:18.749: E/AndroidRuntime(336): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-11 10:03:18.749: E/AndroidRuntime(336): at android.os.Handler.dispatchMessage(Handler.java:99)
03-11 10:03:18.749: E/AndroidRuntime(336): at android.os.Looper.loop(Looper.java:123)
03-11 10:03:18.749: E/AndroidRuntime(336): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-11 10:03:18.749: E/AndroidRuntime(336): at java.lang.reflect.Method.invokeNative(Native Method)
03-11 10:03:18.749: E/AndroidRuntime(336): at java.lang.reflect.Method.invoke(Method.java:507)
03-11 10:03:18.749: E/AndroidRuntime(336): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-11 10:03:18.749: E/AndroidRuntime(336): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-11 10:03:18.749: E/AndroidRuntime(336): at dalvik.system.NativeStart.main(Native Method)
03-11 10:03:18.749: E/AndroidRuntime(336): Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
03-11 10:03:18.749: E/AndroidRuntime(336): at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:181)
03-11 10:03:18.749: E/AndroidRuntime(336): at android.app.Activity.requestWindowFeature(Activity.java:2729)
03-11 10:03:18.749: E/AndroidRuntime(336): at com.example.testflashfile.SecondActivity.onCreate(SecondActivity.java:35)
03-11 10:03:18.749: E/AndroidRuntime(336): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-11 10:03:18.749: E/AndroidRuntime(336): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-11 10:03:18.749: E/AndroidRuntime(336): ... 11 more
03-11 10:03:21.539: I/Process(336): Sending signal. PID: 336 SIG: 9
03-11 12:37:51.179: D/dalvikvm(382): GC_EXTERNAL_ALLOC freed 67K, 53% free 2581K/5379K, external 2032K/2137K, paused 199ms
03-11 12:37:51.478: D/AndroidRuntime(382): Shutting down VM
03-11 12:37:51.479: W/dalvikvm(382): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-11 12:37:51.722: E/AndroidRuntime(382): FATAL EXCEPTION: main
03-11 12:37:51.722: E/AndroidRuntime(382): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testflashfile/com.example.testflashfile.SecondActivity}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
03-11 12:37:51.722: E/AndroidRuntime(382): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-11 12:37:51.722: E/AndroidRuntime(382): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-11 12:37:51.722: E/AndroidRuntime(382): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-11 12:37:51.722: E/AndroidRuntime(382): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-11 12:37:51.722: E/AndroidRuntime(382): at android.os.Handler.dispatchMessage(Handler.java:99)
03-11 12:37:51.722: E/AndroidRuntime(382): at android.os.Looper.loop(Looper.java:123)
03-11 12:37:51.722: E/AndroidRuntime(382): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-11 12:37:51.722: E/AndroidRuntime(382): at java.lang.reflect.Method.invokeNative(Native Method)
03-11 12:37:51.722: E/AndroidRuntime(382): at java.lang.reflect.Method.invoke(Method.java:507)
03-11 12:37:51.722: E/AndroidRuntime(382): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-11 12:37:51.722: E/AndroidRuntime(382): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-11 12:37:51.722: E/AndroidRuntime(382): at dalvik.system.NativeStart.main(Native Method)
03-11 12:37:51.722: E/AndroidRuntime(382): Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
03-11 12:37:51.722: E/AndroidRuntime(382): at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:181)
03-11 12:37:51.722: E/AndroidRuntime(382): at android.app.Activity.requestWindowFeature(Activity.java:2729)
03-11 12:37:51.722: E/AndroidRuntime(382): at com.example.testflashfile.SecondActivity.onCreate(SecondActivity.java:35)
03-11 12:37:51.722: E/AndroidRuntime(382): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-11 12:37:51.722: E/AndroidRuntime(382): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-11 12:37:51.722: E/AndroidRuntime(382): ... 11 more
03-11 12:37:57.159: I/Process(382): Sending signal. PID: 382 SIG: 9
03-11 12:40:41.579: W/ActivityThread(418): Application com.example.testflashfile is waiting for the debugger on port 8100...
03-11 12:40:41.649: I/System.out(418): Sending WAIT chunk
03-11 12:40:42.381: I/dalvikvm(418): Debugger is active
03-11 12:40:42.499: I/System.out(418): Debugger has connected
03-11 12:40:42.499: I/System.out(418): waiting for debugger to settle...
03-11 12:40:42.703: I/System.out(418): waiting for debugger to settle...
03-11 12:40:43.011: I/System.out(418): waiting for debugger to settle...
03-11 12:40:43.209: I/System.out(418): waiting for debugger to settle...
03-11 12:40:43.419: I/System.out(418): waiting for debugger to settle...
03-11 12:40:43.619: I/System.out(418): waiting for debugger to settle...
03-11 12:40:43.869: I/System.out(418): waiting for debugger to settle...
03-11 12:40:44.070: I/System.out(418): waiting for debugger to settle...
03-11 12:40:44.269: I/System.out(418): waiting for debugger to settle...
03-11 12:40:44.469: I/System.out(418): waiting for debugger to settle...
03-11 12:40:44.720: I/System.out(418): waiting for debugger to settle...
03-11 12:40:44.925: I/System.out(418): waiting for debugger to settle...
03-11 12:40:45.140: I/System.out(418): waiting for debugger to settle...
03-11 12:40:45.339: I/System.out(418): waiting for debugger to settle...
03-11 12:40:45.559: I/System.out(418): waiting for debugger to settle...
03-11 12:40:45.793: I/System.out(418): waiting for debugger to settle...
03-11 12:40:46.031: I/System.out(418): waiting for debugger to settle...
03-11 12:40:46.261: I/System.out(418): waiting for debugger to settle...
03-11 12:40:46.468: I/System.out(418): waiting for debugger to settle...
03-11 12:40:46.669: I/System.out(418): waiting for debugger to settle...
03-11 12:40:46.875: I/System.out(418): waiting for debugger to settle...
03-11 12:40:47.109: I/System.out(418): debugger has settled (1505)
03-11 12:49:24.089: D/dalvikvm(418): GC_EXTERNAL_ALLOC freed 68K, 52% free 2583K/5379K, external 2032K/2137K, paused 212ms
03-11 12:51:01.819: D/AndroidRuntime(418): Shutting down VM
03-11 12:51:01.839: W/dalvikvm(418): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-11 12:51:07.499: E/AndroidRuntime(418): FATAL EXCEPTION: main
03-11 12:51:07.499: E/AndroidRuntime(418): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testflashfile/com.example.testflashfile.SecondActivity}: java.lang.NullPointerException
03-11 12:51:07.499: E/AndroidRuntime(418): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-11 12:51:07.499: E/AndroidRuntime(418): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-11 12:51:07.499: E/AndroidRuntime(418): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-11 12:51:07.499: E/AndroidRuntime(418): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-11 12:51:07.499: E/AndroidRuntime(418): at android.os.Handler.dispatchMessage(Handler.java:99)
03-11 12:51:07.499: E/AndroidRuntime(418): at android.os.Looper.loop(Looper.java:123)
03-11 12:51:07.499: E/AndroidRuntime(418): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-11 12:51:07.499: E/AndroidRuntime(418): at java.lang.reflect.Method.invokeNative(Native Method)
03-11 12:51:07.499: E/AndroidRuntime(418): at java.lang.reflect.Method.invoke(Method.java:507)
03-11 12:51:07.499: E/AndroidRuntime(418): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-11 12:51:07.499: E/AndroidRuntime(418): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-11 12:51:07.499: E/AndroidRuntime(418): at dalvik.system.NativeStart.main(Native Method)
03-11 12:51:07.499: E/AndroidRuntime(418): Caused by: java.lang.NullPointerException
03-11 12:51:07.499: E/AndroidRuntime(418): at com.example.testflashfile.SecondActivity.onCreate(SecondActivity.java:69)
03-11 12:51:07.499: E/AndroidRuntime(418): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-11 12:51:07.499: E/AndroidRuntime(418): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-11 12:51:07.499: E/AndroidRuntime(418): ... 11 more
03-11 12:56:07.749: I/Process(418): Sending signal. PID: 418 SIG: 9
03-11 14:17:47.570: D/dalvikvm(457): GC_EXTERNAL_ALLOC freed 68K, 53% free 2581K/5379K, external 2032K/2137K, paused 116ms
03-11 14:17:47.699: D/AndroidRuntime(457): Shutting down VM
03-11 14:17:47.699: W/dalvikvm(457): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-11 14:17:47.729: E/AndroidRuntime(457): FATAL EXCEPTION: main
03-11 14:17:47.729: E/AndroidRuntime(457): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testflashfile/com.example.testflashfile.SecondActivity}: java.lang.NullPointerException
03-11 14:17:47.729: E/AndroidRuntime(457): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-11 14:17:47.729: E/AndroidRuntime(457): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-11 14:17:47.729: E/AndroidRuntime(457): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-11 14:17:47.729: E/AndroidRuntime(457): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-11 14:17:47.729: E/AndroidRuntime(457): at android.os.Handler.dispatchMessage(Handler.java:99)
03-11 14:17:47.729: E/AndroidRuntime(457): at android.os.Looper.loop(Looper.java:123)
03-11 14:17:47.729: E/AndroidRuntime(457): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-11 14:17:47.729: E/AndroidRuntime(457): at java.lang.reflect.Method.invokeNative(Native Method)
03-11 14:17:47.729: E/AndroidRuntime(457): at java.lang.reflect.Method.invoke(Method.java:507)
03-11 14:17:47.729: E/AndroidRuntime(457): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-11 14:17:47.729: E/AndroidRuntime(457): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-11 14:17:47.729: E/AndroidRuntime(457): at dalvik.system.NativeStart.main(Native Method)
03-11 14:17:47.729: E/AndroidRuntime(457): Caused by: java.lang.NullPointerException
03-11 14:17:47.729: E/AndroidRuntime(457): at com.example.testflashfile.SecondActivity.onCreate(SecondActivity.java:44)
03-11 14:17:47.729: E/AndroidRuntime(457): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-11 14:17:47.729: E/AndroidRuntime(457): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-11 14:17:47.729: E/AndroidRuntime(457): ... 11 more
清单是:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testflashfile"
android:versionCode="1"
android:versionName="1.0"
>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17"
/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:hardwareAccelerated="true"
android:debuggable="true">
<activity
android:screenOrientation="portrait"
android:name="com.example.testflashfile.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="com.example.testflashfile.SecondActivity"
android:screenOrientation="portrait"></activity>
<activity android:name="com.example.testflashfile.ThirdActivity"
android:screenOrientation="portrait"></activity>
<activity android:name="com.example.testflashfile.FourthActivity"
android:screenOrientation="portrait"></activity>
<activity android:name="com.example.testflashfile.FifthActivity"
android:screenOrientation="portrait"></activity>
<activity android:name="com.example.testflashfile.PlayAnimationActivity"
android:screenOrientation="portrait"></activity>
</application>
</manifest>