当前位置: 首页 > 知识库问答 >
问题:

应用程序崩溃,因为我添加了新的主活动

华阳秋
2023-03-14

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

我想创建一个名为“主页”的简单主页,上面有一个按钮,可以弹出名为“主活动”的原始主活动。"主要活动用于连接到名为"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>

Logcat:

03-11 18:08:09.522:I/进程(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):致命异常:main 03-1118:08:15.272:E/AndroidRuntime(893):java。lang.RuntimeException:无法实例化活动组件信息{com.example.clearlight/com.clearlight.HomePage}:java。lang.ClassNotFoundException:com。晴朗的灯光。主页03-11 18:08:15.272:E/AndroidRuntime(893):在android上。应用程序。活动线程。performLaunchActivity(ActivityThread.java:1983)03-11 18:08:15.272:E/AndroidRuntime(893):在android上。应用程序。活动线程。handleLaunchActivity(ActivityThread.java:2084)03-11 18:08:15.272:E/AndroidRuntime(893):在android上。应用程序。活动线程。在android上访问$600(ActivityThread.java:130)03-11 18:08:15.272:E/AndroidRuntime(893)。应用程序。ActivityThread$H.handleMessage(ActivityThread.java:1195)03-11 18:08:15.272:E/AndroidRuntime(893):在android上。操作系统。汉德勒。dispatchMessage(Handler.java:99)03-11 18:08:15.272:E/AndroidRuntime(893):在android上。操作系统。活套。loop(Looper.java:137)03-11 18:08:15.272:E/AndroidRuntime(893):在android上。应用程序。活动线程。main(ActivityThread.java:4745)03-11 18:08:15.272:E/AndroidRuntime(893):在java。朗,反思一下。方法Invokenactive(本机方法)03-11 18:08:15.272:E/AndroidRuntime(893):在java上。朗,反思一下。方法invoke(Method.java:511)03-11 18:08:15.272:E/AndroidRuntime(893):在com上。Android内部的操作系统。ZygoteInit$MethodandArgscaler。运行(ZygoteInit.java:786)03-11 18:08:15.272:E/AndroidRuntime(893):在com上。Android内部的操作系统。合子体。main(zyteinit.java:553)03-11 18:08:15.272:E/AndroidRuntime(893):在dalvik。系统原生艺术。main(原生方法)03-11 18:08:15.272:E/AndroidRuntime(893):由java引起。lang.ClassNotFoundException:com。晴朗的灯光。主页03-11 18:08:15.272:E/AndroidRuntime(893):在dalvik。系统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上。应用程序。仪器。newActivity(Instrumentation.java:1053)03-11 18:08:15.272:E/AndroidRuntime(893):在android上。应用程序。活动线程。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信号:9

共有1个答案

甄佐
2023-03-14

改变

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

目前,你正在AndroidManifest网站上注册HomePage活动com。晴朗的灯光。主页但你的主要软件包是com。实例clearlight

 类似资料:
  • 当我尝试在Android Studio3中添加图像时,应用程序就会崩溃。请帮帮忙。 logcat: activity_main.xml 主要活动.java 当我删除ImageView时,应用程序就可以正常工作了。我已经尝试了一切,调整图像大小,重新安装android Studio。 有人能把问题弄清楚吗?

  • 这是主要活动XML 这是片段活动XML 这是主活动Java文件 这是片段Java文件 我收到的logcat消息说: java.lang.RuntimeException:无法启动活动ComponentInfo{com.csdelta.haroon.fragmentpractice/com.csdelta.haroon.fragmentpractice.MainActivity}:android.v

  • 注意:在我添加广告之前,我的应用程序运行良好,使用相同的方法 当我试图将adview添加到我的应用程序时,它会使我的应用程序崩溃,所以我删除了它,但仍然给我这个这是logcat 10-12 21:33:19.765 4993-4993/com。fm360。almorfis E/AndroidRuntime:致命异常:主进程:com。fm360。阿尔莫菲斯,PID:4993爪哇。lang.Runti

  • 我的menifest权限在代码下面 这就是坠机 由java.lang.SecurityException引起:不允许在没有权限的情况下启动服务意图{act=com.google.android.c2dm.Intent.register pkg=com.google.android.c2dm.android.gms(havs extras)}。com.google.android.c2dm.perm

  • 这就是它崩溃的地方 01-12 13:44:12.571 296 26-29688/ca.dti.grounded.app E/OtherService:AsynchChecker 287:73 java.lang.IllegalStateException:Parcel已完成!在android.os.binderproxy.transactNative(本机方法)在android.os.bind