以下是我的代码。请告诉我我做错了什么?
package version.nitt;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import version.nitt.R;
import android.util.Log;
import android.content.Intent;
public class versionActivity extends Activity {
private Runnable runnable;
private static final String TAG="WElcome!";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.startscreen);
runnable= new Runnable(){
public void run()
{
try
{
Log.v(TAG,"Going to sleep...");
Thread.sleep(1000);
Log.v(TAG,"Waking up from sleep...");
}
catch(Exception e1)
{
}
}
};
try
{
Thread t=new Thread(null,runnable);
t.start();
final Intent i=new Intent(versionActivity.this,secondActivity.class);
LinearLayout lin =(LinearLayout) findViewById(R.id.ll);
lin.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
finish();
startActivity(i);
}
});
}
catch(Exception e)
{
}
Button button = (Button) findViewById(R.id.cls);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
System.exit(0);
}
});
}
}
如果用户点击屏幕上的任意位置,我会尝试从一个活动转移到另一个活动。线性布局设置为clikable。应用程序被加载。但是当执行onClick以继续进行下一个活动时,应用程序将被强制关闭。
Log Cat:
08-05 14:46:37.028: I/Process(1883): Sending signal. PID: 1883 SIG: 9
08-05 14:46:51.479: D/dalvikvm(1915): GC_FOR_ALLOC freed 31K, 4% free 6342K/6595K, paused 64ms
08-05 14:46:51.499: I/dalvikvm-heap(1915): Grow heap (frag case) to 7.279MB for 1080016-byte allocation
08-05 14:46:51.650: D/dalvikvm(1915): GC_FOR_ALLOC freed <1K, 4% free 7396K/7687K, paused 59ms
08-05 14:46:51.819: D/dalvikvm(1915): GC_CONCURRENT freed <1K, 4% free 7396K/7687K, paused 4ms+13ms
08-05 14:46:52.019: D/dalvikvm(1915): GC_FOR_ALLOC freed <1K, 4% free 7396K/7687K, paused 57ms
08-05 14:46:52.031: I/dalvikvm-heap(1915): Grow heap (frag case) to 7.737MB for 480016-byte allocation
08-05 14:46:52.162: D/dalvikvm(1915): GC_FOR_ALLOC freed 0K, 5% free 7865K/8199K, paused 65ms
08-05 14:46:52.439: D/dalvikvm(1915): GC_CONCURRENT freed 1054K, 17% free 6813K/8199K, paused 10ms+5ms
08-05 14:46:52.840: D/dalvikvm(1915): GC_CONCURRENT freed 351K, 15% free 6973K/8199K, paused 8ms+6ms
08-05 14:46:52.840: V/WElcome!(1915): Going to sleep...
08-05 14:46:53.838: V/WElcome!(1915): Waking up from sleep...
08-05 14:47:03.398: D/AndroidRuntime(1915): Shutting down VM
08-05 14:47:03.398: W/dalvikvm(1915): threadid=1: thread exiting with uncaught exception (group=0x40014760)
08-05 14:47:03.428: E/AndroidRuntime(1915): FATAL EXCEPTION: main
08-05 14:47:03.428: E/AndroidRuntime(1915): android.content.ActivityNotFoundException: Unable to find explicit activity class {version.nitt/version.nitt.secondActivity}; have you declared this activity in your AndroidManifest.xml?
08-05 14:47:03.428: E/AndroidRuntime(1915): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1504)
08-05 14:47:03.428: E/AndroidRuntime(1915): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1382)
08-05 14:47:03.428: E/AndroidRuntime(1915): at android.app.Activity.startActivityForResult(Activity.java:3131)
08-05 14:47:03.428: E/AndroidRuntime(1915): at android.app.Activity.startActivity(Activity.java:3237)
08-05 14:47:03.428: E/AndroidRuntime(1915): at version.nitt.versionActivity$2.onClick(versionActivity.java:46)
08-05 14:47:03.428: E/AndroidRuntime(1915): at android.view.View.performClick(View.java:3110)
08-05 14:47:03.428: E/AndroidRuntime(1915): at android.view.View$PerformClick.run(View.java:11934)
08-05 14:47:03.428: E/AndroidRuntime(1915): at android.os.Handler.handleCallback(Handler.java:587)
08-05 14:47:03.428: E/AndroidRuntime(1915): at android.os.Handler.dispatchMessage(Handler.java:92)
08-05 14:47:03.428: E/AndroidRuntime(1915): at android.os.Looper.loop(Looper.java:132)
08-05 14:47:03.428: E/AndroidRuntime(1915): at android.app.ActivityThread.main(ActivityThread.java:4123)
08-05 14:47:03.428: E/AndroidRuntime(1915): at java.lang.reflect.Method.invokeNative(Native Method)
08-05 14:47:03.428: E/AndroidRuntime(1915): at java.lang.reflect.Method.invoke(Method.java:491)
08-05 14:47:03.428: E/AndroidRuntime(1915): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
08-05 14:47:03.428: E/AndroidRuntime(1915): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
08-05 14:47:03.428: E/AndroidRuntime(1915): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:5)
lin.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent(versionActivity.this, secondActivity.class);
startActivity(i);
finish(); //should use the finish if you need to preserve memory
//other wise don't use it.
}
});
第二步:AndroidManifest.xml
<activity android:name=".secondActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
答案 1 :(得分:0)
使用这个简单的方法将一个活动移动到另一个活动
public void onClick(View arg0) {
Intent intent = new Intent(context, App2Activity.class);
startActivity(intent);
}
添加manifest.xml