简体   繁体   English

Android应用内结算应用无法运行

[英]Android In-App Billing app won't run

I'm attempting to build a simple Android app using In-App Billing. 我正在尝试使用应用内结算来构建一个简单的Android应用。 I'm using a Nexus 7 2013 device to run and debug it. 我正在使用Nexus 7 2013设备运行和调试它。 I just get the error message, "Unfortunately, your app has stopped." 我只是收到错误消息,“不幸的是,您的应用已停止。” I've tried logging and inserting breakpoints, but it never even seems to get to the beginning of the code. 我曾尝试记录和插入断点,但它似乎从未到达代码的开头。 I'm using this tutorial and this one , and have only started working with them. 我使用本教程这一个 ,并已开始只与他们合作。 My code: 我的代码:

package com.myknitcards.project;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.myknitcards.project.util.IabHelper;
import com.myknitcards.project.util.IabResult;
import com.myknitcards.project.util.Inventory;
import com.myknitcards.project.util.Purchase;


public class MainActivity extends Activity {
    private Button clickButton = (Button)findViewById(R.id.clickButton);
    private Button buyButton = (Button)findViewById(R.id.buyButton);

    private static final String TAG = "com.myknitcards.project";
    IabHelper mHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main); 

        clickButton.setEnabled(false);

        String base64EncodedPublicKey = 
                "mykeyhere";

        mHelper = new IabHelper(this, base64EncodedPublicKey);

        mHelper.startSetup(new 
                IabHelper.OnIabSetupFinishedListener() {
                     public void onIabSetupFinished(IabResult result) 
                 {
                           if (!result.isSuccess()) {
                             Log.d(TAG, "In-app Billing setup failed: " + 
                        result);
                           } else {             
                             Log.d(TAG, "In-app Billing is set up OK");
                       }
                         }
                });
    }

    public void buttonClicked(View view) {
        clickButton.setEnabled(false);
        buyButton.setEnabled(true);
    }
}

My LogCat reads: 我的LogCat读取:

10-14 17:30:07.458: D/AndroidRuntime(10207): Shutting down VM 10-14 17:30:07.458: W/dalvikvm(10207): threadid=1: thread exiting with uncaught exception (group=0x41be2ba8) 10-14 17:30:07.458: E/AndroidRuntime(10207): FATAL EXCEPTION: main 10-14 17:30:07.458: E/AndroidRuntime(10207): Process: com.myknitcards.project, PID: 10207 10-14 17:30:07.458: E/AndroidRuntime(10207): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.myknitcards.project/com.myknitcards.project.MainActivity}: java.lang.NullPointerException 10-14 17:30:07.458: E/AndroidRuntime(10207): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110) 10-14 17:30:07.458: E/AndroidRuntime(10207): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 10-14 17:30:07.458: E/AndroidRuntime(10207): at android.app.ActivityThread.access$800(ActivityThread.java:135) 10-14 17:30:07.458: E/AndroidRuntime(10207): at android.app.ActivityThread$H.handleMessage(ActivityThrea 10-14 17:30:07.458:D / AndroidRuntime(10207):关闭VM 10-14 17:30:07.458:W / dalvikvm(10207):threadid = 1:线程退出且未捕获到异常(group = 0x41be2ba8)10 -14 17:30:07.458:E / AndroidRuntime(10207):致命异常:主10-14 17:30:07.458:E / AndroidRuntime(10207):进程:com.myknitcards.project,PID:10207 10-14 17 :30:07.458:E / AndroidRuntime(10207):java.lang.RuntimeException:无法实例化活动ComponentInfo {com.myknitcards.project / com.myknitcards.project.MainActivity}:java.lang.NullPointerException 10-14 17:30 :07.458:E / AndroidRuntime(10207):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)10-14 17:30:07.458:E / AndroidRuntime(10207):在android.app.ActivityThread.handleLaunchActivity( ActivityThread.java:2233)10-14 17:30:07.458:E / AndroidRuntime(10207):在android.app.ActivityThread.access $ 800(ActivityThread.java:135)10-14 17:30:07.458:E / AndroidRuntime (10207):位于android.app.ActivityThread $ H.handleMessage(ActivityThrea d.java:1196) 10-14 17:30:07.458: E/AndroidRuntime(10207): at android.os.Handler.dispatchMessage(Handler.java:102) 10-14 17:30:07.458: E/AndroidRuntime(10207): at android.os.Looper.loop(Looper.java:136) 10-14 17:30:07.458: E/AndroidRuntime(10207): at android.app.ActivityThread.main(ActivityThread.java:5001) 10-14 17:30:07.458: E/AndroidRuntime(10207): at java.lang.reflect.Method.invokeNative(Native Method) 10-14 17:30:07.458: E/AndroidRuntime(10207): at java.lang.reflect.Method.invoke(Method.java:515) 10-14 17:30:07.458: E/AndroidRuntime(10207): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 10-14 17:30:07.458: E/AndroidRuntime(10207): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 10-14 17:30:07.458: E/AndroidRuntime(10207): at dalvik.system.NativeStart.main(Native Method) 10-14 17:30:07.458: E/AndroidRuntime(10207): Caused by: java.lang.NullPointerException 10-14 17:30:07.458: E/AndroidRuntime(10207): at android.app.Activity.findVie d.java:1196)10-14 17:30:07.458:E / AndroidRuntime(10207):在android.os.Handler.dispatchMessage(Handler.java:102)10-14 17:30:07.458:E / AndroidRuntime( 10207):位于android.os.Looper.loop(Looper.java:136)10-14 17:30:07.458:E / AndroidRuntime(10207):位于android.app.ActivityThread.main(ActivityThread.java:5001)10 -14 17:30:07.458:E / AndroidRuntime(10207):at java.lang.reflect.Method.invokeNative(Native Method)10-14 17:30:07.458:E / AndroidRuntime(10207):at java.lang。反射.Method.invoke(Method.java:515)10-14 17:30:07.458:E / AndroidRuntime(10207):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785)10 -14 17:30:07.458:E / AndroidRuntime(10207):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)10-14 17:30:07.458:E / AndroidRuntime(10207):在dalvik.system.NativeStart.main(本机方法)10-14 17:30:07.458:E / AndroidRuntime(10207):原因:java.lang.NullPointerException 10-14 17:30:07.458:E / AndroidRuntime(10207) ):位于android.app.Activity.findVie wById(Activity.java:1884) 10-14 17:30:07.458: E/AndroidRuntime(10207): at com.myknitcards.project.MainActivity.(MainActivity.java:24) 10-14 17:30:07.458: E/AndroidRuntime(10207): at java.lang.Class.newInstanceImpl(Native Method) 10-14 17:30:07.458: E/AndroidRuntime(10207): at java.lang.Class.newInstance(Class.java:1208) 10-14 17:30:07.458: E/AndroidRuntime(10207): at android.app.Instrumentation.newActivity(Instrumentation.java:1061) 10-14 17:30:07.458: E/AndroidRuntime(10207): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101) 10-14 17:30:07.458: E/AndroidRuntime(10207): ... 11 more 10-14 17:30:10.701: I/Process(10207): Sending signal. wById(Activity.java:1884)10-14 17:30:07.458:E / AndroidRuntime(10207):在com.myknitcards.project.MainActivity。(MainActivity.java:24)10-14 17:30:07.458:E / AndroidRuntime(10207):位于java.lang.Class.newInstanceImpl(本机方法)10-14 17:30:07.458:E / AndroidRuntime(10207):位于java.lang.Class.newInstance(Class.java:1208)10 -14 17:30:07.458:E / AndroidRuntime(10207):在android.app.Instrumentation.newActivity(Instrumentation.java:1061)10-14 17:30:07.458:E / AndroidRuntime(10207):在android.app .ActivityThread.performLaunchActivity(ActivityThread.java:2101)10-14 17:30:07.458:E / AndroidRuntime(10207):... 11更多10-14 17:30:10.701:I / Process(10207):发送信号。 PID: 10207 SIG: 9 PID:10207 SIG:9

I don't know where to even start. 我什至不知道从哪里开始。 Why would it not be able to instantiate the activity? 为什么它不能实例化活动? BTW, I have downloaded the TrivialDrive sample from Google's Developer site, and it will run on my tablet, so that's not the issue. 顺便说一句,我已经从Google开发人员网站下载了TrivialDrive示例,该示例将在我的平板电脑上运行,所以这不是问题。 Any suggestions are welcome. 欢迎任何建议。

Start with the last "Caused by" in the stack trace listing (that is the first exception): 从堆栈跟踪列表中的最后一个“ Caused by”开始(这是第一个例外):

Caused by: java.lang.NullPointerException 10-14 17:30:07.458: E/AndroidRuntime(10207): at android.app.Activity.findViewById(Activity.java:1884) 引起原因:java.lang.NullPointerException 10-14 17:30:07.458:E / AndroidRuntime(10207):at android.app.Activity.findViewById(Activity.java:1884)

The doc says findViewById(int id) returns "The view if found or null otherwise." 该文档说findViewById(int id)返回“如果找到该视图,否则返回null”。 But it's not returning null, it's throwing NPE. 但是它没有返回null,而是抛出了NPE。

You could look at the source code for Activity.java:1884 or set an exception breakpoint to find out what null it's tripping over. 您可以查看Activity.java:1884的源代码,也可以设置一个异常断点来查找其传递的null。

Perhaps it couldn't load the needed layout resources or manifest or other resources. 也许它无法加载所需的布局资源或清单或其他资源。 Or maybe a resource is missing. 也许缺少资源。

Here's a link to the source code for ?? 这是指向??的源代码的链接。 version of Activity.java . Activity.java的版本。 It says: 它说:

public View findViewById(int id) {
    return getWindow().findViewById(id);
}

So getWindow() must be returning null . 因此, getWindow()必须返回null The doc says it returns "The current window, or null if the activity is not visual." 该文档说它返回“当前窗口,如果活动不可见,则返回null”。

Apparently it thinks your main activity is not "visual." 显然,它认为您的主要活动不是“视觉”。 Is your main activity declared to be a Service? 您的主要活动是否宣布为服务?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM