Android:启动新活动时遇到问题

发布于 2024-12-26 00:52:19 字数 2718 浏览 0 评论 0原文

我正在尝试编写一个从主菜单(网格视图)开始的应用程序,然后根据用户单击的图标创建一个新活动。现在,我尝试简单地为其中一个图标显示一个空白屏幕,以确保我可以成功创建新活动。我的主要活动称为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

榕城若虚 2025-01-02 00:52:19

我认为这是你的问题:

Intent cuAppIntent = null;

你将 cuAppIntent 定义为 null 但你直接访问 cuAppIntent.setClass() 而不初始化 cuAppIntent代码> 首先。

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

要修复此问题,请尝试先初始化:

Intent cuAppIntent = new Intent();

,然后

startActivity(cuAppIntent);

在调用 cuAppIntent.setClass() 后添加:

I think this is your problem:

Intent cuAppIntent = null;

You define cuAppIntent as null yet you directly access cuAppIntent.setClass() without initializing cuAppIntent first.

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

To fix it, try to initialize first:

Intent cuAppIntent = new Intent();

and then add:

startActivity(cuAppIntent);

right after calling cuAppIntent.setClass()

笨笨の傻瓜 2025-01-02 00:52:19
cuAppIntent = new Intent();
cuAppIntent.setClass(HomeScreen.this, CulpaActivity.class);
startActivity(cuAppIntent);

创建意图后。此外,您可能会生成空指针异常,因为您似乎没有调用来生成意图。

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

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.

秋风の叶未落 2025-01-02 00:52:19

在您的主页活动的这部分代码中

Intent cuAppIntent = null;
        switch (position){
             case 0:
                  cuAppIntent=(homeScreen.this, CulpaActivity.class);
                 startActivity(cuAppIntent);
                  break;

in this part of your code in your homepage activity

Intent cuAppIntent = null;
        switch (position){
             case 0:
                  cuAppIntent=(homeScreen.this, CulpaActivity.class);
                 startActivity(cuAppIntent);
                  break;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文