2011-08-11 11 views
0

2つのアクティビティ間を移動しようとしていますが、アプリケーションがクラッシュして強制終了を要求しています。ここで アクティビティ間の移動が行われていません

は私のコードです: 第一の活動

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>  

これは、onClickの方法で私の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 
+0

クラッシュのスタックトレースを確認しましたか? Eclipseを使用している場合は、LogCatの表示に切り替え、クラッシュ時に吐き出されたスタックトレースを探します。スタックトレースを理解できない場合は、ここに投稿してください。 – glorifiedHacker

+0

NullPointerExceptionが発生し、スタックトレースが表示されます 例外処理非同期スレッドキュー 例外処理非同期スレッドキュー org.eclipse.jdt.internal.debug.core.model.JDIObjectValueをorg.eclipse.jdt.debugにキャストできません。 core.IJavaArray – captgeek029

+0

以下の2つの答えがあなたのソリューションを保持していない場合(彼らはHelloWorldActivityからのコンテキストを取得することが正しいです。)、LogCatから得ることができるスタックトレースを投稿します。 NullPointerExceptionのソースはスタックトレースのどこかに埋め込まれている可能性があります。 – glorifiedHacker

答えて

3

で、あなたが意図を開始するview.getContext()を使用するべきではありません。 は代わりに、使用:HelloWorldActivityため

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

とActivity2

+0

これは、 – GoodSp33d

1

ため

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

使用しているコンテキストが間違っています。

ホープ、このことができます。この

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(); 
     } 
    }); 


} 

をやってみてください。

関連する問題