I'm very new to Android programming and am trying to create a helloWorld application for Google Glasses. I'm having a little problem though:
我对Android编程非常熟悉,并且正在尝试为谷歌眼镜创建helloWorld应用程序。我有一个小问题:
Here's the stack Trace:
堆栈跟踪:
04-06 02:10:49.300: E/AndroidRuntime(724): FATAL EXCEPTION: main
04-06 02:10:49.300: E/AndroidRuntime(724): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloworld/com.example.helloworld.MainActivity}: java.lang.RuntimeException: Stub!
04-06 02:10:49.300: E/AndroidRuntime(724): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
04-06 02:10:49.300: E/AndroidRuntime(724): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
04-06 02:10:49.300: E/AndroidRuntime(724): at android.app.ActivityThread.access$600(ActivityThread.java:123)
04-06 02:10:49.300: E/AndroidRuntime(724): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
04-06 02:10:49.300: E/AndroidRuntime(724): at android.os.Handler.dispatchMessage(Handler.java:99)
04-06 02:10:49.300: E/AndroidRuntime(724): at android.os.Looper.loop(Looper.java:137)
04-06 02:10:49.300: E/AndroidRuntime(724): at android.app.ActivityThread.main(ActivityThread.java:4424)
04-06 02:10:49.300: E/AndroidRuntime(724): at java.lang.reflect.Method.invokeNative(Native Method)
04-06 02:10:49.300: E/AndroidRuntime(724): at java.lang.reflect.Method.invoke(Method.java:511)
04-06 02:10:49.300: E/AndroidRuntime(724): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-06 02:10:49.300: E/AndroidRuntime(724): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-06 02:10:49.300: E/AndroidRuntime(724): at dalvik.system.NativeStart.main(Native Method)
04-06 02:10:49.300: E/AndroidRuntime(724): Caused by: java.lang.RuntimeException: Stub!
04-06 02:10:49.300: E/AndroidRuntime(724): at com.google.android.glass.app.Card.<init>(Card.java:10)
04-06 02:10:49.300: E/AndroidRuntime(724): at com.example.helloworld.MainActivity.onCreate(MainActivity.java:23)
04-06 02:10:49.300: E/AndroidRuntime(724): at android.app.Activity.performCreate(Activity.java:4465)
04-06 02:10:49.300: E/AndroidRuntime(724): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-06 02:10:49.300: E/AndroidRuntime(724): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
04-06 02:10:49.300: E/AndroidRuntime(724): ... 11 more
04-06 02:10:49.520: I/dalvikvm(724): threadid=3: reacting to signal 3
04-06 02:10:49.540: I/dalvikvm(724): Wrote stack traces to '/data/anr/traces.txt'
04-06 02:10:49.840: I/dalvikvm(724): threadid=3: reacting to signal 3
04-06 02:10:49.850: I/dalvikvm(724): Wrote stack traces to '/data/anr/traces.txt'
the .xml:
. xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloworld"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="15" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.helloworld.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.google.android.glass.action.VOICE_TRIGGER" />
</intent-filter>
<meta-data android:name="com.google.android.glass.VoiceTrigger" android:resource="@xml/voice_trigger" />
</activity>
</application>
</manifest>
and the main activity:
主要活动:
package com.example.helloworld;
import com.google.android.glass.app.Card;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello_world);
Card card = new Card(this);
card.setText("Hello world!");
card.setFootnote("androidzeitgeist.com");
setContentView(card.toView());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.hello_world, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_hello_world,
container, false);
return rootView;
}
}
}
I've tried running it on an android device, on an emulator, everything, and nothing had worked for me. I'd be really glad if someone could help me. Many thanks in advance!
我试过在一个android设备上运行它,在模拟器上运行,所有的东西,我都没有工作过。如果有人能帮我,我会很高兴的。提前感谢!
3 个解决方案
#1
0
I modified your code slightly and it works now. You didn't provide your layout file so I made one, make sure to use it. Note you have some parts of using fragments in your activity, I just removed them since they weren't being used fully. I also commented out your menu code.
我稍微修改了你的代码,它现在有效了。你没有提供你的布局文件,所以我做了一个,确保使用它。注意,在您的活动中有一些使用片段的部分,我只是删除它们,因为它们没有被完全使用。我也注释掉了你的菜单代码。
Main Activity
主要活动
package com.example.helloworld;
import com.google.android.glass.app.Card;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello_world);
Card card = new Card(this);
card.setText("Hello world!");
card.setFootnote("androidzeitgeist.com");
setContentView(card.toView());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.hello_world, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Layout XML (res/layout/activity_hello_world.xml)
布局的XML(res /布局/ activity_hello_world.xml)
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.com.glass.cardscrollview.MainActivity"
tools:ignore="MergeRootFrame" />
The result is:
其结果是:
#2
1
You need to put all your code in the onCreateView
of the PlaceholderFragment
class, before: return rootView;
This the new way to code in android.
您需要将所有代码放在PlaceholderFragment类的onCreateView中:返回rootView;这是android代码的新方法。
#3
1
Normally, this shows up when you try to run Android code in your IDE. The copies of the API that you download to compile against are not actually functional. If you try to access any of the methods in that library, it throws an exception like:
通常,当您尝试在IDE中运行Android代码时,会出现这种情况。您下载到编译的API的副本实际上并没有功能。如果您试图访问该库中的任何方法,它会抛出一个异常:
Caused by: java.lang.RuntimeException: Stub!
引起的:. lang。RuntimeException:存根!
I see you appear to be running on the device. By any chance have you accidentally packaged the stub Android API code with your app? (For example, in Maven, did you not set the dependency to provided
?) It may be that your app is accessing the fake version of Android APIs and not the device.
我看到你好像在设备上运行。您是否偶然地将存根Android API代码打包到您的应用程序中?(例如,在Maven中,您没有设置提供的依赖项吗?)你的应用程序可能是在访问假的Android api,而不是设备。
#1
0
I modified your code slightly and it works now. You didn't provide your layout file so I made one, make sure to use it. Note you have some parts of using fragments in your activity, I just removed them since they weren't being used fully. I also commented out your menu code.
我稍微修改了你的代码,它现在有效了。你没有提供你的布局文件,所以我做了一个,确保使用它。注意,在您的活动中有一些使用片段的部分,我只是删除它们,因为它们没有被完全使用。我也注释掉了你的菜单代码。
Main Activity
主要活动
package com.example.helloworld;
import com.google.android.glass.app.Card;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello_world);
Card card = new Card(this);
card.setText("Hello world!");
card.setFootnote("androidzeitgeist.com");
setContentView(card.toView());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.hello_world, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Layout XML (res/layout/activity_hello_world.xml)
布局的XML(res /布局/ activity_hello_world.xml)
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.com.glass.cardscrollview.MainActivity"
tools:ignore="MergeRootFrame" />
The result is:
其结果是:
#2
1
You need to put all your code in the onCreateView
of the PlaceholderFragment
class, before: return rootView;
This the new way to code in android.
您需要将所有代码放在PlaceholderFragment类的onCreateView中:返回rootView;这是android代码的新方法。
#3
1
Normally, this shows up when you try to run Android code in your IDE. The copies of the API that you download to compile against are not actually functional. If you try to access any of the methods in that library, it throws an exception like:
通常,当您尝试在IDE中运行Android代码时,会出现这种情况。您下载到编译的API的副本实际上并没有功能。如果您试图访问该库中的任何方法,它会抛出一个异常:
Caused by: java.lang.RuntimeException: Stub!
引起的:. lang。RuntimeException:存根!
I see you appear to be running on the device. By any chance have you accidentally packaged the stub Android API code with your app? (For example, in Maven, did you not set the dependency to provided
?) It may be that your app is accessing the fake version of Android APIs and not the device.
我看到你好像在设备上运行。您是否偶然地将存根Android API代码打包到您的应用程序中?(例如,在Maven中,您没有设置提供的依赖项吗?)你的应用程序可能是在访问假的Android api,而不是设备。