【发布时间】:2014-05-18 08:00:16
【问题描述】:
我对 Android 编程非常陌生,正在尝试为 Google Glasses 创建一个 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;
}
}
}
我试过在安卓设备、模拟器上运行它,一切都对我有用。如果有人可以帮助我,我会非常高兴。非常感谢!
【问题讨论】:
-
您的代码中的卡片是什么。您正在将文本设置为对象。这是这个错误的原因。
-
@Ankit
setText()可以是对象的方法。为什么你认为你不能这样做? -
你能分享一下你的 Card 类是什么......
-
它来自 Google Glass 库\
标签: java android google-glass stub