如何访问在android studio中创建的包?

时间:2015-11-03 12:18:03

标签: android android-studio

我是android的新手并坚持到底。请帮忙!

我在android studio中创建了一个虚拟的Hello world程序(带有项目名称testneha)。我在“com.example.ospune.testneha”文件夹中创建了一个新的包“AppTest”文件夹。在包中,创建了一个名为AppTest1的JAVA类文件。 文件夹结构

com.example.ospune.test
    AppTest(package folder)
       AppTest1(java file)
    MainActivity(file)

在AppTest1文件中,onCreate文件夹我正在调用“apptest布局文件”。

现在在mainActivity文件中,我添加了一个按钮;点击它会调用AppTest1类中提到的startActivity。

public void sendMessage(View view) { Intent intent = new Intent(this, com.example.ospune.test.neha.AppTest.AppTest1.class"); startActivity(intent); }

我的问题是当我点击按钮时,我弹出一个说“不幸的是,测试停止了。”

AndroidManifest.xml也使用AppTest1.class活动

进行了更新

<activity android:name=".AppTest.AppTest1" android:parentActivityName=".MainActivity" </activity>

我无能为力,原因是什么:(

LogCat错误 -

11-03 04:34:43.409 2055-2055/com.example.ospunelap_130.testneha I/art: Not late-enabling -Xcheck:jni (already on)
11-03 04:34:43.410 2055-2055/com.example.ospunelap_130.testneha I/art: Late-enabling JIT
11-03 04:34:43.419 2055-2055/com.example.ospunelap_130.testneha I/art: JIT created with code_cache_capacity=2MB compile_threshold=1000
11-03 04:34:43.507 2055-2055/com.example.ospunelap_130.testneha W/System: ClassLoader referenced unknown path: /data/app/com.example.ospunelap_130.testneha-2/lib/x86
11-03 04:34:43.914 2055-2083/com.example.ospunelap_130.testneha D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
11-03 04:34:43.918 2055-2055/com.example.ospunelap_130.testneha D/: HostConnection::get() New Host Connection established 0xad7f0110, tid 2055
11-03 04:34:44.008 2055-2083/com.example.ospunelap_130.testneha D/: HostConnection::get() New Host Connection established 0xad7f0420, tid 2083
11-03 04:34:44.033 2055-2083/com.example.ospunelap_130.testneha I/OpenGLRenderer: Initialized EGL, version 1.4
11-03 04:34:44.145 2055-2083/com.example.ospunelap_130.testneha W/EGL_emulation: eglSurfaceAttrib not implemented
11-03 04:34:44.145 2055-2083/com.example.ospunelap_130.testneha W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xabebf2c0, error=EGL_SUCCESS
11-03 04:34:45.360 2055-2055/com.example.ospunelap_130.testneha I/Choreographer: Skipped 66 frames!  The application may be doing too much work on its main thread.
11-03 04:35:03.158 2055-2055/com.example.ospunelap_130.testneha D/AndroidRuntime: Shutting down VM
11-03 04:35:03.159 2055-2055/com.example.ospunelap_130.testneha E/AndroidRuntime: FATAL EXCEPTION: main
11-03 04:35:03.159 2055-2055/com.example.ospunelap_130.testneha E/AndroidRuntime: Process: com.example.ospunelap_130.testneha, PID: 2055
11-03 04:35:03.159 2055-2055/com.example.ospunelap_130.testneha E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ospunelap_130.testneha/com.example.ospunelap_130.testneha.AppTest.AppTest1}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
11-03 04:35:03.159 2055-2055/com.example.ospunelap_130.testneha E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
11-03 04:35:03.159 2055-2055/com.example.ospunelap_130.testneha E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
11-03 04:35:03.159 2055-2055/com.example.ospunelap_130.testneha E/AndroidRuntime:     at android.app.ActivityThread.-wrap11(ActivityThread.java)
11-03 04:35:03.159 2055-2055/com.example.ospunelap_130.testneha E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
11-03 04:35:03.159 2055-2055/com.example.ospunelap_130.testneha E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
11-03 04:35:03.159 2055-2055/com.example.ospunelap_130.testneha E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148)
11-03 04:35:03.159 2055-2055/com.example.ospunelap_130.testneha E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417)
11-03 04:35:03.159 2055-2055/com.example.ospunelap_130.testneha E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
11-03 04:35:03.159 2055-2055/com.example.ospunelap_130.testneha E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
11-03 04:35:03.159 2055-2055/com.example.ospunelap_130.testneha E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-03 04:35:03.159 2055-2055/com.example.ospunelap_130.testneha E/AndroidRuntime:  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
11-03 04:35:03.159 2055-2055/com.example.ospunelap_130.testneha E/AndroidRuntime:     at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:100)
11-03 04:35:03.159 2055-2055/com.example.ospunelap_130.testneha E/AndroidRuntime:     at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:93)
11-03 04:35:03.159 2055-2055/com.example.ospunelap_130.testneha E/AndroidRuntime:     at android.support.v7.internal.app.ToolbarActionBar.<init>(ToolbarActionBar.java:78)
11-03 04:35:03.159 2055-2055/com.example.ospunelap_130.testneha E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:206)
11-03 04:35:03.159 2055-2055/com.example.ospunelap_130.testneha E/AndroidRuntime:     at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:99)
11-03 04:35:03.159 2055-2055/com.example.ospunelap_130.testneha E/AndroidRuntime:     at com.example.ospunelap_130.testneha.AppTest.AppTest1.onCreate(AppTest1.java:22)
11-03 04:35:03.159 2055-2055/com.example.ospunelap_130.testneha E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:6237)
11-03 04:35:03.159 2055-2055/com.example.ospunelap_130.testneha E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
11-03 04:35:03.159 2055-2055/com.example.ospunelap_130.testneha E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
11-03 04:35:03.159 2055-2055/com.example.ospunelap_130.testneha E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
11-03 04:35:03.159 2055-2055/com.example.ospunelap_130.testneha E/AndroidRuntime:     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
11-03 04:35:03.159 2055-2055/com.example.ospunelap_130.testneha E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
11-03 04:35:03.159 2055-2055/com.example.ospunelap_130.testneha E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
11-03 04:35:03.159 2055-2055/com.example.ospunelap_130.testneha E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148) 
11-03 04:35:03.159 2055-2055/com.example.ospunelap_130.testneha E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417) 
11-03 04:35:03.159 2055-2055/com.example.ospunelap_130.testneha E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
11-03 04:35:03.159 2055-2055/com.example.ospunelap_130.testneha E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
11-03 04:35:03.159 2055-2055/com.example.ospunelap_130.testneha E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
11-03 04:35:07.335 2055-2055/com.example.ospunelap_130.testneha I/Process: Sending signal. PID: 2055 SIG: 9

1 个答案:

答案 0 :(得分:-1)

编辑: 该错误是由于调用toolbar.getTitle()方法引起的。工具栏对象返回null。试试这个:

Toolbar toolbar = (Toolbar) findviewById(R.id.toolbar);

初始化工具栏。错误在第行。 22根据logcat。