Android的活动错误错误、Android

2023-09-12 23:46:05 作者:恋爱恐惧症

我是新手的机器人。在Android中我试图运行一个简单的工作。从我的main.xml(http://pastebin.com/Mhgmpj4w)我有一个按钮(我main.java(http://pastebin.com/b5irLVTc)),它会切换到另一个活动second.xml(HTTP: //pastebin.com/F92VxSEZ)它会显示一些文字(我second.java(http://pastebin.com/MR3L5WXL)),但也有一些错误(http://pastebin.com/KH2wDh7b)。我是新手,请帮我。

的main.xml

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直>

    <按钮
        机器人:ID =@ + ID /按钮1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=按钮/>

< / LinearLayout中>
 

main.java:

 包com.rana.activities;

    进口android.app.Activity;
    进口android.content.Intent;
    进口android.os.Bundle;
    进口android.view.View;
    进口android.view.View.OnClickListener;
    进口android.widget.Button;

    公共类主要扩展活动{
        / **第一次创建活动时调用。 * /
        @覆盖
        公共无效的onCreate(包savedInstanceState){
            super.onCreate(savedInstanceState);
            的setContentView(R.layout.main);

            按钮B =(按钮)findViewById(android.R.id.button1);
            b.setOnClickListener(新OnClickListener(){

                公共无效的onClick(视图v){
                    startActivity(新意图(Main.this,Second.class));
                }
            });
        }
    }
 

second.xml:

 < XML版本=1.0编码=UTF-8&GT?;
    < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:方向=垂直>

        <的TextView
            机器人:ID =@ + ID / textView1
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文本=这是第二次活动/>

    < / LinearLayout中>
 
ubuntu 64位android项目报错的解决方案

second.java:

 包com.rana.activities;

    进口android.app.Activity;
    进口android.os.Bundle;

    公共类第二延伸活动{

        @覆盖
        保护无效的onCreate(包savedInstanceState){
            // TODO自动生成方法存根
            super.onCreate(savedInstanceState);
            的setContentView(R.layout.second);
        }

    }
 

logcat的:

  10-29 19:19:25.377:D / AndroidRuntime(859):关闭虚拟机
    10-29 19:19:25.377:W / dalvikvm(859):主题ID = 1:螺纹退出与未捕获的异常(组= 0x40015560)
    10-29 19:19:25.397:E / AndroidRuntime(859):致命异常:主要
    10-29 19:19:25.397:E / AndroidRuntime(859):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.rana.activities / com.rana.activities.Main}:显示java.lang.NullPointerException
    10-29 19:19:25.397:E / AndroidRuntime(859):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
    10-29 19:19:25.397:E / AndroidRuntime(859):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
    10-29 19:19:25.397:E / AndroidRuntime(859):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
    10-29 19:19:25.397:E / AndroidRuntime(859):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)
    10-29 19:19:25.397:E / AndroidRuntime(859):在android.os.Handler.dispatchMessage(Handler.java:99)
    10-29 19:19:25.397:E / AndroidRuntime(859):在android.os.Looper.loop(Looper.java:123)
    10-29 19:19:25.397:E / AndroidRuntime(859):在android.app.ActivityThread.main(ActivityThread.java:3683)
    10-29 19:19:25.397:E / AndroidRuntime(859):在java.lang.reflect.Method.invokeNative(本机方法)
    10-29 19:19:25.397:E / AndroidRuntime(859):在java.lang.reflect.Method.invoke(Method.java:507)
    10-29 19:19:25.397:E / AndroidRuntime(859):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
    10-29 19:19:25.397:E / AndroidRuntime(859):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    10-29 19:19:25.397:E / AndroidRuntime(859):在dalvik.system.NativeStart.main(本机方法)
    10-29 19:19:25.397:E / AndroidRuntime(859):由:显示java.lang.NullPointerException
    10-29 19:19:25.397:E / AndroidRuntime(859):在com.rana.activities.Main.onCreate(Main.java:18)
    10-29 19:19:25.397:E / AndroidRuntime(859):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    10-29 19:19:25.397:E / AndroidRuntime(859):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
    10-29 19:19:25.397:E / AndroidRuntime(859):11 ...更多
    < ------- LogCat中------------------->
 

解决方案

替换 android.R.id.button1 这与 R.id。按钮1 和导入自己的包研究 main.java 。这将解决这个问题。通过这种方式,将所分配的内存,从您的应用程序,因此它不会搜索全球resouces

I am newbie in android. In android I trying to run a simple work. From my main.xml(http://pastebin.com/Mhgmpj4w) I have one button( my main.java(http://pastebin.com/b5irLVTc) ) it will switch to another activity the second.xml(http://pastebin.com/F92VxSEZ) where it will show some text ( my second.java(http://pastebin.com/MR3L5WXL) ) but there are some errors(http://pastebin.com/KH2wDh7b). I am a newbie please help me.

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>    

main.java:

    package com.rana.activities;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;

    public class Main extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            Button b = (Button) findViewById(android.R.id.button1);
            b.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    startActivity(new Intent(Main.this, Second.class));
                }
            });
        }
    }

second.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="This is the second activity" />

    </LinearLayout>

second.java:

    package com.rana.activities;

    import android.app.Activity;
    import android.os.Bundle;

    public class Second extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.second);
        }

    }

logcat:

    10-29 19:19:25.377: D/AndroidRuntime(859): Shutting down VM
    10-29 19:19:25.377: W/dalvikvm(859): threadid=1: thread exiting with uncaught exception (group=0x40015560)
    10-29 19:19:25.397: E/AndroidRuntime(859): FATAL EXCEPTION: main
    10-29 19:19:25.397: E/AndroidRuntime(859): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rana.activities/com.rana.activities.Main}: java.lang.NullPointerException
    10-29 19:19:25.397: E/AndroidRuntime(859):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
    10-29 19:19:25.397: E/AndroidRuntime(859):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
    10-29 19:19:25.397: E/AndroidRuntime(859):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    10-29 19:19:25.397: E/AndroidRuntime(859):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
    10-29 19:19:25.397: E/AndroidRuntime(859):  at android.os.Handler.dispatchMessage(Handler.java:99)
    10-29 19:19:25.397: E/AndroidRuntime(859):  at android.os.Looper.loop(Looper.java:123)
    10-29 19:19:25.397: E/AndroidRuntime(859):  at android.app.ActivityThread.main(ActivityThread.java:3683)
    10-29 19:19:25.397: E/AndroidRuntime(859):  at java.lang.reflect.Method.invokeNative(Native Method)
    10-29 19:19:25.397: E/AndroidRuntime(859):  at java.lang.reflect.Method.invoke(Method.java:507)
    10-29 19:19:25.397: E/AndroidRuntime(859):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    10-29 19:19:25.397: E/AndroidRuntime(859):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    10-29 19:19:25.397: E/AndroidRuntime(859):  at dalvik.system.NativeStart.main(Native Method)
    10-29 19:19:25.397: E/AndroidRuntime(859): Caused by: java.lang.NullPointerException
    10-29 19:19:25.397: E/AndroidRuntime(859):  at com.rana.activities.Main.onCreate(Main.java:18)
    10-29 19:19:25.397: E/AndroidRuntime(859):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    10-29 19:19:25.397: E/AndroidRuntime(859):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
    10-29 19:19:25.397: E/AndroidRuntime(859):  ... 11 more
    <-------LogCat------------------->

解决方案

Replace android.R.id.button1 this with R.id.button1 and import your own package R class in main.java. It will solve the problem. In this way it will allocated the memory from your application and hence it will not search for the global resouces