繁体   English   中英

单击按钮是否无法启动意图?

[英]Button click not launching intent?

每当我按设定的意图单击按钮时,什么也不会发生。 我已经尝试了很多次,但我无法弄清楚我的代码出了什么问题。

按钮:

Button viewMedsButton = (Button)findViewById(R.id.viewMedicationButton);
                viewMedsButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // Intent to open view meds
                        Intent medView = new Intent("android.intent.action.VIEWMEDS");
                        startActivity(medView);
                    }
                });

清单中的意图:

<activity
            android:theme="@style/MainTheme"
            android:name=".viewMeds"
            android:label="@string/app_name"
            android:parentActivityName=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.VIEWMEDS" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

和viewMeds.java:

package ibettergetagoodgradeforthisorillbepissed.sciencefair.beta.mmmeds.com.mmmeds;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;


public class viewMeds extends Activity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_meds);


        String SDRoot = Environment.getExternalStorageDirectory().getPath();
        final File output = getFileStreamPath("output.txt");
        //Get the text file
        File file = new File(SDRoot,"output.txt");

        //Read text from file


        StringBuilder text = new StringBuilder();

        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line;

            while ((line = br.readLine()) != null) {
                text.append(line);
                text.append('\n');
            }
        }
        catch (IOException e) {
            //You'll need to add proper error handling here
        }

        //Find the view by its id
        TextView tv = (TextView)findViewById(R.id.viewMedsTxt);

        //Set the text
        tv.setText(text);



    }



    public void restoreActionBar() {
        ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayShowTitleEnabled(true);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.action_settings:
                Intent prefIntent = new Intent("android.intent.action.PREFS");
                startActivity(prefIntent);
                break;
            case R.id.exit:
                finish();
                break;

        }

        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        public PlaceholderFragment() {
        }
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_main, container, false);
        return rootView;
    }

   /* @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((MainActivity) activity).onSectionAttached(
                getArguments.getInt(ARG_SECTION_NUMBER));
    }*/
}

和viewMeds.xml文件:

<

RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="ibettergetagoodgradeforthisorillbepissed.sciencefair.beta.mmmeds.com.mmmeds.viewMeds">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/viewMedsTxt" />

</RelativeLayout>

非常感谢。

我的错误是我不小心将按钮和意图等添加到了另一个按钮的方法中,而不是onCreate。

确保始终将您的方法放在正确的位置!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM