Android:启动新活动时遇到问题
我正在尝试编写一个从主菜单(网格视图)开始的应用程序,然后根据用户单击的图标创建一个新活动。现在,我尝试简单地为其中一个图标显示一个空白屏幕,以确保我可以成功创建新活动。我的主要活动称为 HomeScreen,当我单击第一个图标时应启动的新活动称为 CulpaActivity。我正在发布 HomeScreen、CulpaActivity 和 Manifest。
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>
I am trying to write an application that starts off from a main menu (a gridview) and then creates a new activity based on the icon that is clicked by the user. Right now I am trying to simply display a blank screen for one of the icons to make sure I can successfully create a new activity. My main activity is called HomeScreen and the new activity which should be launched when I click on the first icon is called CulpaActivity. I am posting HomeScreen, CulpaActivity, and the Manifest.
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);
}
}
And here is the Manifest:
<?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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为这是你的问题:
你将
cuAppIntent
定义为null
但你直接访问cuAppIntent.setClass()
而不初始化cuAppIntent
代码> 首先。要修复此问题,请尝试先初始化:
,然后
在调用 cuAppIntent.setClass() 后添加:
I think this is your problem:
You define
cuAppIntent
asnull
yet you directly accesscuAppIntent.setClass()
without initializingcuAppIntent
first.To fix it, try to initialize first:
and then add:
right after calling
cuAppIntent.setClass()
创建意图后。此外,您可能会生成空指针异常,因为您似乎没有调用来生成意图。
After you create the intent. Also you are probably generating a null pointer exception since you don't seem to make a call to generate an intent.
在您的主页活动的这部分代码中
in this part of your code in your homepage activity