简体   繁体   中英

app activities crashes

so I'm new and working on android programs to work with activities and stuff anyways im having a problem with the class not found exception. so when i run the app i get the list of all my activities and when i select any of them none of them work, i come to the conclusion its the line with .forName which im sure im using it right .forName("package" + className) which all classes are located in my package and spelling is correct. but there not being located! here is my code all all the items in the test array are my activities

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();
        }
    }
}

so i added the .forName("test.com." + class) and now it just crashes here's what the lifecyle class looks like when i try to select it. iv completely run out of ideas.

    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");
            }





    }
}

this is my manifest

    <?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>

forName("package" + className) you miss dot.

forName("package" + "." + className) this may work

As others have mentioned, missing a dot could be problem for you.

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

If this doesn't fix the problem, can you post an error log?

Try this

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

And make sure you include it in manifest.xml

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM