0

我正在按照本教程中的指南编写一个非常简单的应用程序。

我尝试运行我的应用程序,但在打开时失败。编写的 C 代码已使用ndk-build.

这是Java代码:

package com.example.ndktest;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {


    private Button button;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i( "MainActivity", "beginning of onCreate()" );
        setContentView(R.layout.activity_main);

        button = (Button) findViewById( R.id.button1 );
        button.setOnClickListener( new OnClickListener( ) {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            helloLog( "This will log to LogCat" );
        }

        });
    }
    private native void helloLog( String logThis );

    static {
        System.loadLibrary( "ndk1" );
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }   
}

这是C代码:

#include <jni.h>
#include <string.h>
#include <android/log.h>

#define DEBUG_TAG "NDK_AndroidNDK1SampleActivity"

void Java_com_example_ndktest_MainActivity_helloLog( JNIEnv * env, jobject this, jstring logThis )
{
    jboolean isCopy;
    const char * szLogThis = (*env)->GetStringUTFChars( env, logThis, &isCopy );

    __android_log_print( ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:LC: [%s]", szLogThis );

    (*env)->ReleaseStringUTFChars( env, logThis, szLogThis );
}

这是MakeFile:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)  

LOCAL_LDLIBS := -llog  

LOCAL_MODULE    := ndk1  
LOCAL_SRC_FILES := native.c  

include $(BUILD_SHARED_LIBRARY)

这是 XML 文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:padding="@dimen/padding_medium"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:text="Click Here to Log" />

</RelativeLayout>

这是 AndroidManifest :

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.ndktest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

这是我在 Eclipse 中的环境图片:

在此处输入图像描述

编辑 :

发现 LogCat 确实显示了一些东西,但它们被过滤器隐藏了......仍然无法找出问题所在。这是 LogCat:

07-30 15:22:31.718: E/AndroidRuntime(2589): FATAL EXCEPTION: main
07-30 15:22:31.718: E/AndroidRuntime(2589): java.lang.ExceptionInInitializerError
07-30 15:22:31.718: E/AndroidRuntime(2589):     at java.lang.Class.newInstanceImpl(Native Method)
07-30 15:22:31.718: E/AndroidRuntime(2589):     at java.lang.Class.newInstance(Class.java:1409)
07-30 15:22:31.718: E/AndroidRuntime(2589):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
07-30 15:22:31.718: E/AndroidRuntime(2589):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
07-30 15:22:31.718: E/AndroidRuntime(2589):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-30 15:22:31.718: E/AndroidRuntime(2589):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-30 15:22:31.718: E/AndroidRuntime(2589):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-30 15:22:31.718: E/AndroidRuntime(2589):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 15:22:31.718: E/AndroidRuntime(2589):     at android.os.Looper.loop(Looper.java:130)
07-30 15:22:31.718: E/AndroidRuntime(2589):     at android.app.ActivityThread.main(ActivityThread.java:3683)
07-30 15:22:31.718: E/AndroidRuntime(2589):     at java.lang.reflect.Method.invokeNative(Native Method)
07-30 15:22:31.718: E/AndroidRuntime(2589):     at java.lang.reflect.Method.invoke(Method.java:507)
07-30 15:22:31.718: E/AndroidRuntime(2589):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-30 15:22:31.718: E/AndroidRuntime(2589):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-30 15:22:31.718: E/AndroidRuntime(2589):     at dalvik.system.NativeStart.main(Native Method)
07-30 15:22:31.718: E/AndroidRuntime(2589): Caused by: java.lang.UnsatisfiedLinkError: Couldn't load ndk1: findLibrary returned null
07-30 15:22:31.718: E/AndroidRuntime(2589):     at java.lang.Runtime.loadLibrary(Runtime.java:429)
07-30 15:22:31.718: E/AndroidRuntime(2589):     at java.lang.System.loadLibrary(System.java:554)
07-30 15:22:31.718: E/AndroidRuntime(2589):     at com.example.ndktest.MainActivity.<clinit>(MainActivity.java:38)
07-30 15:22:31.718: E/AndroidRuntime(2589):     ... 15 more
07-30 15:22:31.718: W/ActivityManager(1136):   Force finishing activity com.example.ndktest/.MainActivity

编辑 2:

这是我调用 ndk-build 后我的 cygwin 终端的图片:

在此处输入图像描述

4

1 回答 1

1

很可能libndk1.so您的Activity. 你确定libndk1.so构建良好吗?

by: java.lang.UnsatisfiedLinkError: Couldn't load ndk1: findLibrary returned null
于 2012-07-30T19:33:52.727 回答