0

我已经成功地预先在另一个项目中添加了一个新的主要活动。但是我尝试在不同的项目中使用相同的技术(由于主线程中的工作量而出现问题)。

我想用一个按钮创建一个名为“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);


            }
        });
    }   



}

主要活动

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): 打开跟踪文件时出错: 没有这样的文件或目录 (2) 03-11 18:08:15.192: D/AndroidRuntime(893) : 关闭 VM 03-11 18:08:15.192: W/dalvikvm(893): threadid=1: 线程退出未捕获异常 (group=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): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983) 03-11 18:08:15.272: E/AndroidRuntime( 893):在 android.app.ActivityThread。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:9

4

1 回答 1

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活动,AndroidManifestcom.clearlight.HomePage你的主包是com.example.clearlight

于 2013-03-11T18:06:35.197 回答