应用程序活动崩溃

时间:2014-01-27 00:02:19

标签: java android classnotfoundexception

所以我是新手并且正在研究android程序来处理活动和东西,无论如何我遇到了类未找到异常的问题。因此,当我运行应用程序时,我得到所有活动的列表,当我选择它们中的任何一个都没有工作时,我得出结论它与.forName的行,我确定我正确使用它.forName(“包” + className)所有类都位于我的包中并且拼写正确。但没有找到! 这是我的代码,测试数组中的所有项目都是我的活动

public class MainActivity extends ListActivity {
    String tests [] = {"LifeCycleTest","SingleTouchTest","MultiTouchTest","KeyTest","AccelerometerTest","AssetsTest",
           "ExternalStorageTest","SoundPoolTest","MediaPlayerTest","FullScreenTest","RenderViewTest","ShapeTest","BitmapTest",
            "FontTest","SurfaceViewTest"};

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,tests));
    }

    @Override
    protected void onListItemClick(ListView list, View view, int position, long id){
        super.onListItemClick(list,view,position, id);
        String testName = tests[position];


// my problem
        try {
            Class clazz = Class
                    .forName("test.com." + testName);

            Intent intent = new Intent(this, clazz);
            startActivity(intent);

        }catch (ClassNotFoundException e){

            e.printStackTrace();
        }
    }
}

所以我添加了.forName(“test.com。”+ class),现在它只是崩溃了,当我尝试选择它时,lifecyle类看起来像什么。 iv完全没有想法。

    public class LifeCycleTest extends Activity{
        StringBuilder builder = new StringBuilder();
        TextView textView;

        private void log(String text){
            Log.d("LifeCycleTest",text);
            builder.append(text);
            builder.append('\n');
            textView.setText(builder.toString());

        }


        @Override
        public void onCreate(Bundle savedInstanceState){
           super.onCreate(savedInstanceState);
           textView = new TextView(this);
           textView.setText(builder.toString());
           setContentView(textView);
           log("created");
        }

        @Override
        protected void onResume(){
            super.onResume();
            log("resumed");
        }  

        @Override
        protected void onPause(){
            super.onPause();
            log("Paused");

            if(isFinishing()){
                log("finishing");
            }





    }
}

这是我的清单

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="fox.com"
          android:versionCode="1"
          android:versionName="1.0"
          android:installLocation="preferExternal">
    <application android:label="@string/app_name" 
                 android:icon="@drawable/mrnom"
                 android:debuggable="true">
        <activity android:name=".MainActivity"
                  android:label="@string/app_name"         
                  android:configChanges="keyboard|keyboardHidden|orientation">
            <activity android:label="Life Cycle Test"
                      android:name=".LifeCycleTest"
                      android:configChanges="keyboard|keyboardHidden|orientation"></activity>

                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>

            </activity>
    </application>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-sdk android:minSdkVersion="3" android:targetSdkVersion="16"/>
</manifest>

3 个答案:

答案 0 :(得分:0)

forName("package" + className)你错过了。

forName("package" + "." + className)这可能有效

答案 1 :(得分:0)

正如其他人所说,错过一个点可能对你有用。

Class clazz = Class
                    .forName("test.com." + testName);

如果这不能解决问题,您可以发布错误日志吗?

答案 2 :(得分:0)

试试这个

 Intent intent = new Intent(this, <classname>.class);
 startActivity(intent);

并确保将其包含在 manifest.xml

 <activity
        android:name="<com.packagename.classname>"   android:uiOptions="splitActionBarWhenNarrow">
  </activity>