java.lang.IllegalStateException:无法在活动的click方法上执行

时间:2015-03-26 12:51:37

标签: android

我对android开发很新。我开始创建一个带有片段的导航抽屉我希望调用导航片段形成不同的不同活动我附上了我的代码供您查看。请建议我解决方案。

And this is my fragment which we r calling

package com.example.lunetta;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class FragmentTwo extends Fragment {

	ImageView ivIcon;
	TextView tvItemName;

	public static final String IMAGE_RESOURCE_ID = "iconResourceID";
	public static final String ITEM_NAME = "itemName";

	public FragmentTwo()
	{
		
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		View view=inflater.inflate(R.layout.fragment_layout_two,container, false);
		
		return view;
	}

}



here is my error
03-26 12:35:23.231: W/dalvikvm(5062): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-26 12:35:23.261: E/AndroidRuntime(5062): FATAL EXCEPTION: main
03-26 12:35:23.261: E/AndroidRuntime(5062): java.lang.IllegalStateException: Could not execute method of the activity
03-26 12:35:23.261: E/AndroidRuntime(5062): 	at android.view.View$1.onClick(View.java:3599)
03-26 12:35:23.261: E/AndroidRuntime(5062): 	at android.view.View.performClick(View.java:4204)
03-26 12:35:23.261: E/AndroidRuntime(5062): 	at android.view.View$PerformClick.run(View.java:17355)
03-26 12:35:23.261: E/AndroidRuntime(5062): 	at android.os.Handler.handleCallback(Handler.java:725)
03-26 12:35:23.261: E/AndroidRuntime(5062): 	at android.os.Handler.dispatchMessage(Handler.java:92)
03-26 12:35:23.261: E/AndroidRuntime(5062): 	at android.os.Looper.loop(Looper.java:137)
03-26 12:35:23.261: E/AndroidRuntime(5062): 	at android.app.ActivityThread.main(ActivityThread.java:5041)
03-26 12:35:23.261: E/AndroidRuntime(5062): 	at java.lang.reflect.Method.invokeNative(Native Method)
03-26 12:35:23.261: E/AndroidRuntime(5062): 	at java.lang.reflect.Method.invoke(Method.java:511)
03-26 12:35:23.261: E/AndroidRuntime(5062): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-26 12:35:23.261: E/AndroidRuntime(5062): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-26 12:35:23.261: E/AndroidRuntime(5062): 	at dalvik.system.NativeStart.main(Native Method)
03-26 12:35:23.261: E/AndroidRuntime(5062): Caused by: java.lang.reflect.InvocationTargetException
03-26 12:35:23.261: E/AndroidRuntime(5062): 	at java.lang.reflect.Method.invokeNative(Native Method)
03-26 12:35:23.261: E/AndroidRuntime(5062): 	at java.lang.reflect.Method.invoke(Method.java:511)
03-26 12:35:23.261: E/AndroidRuntime(5062): 	at android.view.View$1.onClick(View.java:3594)
03-26 12:35:23.261: E/AndroidRuntime(5062): 	... 11 more
03-26 12:35:23.261: E/AndroidRuntime(5062): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.lunetta/com.example.lunetta.FragmentTwo}; have you declared this activity in your AndroidManifest.xml?
03-26 12:35:23.261: E/AndroidRuntime(5062): 	at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1618)
03-26 12:35:23.261: E/AndroidRuntime(5062): 	at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
03-26 12:35:23.261: E/AndroidRuntime(5062): 	at android.app.Activity.startActivityForResult(Activity.java:3370)
03-26 12:35:23.261: E/AndroidRuntime(5062): 	at android.app.Activity.startActivityForResult(Activity.java:3331)
03-26 12:35:23.261: E/AndroidRuntime(5062): 	at android.app.Activity.startActivity(Activity.java:3566)
03-26 12:35:23.261: E/AndroidRuntime(5062): 	at android.app.Activity.startActivity(Activity.java:3534)
03-26 12:35:23.261: E/AndroidRuntime(5062): 	at com.example.lunetta.Second.aboutUsClick(Second.java:41)
03-26 12:35:23.261: E/AndroidRuntime(5062): 	... 14 more
here is my navigation drawer class

package com.example.lunetta;

import java.util.ArrayList;
import java.util.List;

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.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

@SuppressWarnings("deprecation")
public class Welcome extends Activity {
	private DrawerLayout mDrawerLayout;
	private ListView mDrawerList;
	private ActionBarDrawerToggle mDrawerToggle;

	private CharSequence mDrawerTitle;
	private CharSequence mTitle;
	CustomDrawerAdapter adapter;

	List<DrawerItem> dataList;

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

		// Initializing
		dataList = new ArrayList<DrawerItem>();
		mTitle = mDrawerTitle = getTitle();
		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		mDrawerList = (ListView) findViewById(R.id.left_drawer);

		mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
				GravityCompat.START);

		// Add Drawer Item to dataList
		dataList.add(new DrawerItem("Home", R.drawable.ic_action_email));
		dataList.add(new DrawerItem(" About", R.drawable.ic_action_about));
		dataList.add(new DrawerItem("Services", R.drawable.ic_action_gamepad));
		dataList.add(new DrawerItem("Product", R.drawable.ic_action_labels));
		dataList.add(new DrawerItem("Marketing", R.drawable.ic_action_search));
		dataList.add(new DrawerItem("Portfolio", R.drawable.ic_action_cloud));
		dataList.add(new DrawerItem("career", R.drawable.ic_action_camera));
		dataList.add(new DrawerItem("Request", R.drawable.ic_action_video));
		dataList.add(new DrawerItem("Contact", R.drawable.ic_action_group));
		dataList.add(new DrawerItem("Import & Export",
				R.drawable.ic_action_import_export));
		dataList.add(new DrawerItem("Likes", R.drawable.ic_action_good));
		dataList.add(new DrawerItem("Settings", R.drawable.ic_action_settings));
		dataList.add(new DrawerItem("Help", R.drawable.ic_action_help));

		adapter = new CustomDrawerAdapter(this, R.layout.custom_drawer_item,
				dataList);

		mDrawerList.setAdapter(adapter);

		mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

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

		mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
				R.drawable.ic_drawer, R.string.drawer_open,
				R.string.drawer_close) {
			public void onDrawerClosed(View view) {
				getActionBar().setTitle(mTitle);
				invalidateOptionsMenu(); // creates call to
											// onPrepareOptionsMenu()
			}

			public void onDrawerOpened(View drawerView) {
				getActionBar().setTitle(mDrawerTitle);
				invalidateOptionsMenu(); // creates call to
											// onPrepareOptionsMenu()
			}
		};

		mDrawerLayout.setDrawerListener(mDrawerToggle);

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

	}

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

	public void SelectItem(int possition) {

		Fragment fragment = null;
		Bundle args = new Bundle();
		switch (possition) {
		case 0:
			fragment = new FragmentOne();
			args.putString(FragmentOne.ITEM_NAME, dataList.get(possition)
					.getItemName());
			args.putInt(FragmentOne.IMAGE_RESOURCE_ID, dataList.get(possition)
					.getImgResID());
			break;
		case 1:
			fragment = new FragmentTwo();

			break;
		case 2:
			fragment = new FragmentThree();
			args.putString(FragmentThree.ITEM_NAME, dataList.get(possition)
					.getItemName());
			args.putInt(FragmentThree.IMAGE_RESOURCE_ID, dataList
					.get(possition).getImgResID());
			break;
		case 3:
			fragment = new FragmentOne();
			args.putString(FragmentOne.ITEM_NAME, dataList.get(possition)
					.getItemName());
			args.putInt(FragmentOne.IMAGE_RESOURCE_ID, dataList.get(possition)
					.getImgResID());
			break;
		case 4:
			fragment = new FragmentTwo();
			args.putString(FragmentTwo.ITEM_NAME, dataList.get(possition)
					.getItemName());
			args.putInt(FragmentTwo.IMAGE_RESOURCE_ID, dataList.get(possition)
					.getImgResID());
			break;
		case 5:
			fragment = new FragmentThree();
			args.putString(FragmentThree.ITEM_NAME, dataList.get(possition)
					.getItemName());
			args.putInt(FragmentThree.IMAGE_RESOURCE_ID, dataList
					.get(possition).getImgResID());
			break;
		case 6:
			fragment = new FragmentOne();
			args.putString(FragmentOne.ITEM_NAME, dataList.get(possition)
					.getItemName());
			args.putInt(FragmentOne.IMAGE_RESOURCE_ID, dataList.get(possition)
					.getImgResID());
			break;
		case 7:
			fragment = new FragmentTwo();
			args.putString(FragmentTwo.ITEM_NAME, dataList.get(possition)
					.getItemName());
			args.putInt(FragmentTwo.IMAGE_RESOURCE_ID, dataList.get(possition)
					.getImgResID());
			break;
		case 8:
			fragment = new FragmentThree();
			args.putString(FragmentThree.ITEM_NAME, dataList.get(possition)
					.getItemName());
			args.putInt(FragmentThree.IMAGE_RESOURCE_ID, dataList
					.get(possition).getImgResID());
			break;
		case 9:
			fragment = new FragmentOne();
			args.putString(FragmentOne.ITEM_NAME, dataList.get(possition)
					.getItemName());
			args.putInt(FragmentOne.IMAGE_RESOURCE_ID, dataList.get(possition)
					.getImgResID());
			break;
		case 10:
			fragment = new FragmentTwo();
			args.putString(FragmentTwo.ITEM_NAME, dataList.get(possition)
					.getItemName());
			args.putInt(FragmentTwo.IMAGE_RESOURCE_ID, dataList.get(possition)
					.getImgResID());
			break;
		case 11:
			fragment = new FragmentThree();
			args.putString(FragmentThree.ITEM_NAME, dataList.get(possition)
					.getItemName());
			args.putInt(FragmentThree.IMAGE_RESOURCE_ID, dataList
					.get(possition).getImgResID());
			break;
		case 12:
			fragment = new FragmentOne();
			args.putString(FragmentOne.ITEM_NAME, dataList.get(possition)
					.getItemName());
			args.putInt(FragmentOne.IMAGE_RESOURCE_ID, dataList.get(possition)
					.getImgResID());
			break;
		default:
			break;
		}

		fragment.setArguments(args);
		FragmentManager frgManager = getFragmentManager();
		frgManager.beginTransaction().replace(R.id.content_frame, fragment)
				.commit();

		mDrawerList.setItemChecked(possition, true);
		setTitle(dataList.get(possition).getItemName());
		mDrawerLayout.closeDrawer(mDrawerList);

	}

	@Override
	public void setTitle(CharSequence title) {
		mTitle = title;
		getActionBar().setTitle(mTitle);
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		// Sync the toggle state after onRestoreInstanceState has occurred.
		mDrawerToggle.syncState();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		// Pass any configuration change to the drawer toggles
		mDrawerToggle.onConfigurationChanged(newConfig);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// The action bar home/up action should open or close the drawer.
		// ActionBarDrawerToggle will take care of this.
		int id = item.getItemId();
		if (mDrawerToggle.onOptionsItemSelected(item)) {
			return true;
		} else if (id == R.id.action_Home) {
			Intent intent = new Intent(Welcome.this, Second.class);
			startActivity(intent);
		} else if (id == R.id.action_Contact) {
			Intent intent = new Intent(Welcome.this, Contact.class);
			startActivity(intent);
		}

		return false;
	}

	private class DrawerItemClickListener implements
			ListView.OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			SelectItem(position);

		}

	}

}
Main class here my all activity class

package com.example.lunetta;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class Second extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_second);

	}

	public void onClickNext(View view) {
		Intent intent = new Intent(Second.this, MainActivity.class);
		startActivityForResult(intent, 400);
		overridePendingTransition(R.anim.slide_in_top, R.anim.slide_out_bottom);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		overridePendingTransition(R.anim.slide_in_bottom, R.anim.slide_out_top);
	}

	public void btnHome(View view) {
		Intent intent = new Intent(Second.this, Welcome.class);
		startActivity(intent);
	}

	public void aboutUsClick(View view) {

		Fragment fragment = new FragmentTwo();
		Intent intent = new Intent(Second.this, FragmentTwo.class);
		startActivity(intent);
		FragmentManager frgManager = getFragmentManager();
		frgManager.beginTransaction().replace(R.id.content_frame, fragment)
				.commit();

	}

	public void serviceClick(View view) {
		Intent intent = new Intent(Second.this, Services.class);
		startActivity(intent);
	}

	public void productClicked(View view) {
		Intent intent = new Intent(Second.this, Product.class);
		startActivity(intent);
	}

	public void marketingClicked(View view) {
		Intent intent = new Intent(Second.this, Marketing.class);
		startActivity(intent);
	}

	public void portfilioClicked(View view) {
		Intent intent = new Intent(Second.this, Portfolio.class);
		startActivity(intent);
	}

	public void careerlicked(View view) {
		Intent intent = new Intent(Second.this, Career.class);
		startActivity(intent);
	}

	public void requestClicked(View view) {
		Intent intent = new Intent(Second.this, Request.class);
		startActivity(intent);
	}

	public void contactClicked(View view) {
		Intent intent = new Intent(Second.this, Contact.class);
		startActivity(intent);
	}

}

Here is my manifest file

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/lunettaa"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Second"
            android:label="@string/title_activity_second"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen" >
        </activity>
        <activity
            android:name=".Welcome"
            android:icon="@drawable/ic_drawer"
            android:label="@string/title_activity_welcome"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            
            android:name=".Contact"
            android:label="@string/title_activity_contact" >
        </activity>
        <activity
            android:name=".AboutUs"
            android:label="@string/title_activity_about_us" >
        </activity>
        <activity
            android:name=".Services"
            android:label="@string/title_activity_services" >
        </activity>
        <activity
            android:name=".Product"
            android:label="@string/title_activity_product" >
        </activity>
        <activity
            android:name=".Marketing"
            android:label="@string/title_activity_marketing" >
        </activity>
        <activity
            android:name=".Portfolio"
            android:label="@string/title_activity_portfolio" >
        </activity>
        <activity
            android:name=".Career"
            android:label="@string/title_activity_career" >
        </activity>
        <activity
            android:name=".Request"
            android:label="@string/title_activity_request" >
        </activity>
    </application>

</manifest>

3 个答案:

答案 0 :(得分:0)

 Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.lunetta/com.example.lunetta.FragmentTwo}; have you declared this activity in your AndroidManifest.xml?

检查您是否已在清单文件中声明所有活动

答案 1 :(得分:0)

您应该在AndroidManifest.xml中声明所有活动。内部标签应用。像这样:

<activity
        android:name="com.example.gui.SecondActivity"
         android:label="@string/app_name" />

如果活动是您展示的第一个,您应该声明如下:

  <activity
            android:name="com.example.gui.FisrtActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity> 

答案 2 :(得分:0)

从堆栈跟踪:

03-26 12:35:23.261: E/AndroidRuntime(5062):     at com.example.lunetta.Second.aboutUsClick(Second.java:41)

这一行指向:

Fragment fragment = new FragmentTwo();
Intent intent = new Intent(Second.this, FragmentTwo.class);
startActivity(intent);

您无法使用startActivity启动FragmentTwo,因为它是Fragment子类,而不是Activity子类。