我已經事先在另一個項目中成功添加了新的主要活動。然而,我試圖在不同的項目中使用相同的技術(由於主線程中的工作量有問題)。自從我添加了新的主要活動後,應用程序崩潰
我想創建一個名爲「HomePage」的簡單主頁,並帶有一個按鈕,該按鈕顯示名爲「MainActivity」的原始主活動。 「MainActivity用於連接到一個名爲」GetLightData「的servlet,該項目在添加主頁之前在模擬器上工作(但有一些bug),但現在它立即崩潰,我非常感謝我的代碼特有的答案,謝謝。
首頁:
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): 關閉虛擬機03-11 18:08:15.192:W/dalvikvm(893):threadid = 1: 線程正在以未捕獲的異常退出(group = 0x40a13300)03-11 18:08:15.272:E/AndroidRuntime(893):致命異常:main 03-11 18:08:15.272:E/AndroidRuntime(893):java.lang.RuntimeException: 無法實例化活動 ComponentInfo {com.example.clearlight/com.clearlight。主頁}: java.lang.ClassNotFoundException:com.clearlight.HomePage 03-11 18:08:15.272:E/AndroidRuntime(893):at android.app.ActivityThread.performLaunchA (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):在 android.os.Looper.loop(Looper.java:137)03-11 18:08:15.272: E/AndroidRuntime(893):at android。 (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.ClassNotFoundEx引起ception: 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):在 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):發送信號。 PID:893 SIG:9
你可以添加您的logcat與錯誤消息(S)? – ianhanniballake 2013-03-11 18:05:34
還使用Asynctask連接您的服務器並檢索數據。這樣你可以擺脫內存錯誤。 – Raghunandan 2013-03-11 18:07:56
我已經添加了Logcat – Nick 2013-03-11 18:10:12