【问题标题】:java.lang.RuntimeException: Unable to start activity ComponentInfo: Stubjava.lang.RuntimeException:无法启动活动 ComponentInfo:存根
【发布时间】: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


【解决方案1】:

您需要将所有代码放在PlaceholderFragment 类的onCreateView 之前:return rootView;
这是在 android 中编码的新方法。

【讨论】:

    【解决方案2】:

    通常,当您尝试在 IDE 中运行 Android 代码时会出现这种情况。您下载的用于编译的 API 副本实际上并不能正常工作。如果您尝试访问该库中的任何方法,则会引发如下异常:

    Caused by: java.lang.RuntimeException: Stub!

    我看到您似乎正在设备上运行。您是否有意外将存根 Android API 代码与您的应用程序打包在一起? (例如,在 Maven 中,您是否没有将依赖项设置为 provided?)这可能是您的应用正在访问伪造版本的 Android API,而不是设备。

    【讨论】:

      【解决方案3】:

      我稍微修改了您的代码,它现在可以工作了。你没有提供你的布局文件,所以我做了一个,确保使用它。请注意,您的活动中有一些使用片段的部分,我只是删除了它们,因为它们没有被完全使用。我还注释掉了你的菜单代码。

      主要活动

      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" />
      

      结果是:

      【讨论】:

        猜你喜欢
        • 2014-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-10
        • 2014-02-07
        • 2014-01-23
        相关资源
        最近更新 更多