按钮点击不启动意图?

时间:2014-10-21 01:49:01

标签: java android android-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文件:

&LT;

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>

非常感谢。

1 个答案:

答案 0 :(得分:1)

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

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