我事先在另一个项目中成功添加了一个新的主要活动。但是我试图在不同的项目中使用相同的技术(由于主线程中的工作量而导致问题)。
我想创建一个名为“HomePage”的简单主页,其中的按钮会显示名为“MainActivity”的原始主要活动。 “MainActivity用于连接到一个名为”GetLightData“的servlet。这个项目在添加主页之前在模拟器上运行(有一些错误),但现在它立即崩溃。我真的很感谢我的代码特有的答案,谢谢
首页:
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();
}
}
}
清单:
<?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):发送信号。 PID:878 SIG:9 03-11 18:08:14.882:E / Trace(893):错误打开跟踪文件:没有这样的 文件或目录(2)03-11 18:08:15.192:D / AndroidRuntime(893): 关闭VM 03-11 18:08:15.192:W / dalvikvm(893):threadid = 1: 线程退出与未捕获的异常(组= 0x40a13300)03-11 18:08:15.272:E / AndroidRuntime(893):致命异常:主03-11 18:08:15.272:E / AndroidRuntime(893):java.lang.RuntimeException: 无法实例化活动 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):引起: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更多03-11 18:08:19.122:I / Process(893):发送信号。 PID:893 SIG:9
答案 0 :(得分:3)
变化
<activity
android:name="com.clearlight.HomePage"
android:label="@string/homepage"
android:parentActivityName="com.example.clearlight.MainActivity" >
.... />
到
<activity
android:name="com.example.clearlight.HomePage"
android:label="@string/homepage"
android:parentActivityName="com.example.clearlight.MainActivity" >
.... />
目前,您正在HomePage
AndroidManifest
注册com.clearlight.HomePage
活动,但主要包裹为com.example.clearlight