2016-09-13 7 views
0

私のプロジェクトに問題があります。 私はMainActivityとInsertの2つのクラスを持っています。 "Insert"クラスでエラーが表示されます。 ダイアログ=新しいProgressDialog(MainActivity.this); 私のエラーは "囲いのクラスではありません"です。 私を助けてくださいエラー:内包クラスではありません

CODEインサート:あなたはコンテキスト&の時にそれを使用渡す必要がAsyncTaskのコンストラクタで

public class Insert extends AsyncTask<String, Void, Boolean> { 

ProgressDialog dialog; 

@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 
    dialog = new ProgressDialog(MainActivity.this); //error is here 
    dialog.setMessage("Inserting details, please wait"); 
    dialog.setTitle("Connecting... "); 
    dialog.show(); 
    dialog.setCancelable(false); 
} 

@Override 
protected Boolean doInBackground(String... urls) { 


    try { 

     List<NameValuePair> insert = new ArrayList<NameValuePair>(); 
     insert.add(new BasicNameValuePair("name", nameStr)); 
     insert.add(new BasicNameValuePair("country", countryStr)); 
     insert.add(new BasicNameValuePair("email", emailStr)); 
     insert.add(new BasicNameValuePair("hobbies", hobbiesStr)); 


     HttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(
       "http://bookcompr.netne.net/input.php"); 
     httpPost.setEntity(new UrlEncodedFormEntity(insert)); 

     HttpResponse response = httpClient.execute(httpPost); 

     HttpEntity entity = response.getEntity(); 

     return true; 


    } catch (IOException e) { 
     e.printStackTrace(); 

    } 


    return false; 
} 

protected void onPostExecute(Boolean result) { 
    dialog.cancel(); 

    AlertDialog.Builder ac = new AlertDialog.Builder(MainActivity.this);//error is here 

    ac.setTitle("Result"); 
    ac.setMessage("Details Successfully Inserted"); 
    ac.setCancelable(true); 

    ac.setPositiveButton(
      "Ok", 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        dialog.cancel(); 

       } 
      }); 

    AlertDialog alert = ac.create(); 
    alert.show(); 
} 

}

CODE MainActivity

public class MainActivity extends AppCompatActivity { 

EditText name, country, email, hobbies; 

Button insert; 

String nameStr, countryStr, emailStr, hobbiesStr; 

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

    name = (EditText) findViewById(R.id.name); 
    country = (EditText) findViewById(R.id.country); 
    email = (EditText) findViewById(R.id.email); 
    hobbies = (EditText) findViewById(R.id.hobbies); 

    insert = (Button) findViewById(R.id.insert); 

    insert.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      nameStr = name.getText().toString(); 
      countryStr = country.getText().toString(); 
      emailStr = email.getText().toString(); 
      hobbiesStr = hobbies.getText().toString(); 

      new Insert().execute(); 
     } 
    }); 
} 

public class Insert extends AsyncTask<String, Void, Boolean> { 

    ProgressDialog dialog; 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     dialog = new ProgressDialog(MainActivity.this); 
     dialog.setMessage("Inserting details, please wait"); 
     dialog.setTitle("Connecting... "); 
     dialog.show(); 
     dialog.setCancelable(false); 
    } 

    @Override 
    protected Boolean doInBackground(String... urls) { 


     try { 

      List<NameValuePair> insert = new ArrayList<NameValuePair>(); 
      insert.add(new BasicNameValuePair("name", nameStr)); 
      insert.add(new BasicNameValuePair("country", countryStr)); 
      insert.add(new BasicNameValuePair("email", emailStr)); 
      insert.add(new BasicNameValuePair("hobbies", hobbiesStr)); 


      HttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(
        "http://bookcompr.netne.net/input.php"); // link to connect to database 
      httpPost.setEntity(new UrlEncodedFormEntity(insert)); 

      HttpResponse response = httpClient.execute(httpPost); 

      HttpEntity entity = response.getEntity(); 

      return true; 


     } catch (IOException e) { 
      e.printStackTrace(); 

     } 


     return false; 
    } 

    protected void onPostExecute(Boolean result) { 
     dialog.cancel(); 

     AlertDialog.Builder ac = new AlertDialog.Builder(MainActivity.this); 
     ac.setTitle("Result"); 
     ac.setMessage("Details Successfully Inserted"); 
     ac.setCancelable(true); 

     ac.setPositiveButton(
       "Ok", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         dialog.cancel(); 

        } 
       }); 

     AlertDialog alert = ac.create(); 
     alert.show(); 
    } 

} 

}

+0

適切な命名を持つ専用ファイル内の各クラスを入れて(と '新しいProgressDialog(MainActivity.this)を変更;' '新しいProgressDialog(Insert.this)に;') –

+0

あなたが使用して 'context'にアクセスすることはできません'MainActivity.this'はです。あなたはそのクラスに有効な 'context'を提供する必要があります(例えば、コンストラクタで) – Shaishav

答えて

0

あなたは必要でした。 mainActivityで

public class Insert extends AsyncTask<Void, Void, String> { 
    private Context mContext; 
    public Insert (Context context){ 
     mContext = context; 
    } 

    protected void onPreExecute() { 
     super.onPreExecute(); 
     ProgressDialog pd= new ProgressDialog(mContext); //changes is here 
     pd.setMessage("your message"); 
     pd.show(); 
    } 
} 

はこのようにそれを使用しています。

Insert insert = new Insert(getApplicationContext()); 
insert.execute(); 
関連する問題