我是Android编程的新手,我正在尝试为Google眼镜创建一个helloWorld应用程序。我有一点问题:
这是堆栈跟踪:
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'
.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>
和主要活动:
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;
}
}
}
我已经尝试在Android设备上运行它,在模拟器上运行它,所有内容都没有。如果有人能帮助我,我会很高兴。非常感谢提前!
答案 0 :(得分:1)
您需要将所有代码放在onCreateView
课程的PlaceholderFragment
之前:return rootView;
这是在android中编码的新方法。
答案 1 :(得分:1)
通常,当您尝试在IDE中运行Android代码时,会显示此信息。您下载以编译的API副本实际上不起作用。如果您尝试访问该库中的任何方法,则会抛出异常,如:
Caused by: java.lang.RuntimeException: Stub!
我看到你似乎在设备上运行了。你有没有机会在你的应用程序中意外打包存根Android API代码? (例如,在Maven中,你没有将依赖项设置为provided
吗?)可能是你的应用程序正在访问虚假版本的Android API而不是设备。
答案 2 :(得分:0)
我稍微修改了你的代码,现在就可以了。你没有提供你的布局文件所以我做了一个,一定要使用它。请注意,您在活动中使用片段的某些部分,我只是删除它们,因为它们没有被完全使用。我还注释了你的菜单代码。
主要活动
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);
}
}
布局XML (res / layout / 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" />
结果是: