Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在活动之间导航没有发生_Android - Fatal编程技术网

Android 在活动之间导航没有发生

Android 在活动之间导航没有发生,android,Android,我试图在两个活动之间导航,但应用程序崩溃并请求强制关闭 这是我的密码: 第一项活动 public class HelloWorldActivity extends Activity { /** Called when the activity is first created. */ //public boolean returnFlag = false; @Override public void onCreate(Bundle savedInstanceState) { supe

我试图在两个活动之间导航,但应用程序崩溃并请求强制关闭

这是我的密码: 第一项活动

public class HelloWorldActivity extends Activity {
/** Called when the activity is first created. */

//public boolean returnFlag = false;
@Override

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button YesButton = (Button) findViewById(R.id.button1);
    YesButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // TODO Auto-generated method stub
    Intent myIntent = new Intent(view.getContext(),Activity2.class);
            startActivity(myIntent);
            finish();
          }
    });


}
第二项活动:

public class Activity2 extends Activity {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.answerspage);


    Button butTryAgain = (Button) findViewById(R.id.id_tryagain);
    butTryAgain.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // TODO Auto-generated method stub
    Intent myIntent = new Intent(view.getContext(), HelloWorldActivity.class);
            startActivity(myIntent);
            finish();
        }
    });
}

}
main.xml-HelloWorldActivity的

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:layout_width="wrap_content"             

android:layout_height="wrap_content"android:textAppearance="?

android:attr/textAppearanceLarge" android:id="@+id/textView1"         `android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="27dp" android:text="Welcome"></TextView>`
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" `android:textAppearance="?android:attr/textAppearanceMedium" android:id="@+id/textView2" android:text="Is your question ready??" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:layout_marginTop="29dp"></TextView>`
<Button android:text="Yes" android:layout_width="wrap_content" `android:layout_height="wrap_content" android:id="@+id/button1" android:layout_alignBaseline="@+id/button2" android:layout_alignBottom="@+id/button2" android:layout_alignLeft="@+id/textView2" android:layout_marginLeft="36dp"></Button>`
<Button android:text="No"  android:layout_width="wrap_content" 

android:layout_height="wrap_content" android:id="@+id/button2" `android:layout_below="@+id/textView2" android:layout_alignRight="@+id/textView2" android:layout_marginRight="38dp" android:layout_marginTop="54dp"></Button>`
</RelativeLayout>

`
`
`
`
answerspage.xml-Activity2的xml文件

        <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout android:id="@+id/FrameLayout1" `xmlns:android="http://schemas.android.com/apk/res/android"`
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <Button android:layout_width="wrap_content" 

android:layout_height="wrap_content" android:text="Try again" android:id="@+id/button1" `android:layout_alignBaseline="@+id/id_tryagain" android:layout_alignBottom="@+id/button2"` `android:layout_alignParentLeft="true" android:layout_marginLeft="68dp"></Button>`


       <Button android:layout_height="wrap_content" android:text="Quit" `android:id="@+id/button2" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/id_quit" android:layout_marginLeft="29dp" android:layout_marginTop="127dp" android:layout_width="wrap_content"></Button>`
        <TextView android:layout_width="wrap_content" `android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/textView1" android:text="TextView" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="46dp" android:editable="true"></TextView>`
     </RelativeLayout>    

`
`
`
这是我的Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.HelloWorld"
  android:versionCode="1"
  android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".HelloWorldActivity"
              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=".Activity2"></activity>
</application>
</manifest>


I have gone through http://www.warriorpoint.com/blog/2009/05/24/android-how-to-switch-between-activities/ to implement the same

我经历过http://www.warriorpoint.com/blog/2009/05/24/android-how-to-switch-between-activities/ 实施同样的政策

在onClick方法中,不应使用view.getContext()启动意图。 相反,请使用:

Intent myIntent = new Intent(HelloWorldActivity.this,Activity2.class);
对于HelloWorld活动和

Intent myIntent = new Intent(Activity2.this, HelloWorldActivity.class);

对于onClick方法中的Activity2,您不应该使用view.getContext()来启动intent。
        <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout android:id="@+id/FrameLayout1" `xmlns:android="http://schemas.android.com/apk/res/android"`
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <Button android:layout_width="wrap_content" 

android:layout_height="wrap_content" android:text="Try again" android:id="@+id/button1" `android:layout_alignBaseline="@+id/id_tryagain" android:layout_alignBottom="@+id/button2"` `android:layout_alignParentLeft="true" android:layout_marginLeft="68dp"></Button>`


       <Button android:layout_height="wrap_content" android:text="Quit" `android:id="@+id/button2" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/id_quit" android:layout_marginLeft="29dp" android:layout_marginTop="127dp" android:layout_width="wrap_content"></Button>`
        <TextView android:layout_width="wrap_content" `android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/textView1" android:text="TextView" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="46dp" android:editable="true"></TextView>`
     </RelativeLayout>    
相反,请使用:

Intent myIntent = new Intent(HelloWorldActivity.this,Activity2.class);
对于HelloWorld活动和

Intent myIntent = new Intent(Activity2.this, HelloWorldActivity.class);

对于Activity2,您使用的上下文是错误的

        <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout android:id="@+id/FrameLayout1" `xmlns:android="http://schemas.android.com/apk/res/android"`
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <Button android:layout_width="wrap_content" 

android:layout_height="wrap_content" android:text="Try again" android:id="@+id/button1" `android:layout_alignBaseline="@+id/id_tryagain" android:layout_alignBottom="@+id/button2"` `android:layout_alignParentLeft="true" android:layout_marginLeft="68dp"></Button>`


       <Button android:layout_height="wrap_content" android:text="Quit" `android:id="@+id/button2" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/id_quit" android:layout_marginLeft="29dp" android:layout_marginTop="127dp" android:layout_width="wrap_content"></Button>`
        <TextView android:layout_width="wrap_content" `android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/textView1" android:text="TextView" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="46dp" android:editable="true"></TextView>`
     </RelativeLayout>    
试着这样做

public class HelloWorldActivity extends Activity {
/** Called when the activity is first created. */

private Context context = null;

//public boolean returnFlag = false;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Set the context here
    context = getApplicationContext();

    Button YesButton = (Button) findViewById(R.id.button1);
    YesButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // TODO Auto-generated method stub
            Intent myIntent = new Intent(context ,Activity2.class);
            startActivity(myIntent);
            finish();
        }
    });


}

希望这有帮助。

您使用的上下文是错误的

试着这样做

public class HelloWorldActivity extends Activity {
/** Called when the activity is first created. */

private Context context = null;

//public boolean returnFlag = false;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Set the context here
    context = getApplicationContext();

    Button YesButton = (Button) findViewById(R.id.button1);
    YesButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // TODO Auto-generated method stub
            Intent myIntent = new Intent(context ,Activity2.class);
            startActivity(myIntent);
            finish();
        }
    });


}

希望这有帮助。

您查看了崩溃的堆栈跟踪吗?如果您使用的是Eclipse,请切换到查看LogCat并查找崩溃时抛出的堆栈跟踪。如果无法理解堆栈跟踪,然后把它贴在这里让我们看看。我得到的是NullPointerException,堆栈跟踪显示异常处理异步线程队列异常处理异步线程队列org.eclipse.jdt.internal.debug.core.model.JDIObjectValue不能转换为org.eclipse.jdt.debug.core.ijavarrayi如果下面的两个答案不支持您的解决方案(顺便说一句,他们从HelloWorldActivity.this获取上下文是对的),发布尽可能多的堆栈跟踪。NullPointerException的源可能隐藏在堆栈跟踪中的某个位置。下面提到的解决方案没有帮助。我正在发布我在日志中获得的错误消息。请检查。错误/Zygote(33):setreuid()失败。错误号:2错误/Zygote(33):setreuid()失败。错误号:17错误/BatteryService(67):usbOnlinePath未找到错误/BatteryService(67):batteryVoltagePath未找到错误/BatteryService(67):batteryTemperaturePath未找到错误/SurfaceFlinger(67):无法打开/sys/power/wait_以获取睡眠或/sys/power/wait_以获取唤醒错误/EventHub(67):无法获取/dev/input/mouse0的驱动程序版本,不是typewriterERROR/EventHub(67):无法获取/dev/input/mouse的驱动程序版本,不是打字机错误/系统(67):无法启动核心服务错误/系统(67):java.lang.SecurityException错误/系统(67):在android.os.BinderProxy.transact(本机方法)错误/系统(67):在android.os.ServiceManagerProxy.addService(serviceManagerActive.java:146)错误/系统(67):在android.os.ServiceManager.addService(ServiceManager.java:72)错误/系统(67):在com.android.server.ServerThread.run(SystemServer.java:184)错误/声音池(67):加载/system/media/audio/ui/Effect_Tick.oggh时出错您是否查看了崩溃的堆栈跟踪?如果您使用Eclipse,请切换到查看LogCat并查找崩溃时溢出的堆栈跟踪。如果您无法理解堆栈跟踪,请将其发布到此处供我们查看。我收到NullPointerException,堆栈跟踪显示异常处理异步线程队列异常处理异步线程队列org.eclipse.jdt.internal.debug.core.model.JDIObjectValue不能强制转换为org.eclipse.jdt.debug.core.ijavarrayi如果以下两个答案不适用于您的解决方案(顺便说一句,它们从HelloWorldActivity.this获取上下文是正确的),发布尽可能多的堆栈跟踪。NullPointerException的源可能隐藏在堆栈跟踪中的某个位置。下面提到的解决方案没有帮助。我正在发布我在日志中获得的错误消息。请检查。错误/Zygote(33):setreuid()失败。错误号:2错误/Zygote(33):setreuid()失败。错误号:17错误/BatteryService(67):usbOnlinePath未找到错误/BatteryService(67):batteryVoltagePath未找到错误/BatteryService(67):batteryTemperaturePath未找到错误/SurfaceFlinger(67):无法打开/sys/power/wait_以获取睡眠或/sys/power/wait_以获取唤醒错误/EventHub(67):无法获取/dev/input/mouse0的驱动程序版本,不是typewriterERROR/EventHub(67):无法获取/dev/input/mouse的驱动程序版本,不是打字机错误/系统(67):无法启动核心服务错误/系统(67):java.lang.SecurityException错误/系统(67):在android.os.BinderProxy.transact(本机方法)错误/系统(67):在android.os.ServiceManagerProxy.addService(serviceManagerActive.java:146)错误/系统(67):在android.os.ServiceManager.addService(ServiceManager.java:72)错误/系统(67):在com.android.server.ServerThread.run(SystemServer.java:184)错误/声音池(67):错误加载/System/media/audio/ui/Effect\u Tick.ogg