2011-07-18 4 views
1

私はgetHotelName()setHotelName()を使用してアプリケーションにデータを格納し、アクセスします。 Launcherとして分類されている私の主なアクティビティでは、このアクティビティでメソッドとgetApplication()の両方が動作します。しかし、主なアクティビティから呼び出された別のアクティビティからgetApplicationにアクセスしようとすると、強制終了エラーが発生します。Android getapplication()data

これは私のマニフェストファイルである:私は私が持っていた、はい、intent.extraを使用して、それを解決しNetworkCommunication活動

public class NetworkCommunication extends Activity { 
RestaurantNetwork application = (RestaurantNetwork) getApplication(); 
String hotelname = application.getHotelName().toString(); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

答えて

1

<application android:icon="@drawable/icon" android:label="@string/app_short_name" android:name="RestaurantNetwork" android:allowClearUserData="true" android:theme="@android:style/Theme.Black" > 

    <activity android:name="RestaurantActivity" 
       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="NetworkCommunication"></activity> 

    <uses-permission android:name="android.permission.INTERNET" /> 

</application> 
主な活動で

RestaurantNetwork application = (RestaurantNetwork) getApplication(); 
application.setHotelName(this.hotelname.getText().toString()); 
Intent intent = new Intent(view.getContext(), NetworkCommunication.class); 
startActivity(intent); 

getApplication()を削除します。まだ前の方法

私の主な活動で

this.hotelname = (EditText) findViewById(R.id.HotelName); 

//RestaurantNetwork application = (RestaurantNetwork)getApplication(); 
//application.setHotelName(this.hotelname.getText().toString()); 

Intent intent = new Intent(view.getContext(), NetworkCommunication.class); 
intent.putExtra("hotelName",this.hotelname.getText().toString()); 
startActivity(intent); 
私の第二の活動で

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

     String value = null; 

     Bundle extras = getIntent().getExtras(); 
     if(extras !=null) 
     { 
      value = extras.getString("hotelName"); 
     } 

     Toast.makeText(getApplicationContext(),value, Toast.LENGTH_SHORT).show(); 
中にエラーが発生した理由を知りたいです
関連する問題