Android使用AWS,Dynamodb,getCredentials()null对象引用

时间:2015-07-06 14:36:47

标签: android amazon-web-services

我在AWS网站上关注他们的dynamodb上的教程。(http://docs.aws.amazon.com/mobile/sdkforandroid/developerguide/dynamodb_om.html

但是当我使用mapper.save(object)函数时,我收到一条错误消息:

[Attempt to invoke interface method 'com.amazonaws.auth.AWSCredentials com.amazonaws.auth.AWSCredentialsProvider.getCredentials()' on a null object reference]

下面是我的代码

public class MainActivity extends ActionBarActivity {

    public CognitoCachingCredentialsProvider credentialsProvider;
    public AmazonDynamoDBClient ddbClient;

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

        CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
                this.getApplicationContext(), // Context
                "identity_pool_id", // Identity Pool ID
                Regions.US_EAST_1 // Region
        );




    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, 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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void saveFile(View view) {
        AmazonDynamoDBClient ddbClient = new AmazonDynamoDBClient(credentialsProvider);
        DynamoDBMapper mapper = new DynamoDBMapper(ddbClient);
        View name = (TextView) findViewById(R.id.name);
        View number = (TextView) findViewById(R.id.number);
        Book book = new Book();
        book.setTitle(name.toString());
        book.setAuthor("Charles Dickens");
        book.setPrice(1299);
        book.setIsbn(number.toString());
        book.setHardCover(false);
        mapper.save(book);
    }
}

图书类只是网站上的复制粘贴

1 个答案:

答案 0 :(得分:0)

CognitoCachingCredentialsProvider内初始化的onCreate()是本地实例,而不是预期的类级别1。请删除CognitoCachingCredentialsProvider中的onCreate()