2013-03-11 2 views
0

다른 프로젝트에 새로운 주요 활동을 성공적으로 추가했습니다. 그러나 나는 다른 프로젝트에서 동일한 기술을 사용하려고 시도했다. (메인 쓰레드의 작업량 때문에 문제가 발생했다.)새로운 기본 활동을 추가 한 이후 앱이 다운 됨

"MainActivity"라는 원래 기본 활동을 불러오는 버튼이있는 "HomePage"라는 간단한 홈페이지를 만들고 싶습니다. "MainActivity는"GetLightData "라는 서블릿에 연결하는 데 사용됩니다.이 프로젝트는 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(); 
     } 

     } 
    } 

매니페스트 :

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

로그인 :

03-11 18 : 08 : 09.522 : I/Process (878) : 보내는 신호. PID : 878 SIG : 9 03-11 18 : 08 : 14.882 : E/Trace (893) : 오류 열림 추적 파일 : 아니요 파일 또는 디렉터리 (0) 03-11 18 : 08 : 15.192 : D/AndroidRuntime (group = 0x40a13300) 03-11 18 : 08 : 15.272 : E/dalvikvm (893) : threadid = 1 : VM 종료 03-11 18 : 08 : 15.192 : AndroidRuntime (893) : 치명적인 예외 : main 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) : android.app.ActivityThread.performLaunchA ctivity (ActivityThread.java:1983) 03-11 18 : 08 : 15.272 : E/AndroidRuntime (893) : android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2084) 03-11 18 : 08 : 15.272 : E/AndroidRuntime (893) : android.app.ActivityThread.access $ 600 (ActivityThread.java:130) 03-11 18 : 08 : 15.272 : E/AndroidRuntime (893) : android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1195) 03-11 18 : 08 : 15.272 : E/AndroidRuntime (893) : 에 있습니다. 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) : 에서 android. app.ActivityThread.main (ActivityThread.java:4745) 03-11 18 : 08 : 15.272 : E/AndroidRuntime (893) : 에서 java.lang.reflect.Method.invokeNative (기본 메소드) 03-11 18 : 08 : 15.272 : E/AndroidRuntime (893) : java.lang.reflect.Method.invoke (Method.java:511) 03-11 18 : 08 : 15.272 : E/AndroidRuntime (893) : com. android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:786) 03-11 18 : 08 : 15.272 : E/AndroidRuntime (893) : 에서 com.android.internal.os.ZygoteInit.main (ZygoteInit .java : 553) 03-11 18 : 08 : 15.272 : E/AndroidRuntime (893) : 에서 dalvik.system.NativeStart.main (기본 메소드) 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) : dalvik.system.BaseDexClassLoader.findClass (BaseDexClassLoader.java:61) 03-11 18 : 08 : 15.272 : E/AndroidRuntime (893) : 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) : android.app.Instrumentation.newActivity (Instrumentation.java:1053) 03-11 18 : 08 : 15.272 : E/AndroidRuntime (893) : 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 : 유 com.clearlight.HomePageAndroidManifest에서 HomePage 활동을 등록하는

<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" > 
.... /> 

에 9

+1

당신이 추가 할 수있다 오류 메시지가 표시되는 logcat? – ianhanniballake

+0

또한 Asynctask를 사용하여 서버를 연결하고 데이터를 검색하십시오. 그렇게하면 메모리 오류를 제거 할 수 있습니다. – Raghunandan

+0

Logcat을 추가했습니다 – Nick

답변

3

변화하지만 기본 패키지는 com.example.clearlight

+0

대단히 감사합니다! 나는 그것을 깨닫지 못했거나 그런 나쁜 효과를 낼 수있는 것을 깨닫지 못했다. 지금은 효과가있다. 다시 한번 감사드립니다. – Nick

+1

@ 닉이 문제를 해결 한 경우 답변을 수락하십시오. – nhaarman

+1

+1 평소와 같이 빠르고 빠르다 :-) –