2012-01-10 11 views
0

メインメニュー(グリッドビュー)から始まり、ユーザーがクリックしたアイコンに基づいて新しいアクティビティを作成するアプリケーションを作成しようとしています。今私はアイコンの1つに空白の画面を表示して、新しいアクティビティを正常に作成できることを確認しようとしています。私の主な活動はHomeScreenと呼ばれ、最初のアイコンをクリックすると起動されるべき新しい活動はCulpaActivityと呼ばれます。私はHomeScreen、CulpaActivity、Manifestを掲示しています。Android:新しいアクティビティの開始に問題がある

public class HomeScreen extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
     public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     GridView gridview = (GridView) findViewById(R.id.gridview); 
     gridview.setAdapter(new ImageAdapter(this)); 
     Drawable background = this.getResources().getDrawable(R.drawable.bg); 
     gridview.setBackgroundDrawable(background); 

    gridview.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
      Toast.makeText(HomeScreen.this, "" + position, Toast.LENGTH_SHORT).show(); 

      Intent cuAppIntent = null; 
      switch (position){ 
      case 0: 
       cuAppIntent.setClass(HomeScreen.this, CulpaActivity.class); 
      } 
     } 
    });  
    } 
} 

public class CulpaActivity extends Activity{ 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     GridView gridview = (GridView) findViewById(R.id.gridview); 
     gridview.setAdapter(new ImageAdapter(this)); 
     gridview.setBackgroundDrawable(null); 
    } 
} 

そしてここにマニフェストです:あなたが意図を作成した後

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

<supports-screens 
    android:anyDensity="true" 
    android:largeScreens="true" 
    android:normalScreens="true" 
    android:resizeable="true" 
    android:smallScreens="true" /> 

<application 
    android:debuggable="true" 
    android:icon="@drawable/icon" 
    android:label="@string/app_name" > 
    <activity 
     android:label="@string/app_name" 
     android:name=".HomeScreen" > 
     <intent-filter > 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity android:name=".CulpaActivity" > 
    </activity> 
</application> 

<uses-sdk android:minSdkVersion="8" /> 

<uses-permission android:name="android.permission.READ_CONTACTS" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
</manifest> 
+0

cuAppIntent = new Intent(); –

答えて

3

私はこれがあなたの問題だと思います。

Intent cuAppIntent = new Intent(); 

をし、その後、追加します:

switch (position){ 
    case 0: 
     cuAppIntent.setClass(HomeScreen.this, CulpaActivity.class); 
} 

はそれを修正するには、最初に初期化しようと

startActivity(cuAppIntent); 

を右のあなたのコードのこの部分でcuAppIntent.setClass()

+0

ありがとうございます、それは動作します! – Aneem

1
cuAppIntent = new Intent(); 
cuAppIntent.setClass(HomeScreen.this, CulpaActivity.class); 
startActivity(cuAppIntent); 

。また、あなたはインテントを生成するための呼び出しをしていないように思われるので、おそらくnullポインタ例外を生成しています。まだあなたが直接最初cuAppIntentを初期化せずにcuAppIntent.setClass()にアクセスcuAppIntentnullとしてあなたが定義

Intent cuAppIntent = null; 

+0

ありがとう!今すぐ実行すると、NullPointerExceptionが発生します。理由は何ですか? – Aneem

+0

気にしないで、固定! – Aneem

0

を呼び出した後、あなたのホームページのアクティビティ

Intent cuAppIntent = null; 
     switch (position){ 
      case 0: 
        cuAppIntent=(homeScreen.this, CulpaActivity.class); 
       startActivity(cuAppIntent); 
        break; 
関連する問題