Android:在新意图中使用radiobutton

时间:2018-03-22 16:05:45

标签: android android-intent radio

我自学,希望能成为一名优秀的开发人员。我目前正在编写一个程序,我可以使用菜单根据自己的知识更改背景颜色。一旦这个工作,我将决定这是否应该是我的实际程序的一部分。我当前在我的MainActivity中创建了一个工具栏。我在工具栏中写了一个意图来启动第二个活动。我现在希望创建一个RadioButton,它允许我改变背景的颜色,就像开发人员允许我们自定义从Google Play商店获得的程序设置一样。我不确定在哪里插入我的代码,它会崩溃我是否尝试插入子菜单或java文件。有没有这方面的教程?我的代码如下所示,以显示我尝试过的内容。我的logcat也在那里,但我不知道如何阅读它或搜索什么,因为我还是新的。

public class SecondActivity extends AppCompatActivity {

    RadioButton radio_cyan;
    View root;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement

        if (id == R.id.change_colour) {
            Intent intent = new Intent(this,MainActivity.class);
            startActivity(intent);
            root.setBackgroundColor(Color.CYAN);
            return true;
        }

        if (id == R.id.action_search) {
            Toast.makeText(this, "Android Menu is Clicked", Toast.LENGTH_LONG).show();
            return true;
        }

        if (id == R.id.action_settings) {
            Toast.makeText(this, "Android Menu is Clicked", Toast.LENGTH_LONG).show();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}


    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        tools:context=".MainActivity">

        <item
            android:id="@+id/action_search"
            android:orderInCategory="100"
            android:title="Search"
            app:actionViewClass="android.support.v7.widget.SearchView"
            app:showAsAction="ifRoom"
            android:icon="@android:drawable/ic_menu_search"/>
        <item
            android:id="@+id/action_settings"
            android:title="Setting"
            app:showAsAction="never" />
        <item
            android:id="@+id/change_colour"
            android:title="Change colour"
            app:showAsAction="never" />

    </menu>

03-23 13:33:30.117 2220-2220 / com.example.menuitem_toolbar_test I / dalvikvm-heap:将堆(frag case)增长到3.782MB,用于635808字节分配 03-23 13:33:30.317 2220-2226 / com.example.menuitem_toolbar_test D / dalvikvm:GC_FOR_ALLOC释放105K,9%免费3715K / 4064K,暂停196ms,总计196ms 03-23 13:33:31.517 2220-2220 / com.example.menuitem_toolbar_test I / Choreographer:跳过204帧!应用程序可能在其主线程上做了太多工作。 03-23 13:33:32.917 2220-2220 / com.example.menuitem_toolbar_test D / EGL_emulation:eglMakeCurrent:0xb914fef0:ver 2 0 03-23 13:33:33.847 2220-2220 / com.example.menuitem_toolbar_test I / Choreographer:跳过123帧!应用程序可能在其主线程上做了太多工作。 03-23 13:33:34.997 2220-2220 / com.example.menuitem_toolbar_test D / EGL_emulation:eglMakeCurrent:0xb914fef0:ver 2 0 03-23 13:33:35.307 2220-2220 / com.example.menuitem_toolbar_test I / Choreographer:跳过32帧!应用程序可能在其主线程上做了太多工作。 03-23 13:33:35.427 2220-2220 / com.example.menuitem_toolbar_test D / EGL_emulation:eglMakeCurrent:0xb914fef0:ver 2 0 03-23 13:33:35.687 2220-2220 / com.example.menuitem_toolbar_test D / EGL_emulation:eglMakeCurrent:0xb914fef0:ver 2 0 03-23 13:33:35.937 2220-2220 / com.example.menuitem_toolbar_test D / EGL_emulation:eglMakeCurrent:0xb914fef0:ver 2 0 03-23 13:33:41.077 2220-2220 / com.example.menuitem_toolbar_test W / InputEventReceiver:尝试完成输入事件但输入事件接收器已被处理掉。 03-23 13:33:41.217 2220-2220 / com.example.menuitem_toolbar_test I / Choreographer:跳过44帧!应用程序可能在其主线程上做了太多工作。 03-23 13:33:43.537 2220-2220 / com.example.menuitem_toolbar_test D / EGL_emulation:eglMakeCurrent:0xb914fef0:ver 2 0 03-23 13:33:44.857 2220-2220 / com.example.menuitem_toolbar_test D / EGL_emulation:eglMakeCurrent:0xb914fef0:ver 2 0 03-23 13:33:47.157 2220-2220 / com.example.menuitem_toolbar_test D / EGL_emulation:eglMakeCurrent:0xb914fef0:ver 2 0 03-23 13:33:47.427 2220-2220 / com.example.menuitem_toolbar_test D / AndroidRuntime:关闭VM 03-23 13:33:47.457 2220-2220 / com.example.menuitem_toolbar_test W / dalvikvm:threadid = 1:线程退出,未捕获异常(组= 0xaf865b20) 03-23 13:33:47.487 2220-2220 / com.example.menuitem_toolbar_test E / AndroidRuntime:FATAL EXCEPTION:main                                                                                  处理:com.example.menuitem_toolbar_test,PID:2220                                                                                  java.lang.IllegalStateException:无法在父级或祖先语句中找到方法onClick(View)for android:onClick属性在视图类android.support.v7.widget.AppCompatRadioButton上定义,ID为&#39; radio_cyan&#39;                                                                                      在android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)                                                                                      在android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)                                                                                      在android.view.View.performClick(View.java:4438)                                                                                      在android.widget.CompoundButton.performClick(CompoundButton.java:100)                                                                                      在android.view.View $ PerformClick.run(View.java:18422)                                                                                      在android.os.Handler.handleCallback(Handler.java:733)                                                                                      在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                      在android.os.Looper.loop(Looper.java:136)                                                                                      在android.app.ActivityThread.main(ActivityThread.java:5017)                                                                                      at java.lang.reflect.Method.invokeNative(Native Method)                                                                                      在java.lang.reflect.Method.invoke(Method.java:515)                                                                                      在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779)                                                                                      在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)                                                                                      at dalvik.system.NativeStart.main(Native Method) 03-23 13:33:55.907 2220-2220 /? I /处理:发送信号。 PID:2220 SIG:9

0 个答案:

没有答案