1

在这个程序中,我实现了滑动菜单库。我现在遇到的问题是为这些菜单设置点击监听器。

该库要求菜单是不同的布局,或者至少这是我实现它的方式。我现在想要实现的是对这些菜单选项实现各种 onClick 侦听器。

该程序看起来像这样(它的一小部分):

菜单.xml

 <RelativeLayout android:id="@+id/ask_a_question"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_marginBottom="5dp"
        android:onClick="getQuestion">

        <TextView android:layout_alignParentLeft="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/ask_a_question"
            android:textColor="#ffffff"
            android:layout_marginLeft="15dp"
            android:layout_centerVertical="true"/>

        <ImageView android:layout_alignParentRight="true"
            android:contentDescription="@string/ask_a_question"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/question"
            android:layout_marginRight="15dp"/>

    </RelativeLayout>

在它的MainActivity.java程序看起来像这样:

public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setBehindContentView(R.layout.menu);

        getSlidingMenu().setBehindOffset(100);

        ask_a_question = (RelativeLayout)findViewById(R.id.ask_a_question);
        ask_a_question.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                getQuestion();
            }
        });
        login = (Button)findViewById(R.id.log_in_button);
        login.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            startActivity(new Intent("com.example.btshome.LOGINACTIVITY"));
            }
        });
    }

    public Intent getQuestion()
    {
        Intent i = new Intent("com.example.btshome.ASKPAGE");
        startActivity(i);
        return null;
    }

返回以下错误:

04-23 19:43:30.030: E/AndroidRuntime(845): FATAL EXCEPTION: main
04-23 19:43:30.030: E/AndroidRuntime(845): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.example.btshome.ASKPAGE }
04-23 19:43:30.030: E/AndroidRuntime(845):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1545)
04-23 19:43:30.030: E/AndroidRuntime(845):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1416)
04-23 19:43:30.030: E/AndroidRuntime(845):  at android.app.Activity.startActivityForResult(Activity.java:3351)
04-23 19:43:30.030: E/AndroidRuntime(845):  at android.app.Activity.startActivityForResult(Activity.java:3312)
04-23 19:43:30.030: E/AndroidRuntime(845):  at android.app.Activity.startActivity(Activity.java:3522)
04-23 19:43:30.030: E/AndroidRuntime(845):  at android.app.Activity.startActivity(Activity.java:3490)
04-23 19:43:30.030: E/AndroidRuntime(845):  at com.example.btshome.MainActivity.getQuestion(MainActivity.java:47)
04-23 19:43:30.030: E/AndroidRuntime(845):  at com.example.btshome.MainActivity$1.onClick(MainActivity.java:30)
04-23 19:43:30.030: E/AndroidRuntime(845):  at android.view.View.performClick(View.java:4084)
04-23 19:43:30.030: E/AndroidRuntime(845):  at android.view.View$PerformClick.run(View.java:16966)
04-23 19:43:30.030: E/AndroidRuntime(845):  at android.os.Handler.handleCallback(Handler.java:615)
04-23 19:43:30.030: E/AndroidRuntime(845):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-23 19:43:30.030: E/AndroidRuntime(845):  at android.os.Looper.loop(Looper.java:137)
04-23 19:43:30.030: E/AndroidRuntime(845):  at android.app.ActivityThread.main(ActivityThread.java:4745)
04-23 19:43:30.030: E/AndroidRuntime(845):  at java.lang.reflect.Method.invokeNative(Native Method)
04-23 19:43:30.030: E/AndroidRuntime(845):  at java.lang.reflect.Method.invoke(Method.java:511)
04-23 19:43:30.030: E/AndroidRuntime(845):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-23 19:43:30.030: E/AndroidRuntime(845):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-23 19:43:30.030: E/AndroidRuntime(845):  at dalvik.system.NativeStart.main(Native Method)

清单页面:

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.btshome.MainActivity"
            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=".LoginActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="com.example.btshome.LOGINACTIVITY"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>

        <activity android:name=".SignUp"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="com.example.btshome.SIGNUP"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>

        <activity android:name=".LoginPage"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="com.example.btshome.LOGINPAGE"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>

        <activity
            android:name=".QuestionAsking"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.btshome.ASKPAGE" />

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

    </application>

我在单击时实现此菜单项的方法是否良好?或者这是一个不好的方法,因为我必须在我的所有页面上都提供滑动菜单。请帮忙。

4

2 回答 2

3

您的清单文件中可能没有ASKPAGEActivity 。

在 AndroidManifest.xml 的application标签内,添加:

<activity android:name=".ASKPAGE">

编辑:

您的getQuestion()方法应如下所示:

public Intent getQuestion()
{
    Intent i = new Intent("com.example.btshome.ASKPAGE"); // Set an action in constructor
    i.setClass(MainActivity.this, QuestionAsking.class);  // Set an Activity class
    startActivity(i);
    return null;
}

只是一个说明 - 我希望你看到你总是null以这种方法返回。

于 2013-04-23T14:17:49.203 回答
0

您必须在至少一个将处理您的意图的活动中明确声明一个意图过滤器。有关更多信息,您可以在此处查看。例如,您应该执行以下操作:

 <activity
            android:name="com.example.btshome.LoginActivity">

            <intent-filter>
                <action android:name="com.example.btshome.LOGINACTIVITY" />>

                <category android:name="com.example.btshome />
</activity>
于 2013-04-23T14:26:02.957 回答