本文介绍了Android的Java的:通过子类的全局变量;无法启动应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读关于如何做到这一点,试图拼凑从本网站和其他人的帖子一个正常运作的例子例子。我敢肯定,我想的东西不多,但作为一个新手到Java,我可以使用一些帮助。

我只是想创建一个小的例子,从自动化的hello world来源产生在Eclipse中创建一个新项目时。所有我想要做的是能够存储一些全局变量在子类中,然后引用这些值在我的主要活动。不幸的是,每次我试图运行的应用程序,它崩溃不幸的是,GeneralTest1已经停止,并且日志猫错误不是非常有帮助。

快速概要:

  • GlobalVars类扩展应用程序
  • 在清单中,机器人:名字已被添加到引用的其他GlobalVars类
  • 在我的主要活动,我初始化与getApplicationContext()
  • 的全局变量类

下面是我所得到的一切;任何帮助将是非常美联社preciated!

MainActivity.java

 包com.example.generaltest1;

进口android.os.Bundle;
进口android.app.Activity;
进口android.view.Menu;
进口android.widget.TextView;

公共类MainActivity延伸活动{

    GlobalVars myVars =((GlobalVars)getApplicationContext());
    TextView中会将myText =(TextView中)findViewById(R.id.myText);


    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);

        myText.setText(myVars.getMyString());
    }

    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        。getMenuInflater()膨胀(R.menu.activity_main,菜单);
        返回true;
    }
}
 

GlobalVars.java

 包com.example.generaltest1;

进口android.app.Application;

公共类GlobalVars扩展应用{

    字符串MyString的=一些文本;

    公共字符串getMyString(){
        返回MyString的;
    }

    公共字符串setMyString(字符串字符串){
        this.myString =串;
        返回MyString的;
    }
}
 

activity_main.xml

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>

    <的TextView
        机器人:ID =@ + ID /会将myText
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_centerHorizo​​ntal =真
        机器人:layout_centerVertical =真
        机器人:文本=@字符串/参考hello world
        工具:上下文=。MainActivity/>

< / RelativeLayout的>
 

的strings.xml

 <资源>

    <字符串名称=APP_NAME> GeneralTest1< /串>
    <字符串名称=参考hello world>世界,你好<!/串>
    <字符串名称=menu_settings>设置< /串>
    <字符串名称=title_activity_main> MainActivity< /串>

< /资源>
 

GeneralTest1清单

 <舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.example.generaltest1
    安卓版code =1
    机器人:VERSIONNAME =1.0>

    <用途-SDK
        安卓的minSdkVersion =8
        机器人:targetSdkVersion =15/>

    <应用
        机器人:图标=@可绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@风格/ AppTheme机器人:NAME =GlobalVars>
        <活动
            机器人:名称=。MainActivity
            机器人:标签=@字符串/ title_activity_main>
            <意向滤光器>
                <作用机器人:名称=android.intent.action.MAIN/>

                <类机器人:名称=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
    < /用途>

< /舱单>
 

登录猫

  9月8日至14日:53:05.546:E /跟踪(620):错误打开跟踪文件:没有这样的文件或目录(2)
9月8日日至14日:53:05.656:D / AndroidRuntime(620):关闭虚拟机
9月8日日至14日:53:05.656:W / dalvikvm(620):主题ID = 1:螺纹退出与未捕获的异常(组= 0x40a13300)
9月8日日至14日:53:05.676:E / AndroidRuntime(620):致命异常:主要
9月8日日至14日:53:05.676:E / AndroidRuntime(620):java.lang.RuntimeException的:无法实例活动ComponentInfo {com.example.generaltest1 / com.example.generaltest1.MainActivity}:显示java.lang.NullPointerException
9月8日日至14日:53:05.676:E / AndroidRuntime(620):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
9月8日日至14日:53:05.676:E / AndroidRuntime(620):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
9月8日日至14日:53:05.676:E / AndroidRuntime(620):在android.app.ActivityThread.access $ 600(ActivityThread.java:130)
9月8日日至14日:53:05.676:E / AndroidRuntime(620):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1195)
9月8日日至14日:53:05.676:E / AndroidRuntime(620):在android.os.Handler.dispatchMessage(Handler.java:99)
9月8日日至14日:53:05.676:E / AndroidRuntime(620):在android.os.Looper.loop(Looper.java:137)
9月8日日至14日:53:05.676:E / AndroidRuntime(620):在android.app.ActivityThread.main(ActivityThread.java:4745)
9月8日日至14日:53:05.676:E / AndroidRuntime(620):在java.lang.reflect.Method.invokeNative(本机方法)
9月8日日至14日:53:05.676:E / AndroidRuntime(620):在java.lang.reflect.Method.invoke(Method.java:511)
9月8日日至14日:53:05.676:E / AndroidRuntime(620):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786)
9月8日日至14日:53:05.676:E / AndroidRuntime(620):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
9月8日日至14日:53:05.676:E / AndroidRuntime(620):在dalvik.system.NativeStart.main(本机方法)
9月8日日至14日:53:05.676:E / AndroidRuntime(620):由:显示java.lang.NullPointerException
9月8日日至14日:53:05.676:E / AndroidRuntime(620):在android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
9月8日日至14日:53:05.676:E / AndroidRuntime(620):在com.example.generaltest1.MainActivity< INIT>(MainActivity.java:10)
9月8日日至14日:53:05.676:E / AndroidRuntime(620):在java.lang.Class.newInstanceImpl(本机方法)
9月8日日至14日:53:05.676:E / AndroidRuntime(620):在java.lang.Class.newInstance(Class.java:1319)
9月8日日至14日:53:05.676:E / AndroidRuntime(620):在android.app.Instrumentation.newActivity(Instrumentation.java:1053)
9月8日日至14日:53:05.676:E / AndroidRuntime(620):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
9月8日日至14日:53:05.676:E / AndroidRuntime(620):11 ...更多
 

解决方案

请查看这篇文章有关的活动和它们的生命周期:的

您的活动应该是这样的:

 公共类MainActivity延伸活动{

    GlobalVars myVars;
    TextView中会将myText;


    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        myVars =((GlobalVars)getApplication());
        =会将myText(TextView中)findViewById(R.id.myText);
        myText.setText(myVars.getMyString());
    }

    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        。getMenuInflater()膨胀(R.menu.activity_main,菜单);
        返回true;
    }
}
 

I've been reading up examples on how to do this, trying to piece together a functioning example from posts on this site and others. I'm sure I'm missing something small, but as a novice to Java, I could use some assistance.

I'm merely trying to create a small example, derived from the automated hello world generated when creating a new project in Eclipse. All I want to do is be able to store a few global variables in a subclass, then reference those values in my main activity. Unfortunately, every time I try to run the app, it crashes "Unfortunately, GeneralTest1 has stopped", and the log cat error is not very helpful.

Quick overview:

  • GlobalVars class extends Application
  • In the manifest, android:name has been added to reference the additional GlobalVars class
  • Within my main activity, I'm initializing the global vars class with getApplicationContext()

Here is everything I've got; any help would be much appreciated!

MainActivity.java

package com.example.generaltest1;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

    GlobalVars myVars = ((GlobalVars)getApplicationContext());      
    TextView myText = (TextView) findViewById(R.id.myText);


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myText.setText(myVars.getMyString());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

GlobalVars.java

package com.example.generaltest1;

import android.app.Application;

public class GlobalVars extends Application {

    String myString = "Some Text";

    public String getMyString() {
        return myString;
    }

    public String setMyString(String string) {
        this.myString = string;
        return myString;
    }
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/myText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />

</RelativeLayout>

strings.xml

<resources>

    <string name="app_name">GeneralTest1</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>

</resources>

GeneralTest1 Manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.generaltest1"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" android:name="GlobalVars">
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Log Cat

08-14 09:53:05.546: E/Trace(620): error opening trace file: No such file or directory (2)
08-14 09:53:05.656: D/AndroidRuntime(620): Shutting down VM
08-14 09:53:05.656: W/dalvikvm(620): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
08-14 09:53:05.676: E/AndroidRuntime(620): FATAL EXCEPTION: main
08-14 09:53:05.676: E/AndroidRuntime(620): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.generaltest1/com.example.generaltest1.MainActivity}: java.lang.NullPointerException
08-14 09:53:05.676: E/AndroidRuntime(620):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
08-14 09:53:05.676: E/AndroidRuntime(620):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-14 09:53:05.676: E/AndroidRuntime(620):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-14 09:53:05.676: E/AndroidRuntime(620):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-14 09:53:05.676: E/AndroidRuntime(620):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-14 09:53:05.676: E/AndroidRuntime(620):  at android.os.Looper.loop(Looper.java:137)
08-14 09:53:05.676: E/AndroidRuntime(620):  at android.app.ActivityThread.main(ActivityThread.java:4745)
08-14 09:53:05.676: E/AndroidRuntime(620):  at java.lang.reflect.Method.invokeNative(Native Method)
08-14 09:53:05.676: E/AndroidRuntime(620):  at java.lang.reflect.Method.invoke(Method.java:511)
08-14 09:53:05.676: E/AndroidRuntime(620):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-14 09:53:05.676: E/AndroidRuntime(620):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-14 09:53:05.676: E/AndroidRuntime(620):  at dalvik.system.NativeStart.main(Native Method)
08-14 09:53:05.676: E/AndroidRuntime(620): Caused by: java.lang.NullPointerException
08-14 09:53:05.676: E/AndroidRuntime(620):  at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
08-14 09:53:05.676: E/AndroidRuntime(620):  at com.example.generaltest1.MainActivity.<init>(MainActivity.java:10)
08-14 09:53:05.676: E/AndroidRuntime(620):  at java.lang.Class.newInstanceImpl(Native Method)
08-14 09:53:05.676: E/AndroidRuntime(620):  at java.lang.Class.newInstance(Class.java:1319)
08-14 09:53:05.676: E/AndroidRuntime(620):  at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
08-14 09:53:05.676: E/AndroidRuntime(620):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
08-14 09:53:05.676: E/AndroidRuntime(620):  ... 11 more
解决方案

Please check this article about activities and their lifecycle : http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

Your activity should look like this:

public class MainActivity extends Activity {

    GlobalVars myVars;      
    TextView myText;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myVars = ((GlobalVars)getApplication());
        myText = (TextView) findViewById(R.id.myText);
        myText.setText(myVars.getMyString());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

这篇关于Android的Java的:通过子类的全局变量;无法启动应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 06:23