2016-12-05 3 views
0

GitHubコードDatabaseManager_For_Androidのアンドロイドスタジオで作業しています。getActivity()メソッドでエラーが発生しています。 次のエラー:GitHubコードDatabaseManager_For_Androidで作業中で、getActivity()メソッドでエラーが発生しました。

Error:(40, 47) error: cannot find symbol method getActivity()

何か助けてください。

コード: MainActivity.this

だからラインになる:

package com.example.toshiba.myapplication; 

import android.app.Activity; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import com.google.android.gms.appindexing.Action; 
import com.google.android.gms.appindexing.AppIndex; 
import com.google.android.gms.common.api.GoogleApiClient; 

import java.util.List; 

public class MainActivity extends Activity { 

    /** 
    * ATTENTION: This was auto-generated to implement the App Indexing API. 
    * See https://g.co/AppIndexing/AndroidStudio for more information. 
    */ 
    private GoogleApiClient client; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Button sButton = (Button) findViewById(R.id.button_l); 
     TextView tv = (TextView) findViewById(R.id.text_id); 

     sButton.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       Intent dbmanager = new Intent(getActivity(), AndroidDatabaseManager.class); 
       startActivity(dbmanager); 
      } 
     }); 
     tv.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 

       Intent dbmanager = new Intent(getActivity(), AndroidDatabaseManager.class); 
       startActivity(dbmanager); 
      } 
     }); 
     DBHandler db = new DBHandler(this); 

// Inserting Shop/Rows 
     Log.d("Insert: ", "Inserting .."); 
     db.addShop(new Shop("Dockers", " 475 Brannan St #330, San Francisco, CA 94107, United States")); 
     db.addShop(new Shop("Dunkin Donuts", "White Plains, NY 10601")); 
     db.addShop(new Shop("Pizza Porlar", "North West Avenue, Boston , USA")); 
     db.addShop(new Shop("Town Bakers", "Beverly Hills, CA 90210, USA")); 

// Reading all shops 
     Log.d("Reading: ", "Reading all shops.."); 
     List<Shop> shops = db.getAllShops(); 

     for (Shop shop : shops) { 
      String log = "Id: " + shop.getId() + " ,Name: " + shop.getName() + " ,Address: " + shop.getAddress(); 
// Writing shops to log 
      Log.d("Shop: : ", log); 
     } 

     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 
    } 

    @Override 
    public void onStart() { 
     super.onStart(); 

     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     client.connect(); 
     Action viewAction = Action.newAction(
       Action.TYPE_VIEW, // TODO: choose an action type. 
       "Main Page", // TODO: Define a title for the content shown. 
       // TODO: If you have web page content that matches this app activity's content, 
       // make sure this auto-generated web page URL is correct. 
       // Otherwise, set the URL to null. 
       Uri.parse("http://host/path"), 
       // TODO: Make sure this auto-generated app deep link URI is correct. 
       Uri.parse("android-app://com.example.toshiba.myapplication/http/host/path") 
     ); 
     AppIndex.AppIndexApi.start(client, viewAction); 
    } 

    @Override 
    public void onStop() { 
     super.onStop(); 

     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     Action viewAction = Action.newAction(
       Action.TYPE_VIEW, // TODO: choose an action type. 
       "Main Page", // TODO: Define a title for the content shown. 
       // TODO: If you have web page content that matches this app activity's content, 
       // make sure this auto-generated web page URL is correct. 
       // Otherwise, set the URL to null. 
       Uri.parse("http://host/path"), 
       // TODO: Make sure this auto-generated app deep link URI is correct. 
       Uri.parse("android-app://com.example.toshiba.myapplication/http/host/path") 
     ); 
     AppIndex.AppIndexApi.end(client, viewAction); 
     client.disconnect(); 
    } 
} 
+0

はどこ 'getActivityあります()'定義されている? –

+0

申し訳ありませんが、私はあなたが何を意味するか分かりません。私は本当に感謝します:) – user7253937

答えて

0

thisまたはアクティビティクラスの名前の方法getActivity()を交換
Intent dbmanager = new Intent(MainActivity.this, AndroidDatabaseManager.class);

+1

私はこれだけ前にgetActivity()を置き換えようとしましたが、うまくいきませんでしたが、2番目の提案はうまくいった:)ありがとう – user7253937

関連する問題