从另一个包启动活动

时间:2014-10-06 15:06:42

标签: android android-intent android-activity package manifest

我试图从另一个包含相同项目的包启动活动。问题是我保持geet错误,说它无法找到该类,并询问我是否在清单文件中声明了它。这是项目的清单文件,类的代码和指出给出的异常的日志。两个源包装简单命名为#34; ingegneria"和" unisannioportal"或者只考虑它们" package1"和" package2"。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0" >

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="unisannioportal.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="ingegneria.ElencoAvvisiActivity"
        android:label="@string/title_activity_elenco_avvisi" >
    </activity>
    <activity
        android:name="ingegneria.AvvisoActivity"
        android:label="@string/title_activity_avviso" >
    </activity>
</application>

</manifest>

这是主类的代码:

package unisannioportal;



import com.example.R;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity 
{
private String titolo;
private String[] dipartimenti;

private DrawerLayout menuDrawerLayout;
private ListView elencoMenuListView;
private ActionBarDrawerToggle menuDrawerToggle;

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

    titolo = this.getTitle().toString();
    dipartimenti = getResources().getStringArray(R.array.departments_array);
    menuDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    elencoMenuListView = (ListView) findViewById(R.id.left_drawer);
    elencoMenuListView.setAdapter(new ArrayAdapter<String>(this,
            R.layout.drawer_list_item, dipartimenti));


    // set up the drawer's list view with items and click listener
    elencoMenuListView.setAdapter(new ArrayAdapter<String>(this,
            R.layout.drawer_list_item, dipartimenti));


    elencoMenuListView.setOnItemClickListener(new DrawerItemClickListener());


    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);


    // ActionBarDrawerToggle ties together the the proper interactions
    // between the sliding drawer and the action bar app icon
    menuDrawerToggle = new ActionBarDrawerToggle(
            this,                  /* host Activity */
            menuDrawerLayout,         /* DrawerLayout object */
            R.drawable.ic_drawer,  /* nav drawer image to replace 'Up' caret */
            R.string.drawer_open,  /* "open drawer" description for accessibility */
            R.string.drawer_close  /* "close drawer" description for accessibility */
            ) {
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(titolo);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(titolo);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    };
    menuDrawerLayout.setDrawerListener(menuDrawerToggle);

   if (savedInstanceState == null) 
   {
        selectItem(0);
    }
}




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

@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();
     if (menuDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
    return super.onOptionsItemSelected(item);
}


/* The click listner for ListView in the navigation drawer */
private class DrawerItemClickListener implements ListView.OnItemClickListener 
{
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
    {
        selectItem(position);
    }
}



private void selectItem(int position) 
{

    if(position == 0)
    {
        Intent intent = new Intent(this, ingegneria.ElencoAvvisiActivity.class);
        this.startActivity(intent);
    }
  /*  // update the main content by replacing fragments
    Fragment fragment = new PlanetFragment();
    Bundle args = new Bundle();
    args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
    fragment.setArguments(args);

    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

    // update selected item and title, then close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(mPlanetTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);*/
}

2 个答案:

答案 0 :(得分:1)

你可能想尝试这样的事情:

            Intent intent = new Intent(Intent.ACTION_MAIN);
            PackageManager manager = getApplicationContext().getPackageManager();
            intent = manager.getLaunchIntentForPackage(YourPackageName);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);    
            startActivity();

答案 1 :(得分:0)

包声明没有错误。

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

但错误位于活动声明中。

<activity
    android:name="ingegneria.ElencoAvvisiActivity"
    android:label="@string/title_activity_elenco_avvisi" >
</activity>

因为活动名称前面缺少一个点。应该如下所示:

<activity
    android:name=".ingegneria.ElencoAvvisiActivity"
    android:label="@string/title_activity_elenco_avvisi" >
</activity>

<activity
    android:name="com.example.ingegneria.ElencoAvvisiActivity"
    android:label="@string/title_activity_elenco_avvisi" >
</activity>