Reputation: 51
I've successfully added a new main activity in another project beforehand. However I've tried to use the same techniques in a different project (which was having issues due to the amount of work in the main thread).
I wanted to create a simple home page named "HomePage" with a button that brings up the original main activity named "MainActivity". "MainActivity is used to connect to a servlet called "GetLightData". This project worked (with a few bugs) on the emulator prior to adding the Homepage, but now it crashes instantly. I'd really appreciate answers specific to my code, thanks.
HomePage:
package com.example.clearlight;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class HomePage extends Activity {
private Button ScheduleBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.homepage);
ScheduleBtn = (Button) findViewById(R.id.home_btn);
ScheduleBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(HomePage.this, MainActivity.class);
HomePage.this.startActivity(myIntent);
}
});
}
}
MainActivity
package com.example.clearlight;
import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;
import android.widget.Toast;
import java.net.URL;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import android.os.StrictMode;
import android.util.Log;
public class MainActivity extends Activity {
TextView txt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
setContentView(R.layout.relative);
// Create a crude view - this should really be set via the layout resources but since its an example saves declaring them in the XML.
/*LinearLayout rootLayout = new LinearLayout(getApplicationContext());
txt = new TextView(getApplicationContext());
rootLayout.addView(txt);
setContentView(rootLayout);*/
URL url = null;
DefaultHttpClient httpclient = null;
try {
String registrationUrl = "http://10.0.2.2/SensorInfo/GetLightData?sensor=light";
url = new URL(registrationUrl);
HttpGet getRequest = new HttpGet(registrationUrl);
ResponseHandler<String> handler = new BasicResponseHandler();
httpclient = new DefaultHttpClient();
// request data from server
String result = httpclient.execute(getRequest, handler);
Log.d("MyApp", "Data from server is "+ result);
//Creating TextView Variable**********************************
TextView text1 = (TextView) findViewById(R.id.text);
//Sets the new text to TextView (runtime click event)//*******
text1.setText("Light Data= " + result);
Toast.makeText(this, "Light Data:" + result, Toast.LENGTH_SHORT).show(); //MESSAGE BOX
//txtMessage.setText(String.valueOf(msg1) + " " + String.valueOf(msg2));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.clearlight"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.clearlight.MainActivity"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.clearlight.HomePage"
android:label="@string/homepage"
android:parentActivityName="com.example.clearlight.MainActivity" >
<!-- Move the intent filter to HomePage -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.clearlight.MainActivity" />
</activity>
</application>
</manifest>
Logcat:
03-11 18:08:09.522: I/Process(878): Sending signal. PID: 878 SIG: 9 03-11 18:08:14.882: E/Trace(893): error opening trace file: No such file or directory (2) 03-11 18:08:15.192: D/AndroidRuntime(893): Shutting down VM 03-11 18:08:15.192: W/dalvikvm(893): threadid=1: thread exiting with uncaught exception (group=0x40a13300) 03-11 18:08:15.272: E/AndroidRuntime(893): FATAL EXCEPTION: main 03-11 18:08:15.272: E/AndroidRuntime(893): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.clearlight/com.clearlight.HomePage}: java.lang.ClassNotFoundException: com.clearlight.HomePage 03-11 18:08:15.272: E/AndroidRuntime(893): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983) 03-11 18:08:15.272: E/AndroidRuntime(893): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 03-11 18:08:15.272: E/AndroidRuntime(893): at android.app.ActivityThread.access$600(ActivityThread.java:130) 03-11 18:08:15.272: E/AndroidRuntime(893): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 03-11 18:08:15.272: E/AndroidRuntime(893): at android.os.Handler.dispatchMessage(Handler.java:99) 03-11 18:08:15.272: E/AndroidRuntime(893): at android.os.Looper.loop(Looper.java:137) 03-11 18:08:15.272: E/AndroidRuntime(893): at android.app.ActivityThread.main(ActivityThread.java:4745) 03-11 18:08:15.272: E/AndroidRuntime(893): at java.lang.reflect.Method.invokeNative(Native Method) 03-11 18:08:15.272: E/AndroidRuntime(893): at java.lang.reflect.Method.invoke(Method.java:511) 03-11 18:08:15.272: E/AndroidRuntime(893): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 03-11 18:08:15.272: E/AndroidRuntime(893): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 03-11 18:08:15.272: E/AndroidRuntime(893): at dalvik.system.NativeStart.main(Native Method) 03-11 18:08:15.272: E/AndroidRuntime(893): Caused by: java.lang.ClassNotFoundException: com.clearlight.HomePage 03-11 18:08:15.272: E/AndroidRuntime(893): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61) 03-11 18:08:15.272: E/AndroidRuntime(893): at java.lang.ClassLoader.loadClass(ClassLoader.java:501) 03-11 18:08:15.272: E/AndroidRuntime(893): at java.lang.ClassLoader.loadClass(ClassLoader.java:461) 03-11 18:08:15.272: E/AndroidRuntime(893): at android.app.Instrumentation.newActivity(Instrumentation.java:1053) 03-11 18:08:15.272: E/AndroidRuntime(893): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974) 03-11 18:08:15.272: E/AndroidRuntime(893): ... 11 more 03-11 18:08:19.122: I/Process(893): Sending signal. PID: 893 SIG: 9
Upvotes: 0
Views: 315
Reputation: 132992
change
<activity
android:name="com.clearlight.HomePage"
android:label="@string/homepage"
android:parentActivityName="com.example.clearlight.MainActivity" >
.... />
to
<activity
android:name="com.example.clearlight.HomePage"
android:label="@string/homepage"
android:parentActivityName="com.example.clearlight.MainActivity" >
.... />
currently u are registering HomePage
Activity in AndroidManifest
as com.clearlight.HomePage
but your main package is com.example.clearlight
Upvotes: 3