Android电子邮件应用在使用标签时没有来自发送按钮的响应

时间:2013-11-15 15:00:18

标签: java android

public class Tabs extends FragmentActivity implements ActionBar.TabListener {

ViewPager mViewPager;

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

    // Set up the action bar.
    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    // When swiping between different sections, select the corresponding
    // tab. We can also use ActionBar.Tab#select() to do this if we have
    // a reference to the Tab.
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }
    });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        // Create a tab with text corresponding to the page title defined by
        // the adapter. Also specify this Activity object, which implements
        // the TabListener interface, as the callback (listener) for when
        // this tab is selected.
        actionBar.addTab(
                actionBar.newTab()
                        .setText(mSectionsPagerAdapter.getPageTitle(i))
                        .setTabListener(this));
    }
}

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

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, switch to the corresponding page in
    // the ViewPager.
    mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a DummySectionFragment (defined as a static inner class
        // below) with the page number as its lone argument.
        Fragment fragment = new DummySectionFragment();
        Bundle args = new Bundle();
        args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
            case 0:
                return "Calendar";
            case 1:
                return "Check Points";
            case 2:
                return "Information";
        }
        return null;
    }
}

/**
 * A dummy fragment representing a section of the app, but that simply
 * displays dummy text.
 */
public static class DummySectionFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";

    public DummySectionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_tabs_dummy, container, false);
        View calendarView = inflater.inflate(R.layout.calendar_view, container, false);
        View emailView = inflater.inflate(R.layout.email_view, container, false);

        int tabPosition = getArguments().getInt(ARG_SECTION_NUMBER);

        switch(tabPosition)
        {
            case 1:
                WebView mWebView = (WebView) calendarView.findViewById(R.id.webview); 
                mWebView.getSettings().setJavaScriptEnabled(true);
                mWebView.loadUrl("a url");
                return calendarView;

            case 2:
                TextView dummyTextView2 = (TextView) rootView.findViewById(R.id.section_label);
                dummyTextView2.setText("Under Construction");
                return rootView;

            case 3:
                emailView.findViewById(R.id.emailView);
                Class ourClass = Class.forName("com.packagename.Email");
                try
                {
                    Intent ourIntent = new Intent(Tabs.this, ourClass);
                    startActivity(ourIntent);
                }
                catch(ClassNotFoundException e)
                {
                    e.printStackTrace();
                }
                return emailView;
        }
        return null;
    }
}

这是我设置每个标签的地方。

public class Email extends Activity implements View.OnClickListener {
EditText subject, message;
String emailaddress[] = {"myemail@domain.com", "otheremail@other.com"};
String msgSend, subjectSend;
Button sendEmail;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.email_view);
    initializeVars();
    sendEmail.setOnClickListener(this);
}

private void initializeVars() 
{
    // TODO Auto-generated method stub
    subject = (EditText) findViewById(R.id.etSubject);
    message = (EditText) findViewById(R.id.etMessage);
    sendEmail = (Button) findViewById(R.id.bSendEmail);
}

public void onClick(View v) 
{
    // TODO Auto-generated method stub
    msgSend = message.getText().toString();
    subjectSend = subject.getText().toString();

    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, emailaddress);
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subjectSend);
    emailIntent.setType("plain/text");
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, msgSend);
    startActivity(emailIntent);
}


@Override
protected void onPause() 
{
    // TODO Auto-generated method stub
    super.onPause();
    finish();
}

这是处理电子邮件的类。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/emailView" >

<EditText
    android:id="@+id/etSubject"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" 
    android:hint="@string/subject_hint">

</EditText>

<EditText
    android:id="@+id/etMessage"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textMultiLine" 
    android:lines="15"
    android:gravity="top|left"
    android:hint="@string/message_hint"/>

<Button
    android:id="@+id/bSendEmail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="@string/send_email" />

这是我的电子邮件视图

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.packagename"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_shacs_metal"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" 
    >
    <activity
        android:name="com.packagename.Tabs"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.packagename.Tabs" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.packagename.Splash"
        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="com.packagename.Email"
        android:label="@string/app_name" >
    </activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />

我得到的唯一错误是“Intent ourIntent = new Intent(Tabs.this,ourClass);”。 Tabs.this带有下划线,错误显示“在范围内无法访问Tabs类型的封闭实例”。我已经尝试设置案例3没有意图并尝试捕获,但它也不适用。我将在下面发布我尝试的备用案例三。

case 3:
                emailView.findViewById(R.id.emailView);
                return emailView;

感谢您查看我的问题。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

此处无法访问您的标签上下文,请尝试使用getActivity()作为上下文:

case 3:
    emailView.findViewById(R.id.emailView);
    Class ourClass = Class.forName("com.packagename.Email");
    try
    {
        //change this
        //Intent ourIntent = new Intent(Tabs.this, ourClass);
        //to
        Intent ourIntent = new Intent(getActivity(), ourClass);
        getActivity().startActivity(ourIntent);
    }
    catch(ClassNotFoundException e)
    {
         e.printStackTrace();
    }
    return emailView;

检查hereherehere,了解有关上下文的讨论。