这是renungan.java
public class renungan extends AppCompatActivity{
AlertDialog alertDialog;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
private NavigationView nvDrawer;
//untuk Posting
EditText judul_r, isi_r;
View view;
//untuk Renungan
String myJSON;
private static final String TAG_RESULT = "result";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_ADD = "address";
private static final String TAG_PHOTO = "gambar";
JSONArray peoples = null;
ArrayList<HashMap<String, String>> personList;
ListView list;
//untuk Renungan
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sidebar_drawer);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
nvDrawer = (NavigationView) findViewById(R.id.NV_sidebar);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
//ini untuk muncul TOMBOL disebelah kiri
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setupDrawerContent(nvDrawer);
//utk Renungan
list = (ListView) findViewById(R.id.Lv_renungan);
personList = new ArrayList<HashMap<String, String>>();
//utk Posting
judul_r = (EditText) findViewById(R.id.ET_judulrenungan);
isi_r = (EditText) findViewById(R.id.ET_isirenungan);
}
@Override //ketika klik di ITEM yg ada di TOOLBAR
public boolean onOptionsItemSelected(MenuItem item) {
if(mToggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
private void setupDrawerContent (NavigationView navigationView){
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener(){
@Override
public boolean onNavigationItemSelected(MenuItem menuItem){
selectDrawerItem(menuItem);
return true;
}
}
);
}
public void selectDrawerItem(MenuItem menuItem) {
Fragment fragment = null;
Class fragmentClass = null;
switch (menuItem.getItemId()){
case R.id.nav_renungan :
fragmentClass = pertama.class;
getData();
break;
case R.id.nav_post:
fragmentClass = posting.class;
break;
case R.id.nav_account:
fragmentClass = kedua.class;
break;
case R.id.nav_settings:
fragmentClass = ketiga.class;
break;
case R.id.nav_logout:
fragmentClass = keempat.class;
break;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.FL_content, fragment).commit();
menuItem.setChecked(true);
setTitle(menuItem.getTitle());
mDrawerLayout.closeDrawers();
}
//utk renungan
public void getData(){
class GetDataJSON extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://192.168.1.15/projectRenungan/getData.php");
httppost.setHeader("Content Type", "application/json");
InputStream inputStream = null;
String result = null;
try{
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null){
sb.append(line + "\n");
}
result = sb.toString();
}
catch (Exception e){
e.printStackTrace();
}
finally {
try{
if(inputStream != null) inputStream.close();
}
catch (Exception squish){
}
}
return result;
}
@Override
protected void onPostExecute(String result) {
myJSON = result;
showList();
}
}
GetDataJSON g = new GetDataJSON();
g.execute();
} //utk renungan
//utk renungan
protected void showList(){
try{
JSONObject jsonObj = new JSONObject(myJSON);
peoples = jsonObj.getJSONArray(TAG_RESULT);
for(int i=0; i<peoples.length(); i++){
JSONObject c = peoples.getJSONObject(i);
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String address = c.getString(TAG_ADD);
String photo = c.getString(TAG_PHOTO);
HashMap<String,String> persons = new HashMap<String, String>();
persons.put(TAG_ID,id);
persons.put(TAG_NAME,name);
persons.put(TAG_ADD,address);
persons.put(TAG_PHOTO,photo);
personList.add(persons);
}
ListAdapter adapter = new SimpleAdapter(
renungan.this, personList, R.layout.design_row,
new String[]{TAG_ID,TAG_NAME,TAG_ADD, TAG_PHOTO},
new int[]{R.id.tv_jmlviewer, R.id.tv_judul, R.id.tv_tanggal, R.id.IV_renungan}
);
list.setAdapter(adapter);
}
catch (JSONException e){
e.printStackTrace();
}
}//utk renungan
public void OnPost (View v){
String judul = judul_r.getText().toString();
String isi = isi_r.getText().toString();
String type = "posting";
BackgroundWorker backgroundWorker = new BackgroundWorker(this);
backgroundWorker.execute(type, judul, isi);
}
}
这是BackgroundWorker.java
public class BackgroundWorker extends AsyncTask<String, Void, String> {
Context context;
AlertDialog alertDialog;
BackgroundWorker (Context ctx) {
context = ctx;
}
@Override
protected String doInBackground(String... params) {
String type = params[0];
String login_url = "http://192.168.1.15/projectRenungan/login.php";
String register_url = "http://192.168.1.15/projectRenungan/register.php";
String posting_url = "http://192.168.1.15/projectRenungan/posting.php";
//UNTUK LOGIN
if(type.equals("login")){
try {
String user_name = params[1];
String password = params[2];
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8")+"&"
+URLEncoder.encode("user_name","UTF-8")+"="+URLEncoder.encode(user_name,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String result="";
String line="";
while((line = bufferedReader.readLine()) != null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
//$result dari login.php masuk kesini
return result;
}
catch (MalformedURLException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
}
//UNTUK DATA REGISTER
else if(type.equals("register")){
try{
String nama = params[1];
String email = params[2];
String no_hp = params[3];
String password = params[4];
URL url = new URL(register_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("nama","UTF-8")+"="+URLEncoder.encode(nama,"UTF-8")+"&"
+URLEncoder.encode("nohp","UTF-8")+"="+URLEncoder.encode(no_hp,"UTF-8")+"&"
+URLEncoder.encode("passwordword","UTF-8")+"="+URLEncoder.encode(password,"UTF-8")+"&"
+URLEncoder.encode("emailmail","UTF-8")+"="+URLEncoder.encode(email,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String result="";
String line="";
while((line = bufferedReader.readLine()) != null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
//$result dari register.php masuk kesini
return result;
}
catch (MalformedURLException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
}
//UNTUK POSTING RENUNGAN
else if(type.equals("posting")){
try{
String judul = params[1];
String isi = params[2];
URL url = new URL(posting_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("judul","UTF-8")+"="+URLEncoder.encode(judul,"UTF-8")+"&"
+URLEncoder.encode("isi","UTF-8")+"="+URLEncoder.encode(isi,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String result="";
String line="";
while((line = bufferedReader.readLine()) != null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
//$result dari register.php masuk kesini
return result;
}
catch (MalformedURLException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Login Status");
}
@Override //Setelah Login Sukses maupun Gagal
protected void onPostExecute(String result) {
if(result.contains("login success")) {
Intent i = new Intent(context, renungan.class);
context.startActivity(i);
alertDialog.setMessage(result);
alertDialog.show();
}
else{
alertDialog.setMessage(result);
alertDialog.show();
}
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
这是posting.java
public class posting extends Fragment {
EditText judul, isi;
View view;
Button btnPost;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.posting, container, false);
}
}
这是posts.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ET_judulrenungan"
android:hint="JUDUL" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/ET_judulrenungan"
android:id="@+id/ET_isirenungan"
android:hint="ISI" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="POST"
android:id="@+id/BTN_post"
android:layout_weight="1"
android:onClick="OnPost">
</Button>
</RelativeLayout>
</LinearLayout>
我可以编译它和它的运行但是当我伸出POST按钮它不会工作
I am using sidebar to enter posting.java and posting.xml
it is failed when i click the "POST" button
这是错误堆栈消息
11-16 08:31:50.850 2768-2768 / com.breakthrough.renungan E / AndroidRuntime:FATAL EXCEPTION:main 过程:com.breakthrough.renungan,PID:2768 java.lang.IllegalStateException:无法执行android:onClick的方法 在android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293) 在android.view.View.performClick(View.java:5198) 在android.view.View $ PerformClick.run(View.java:21147) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 引起:java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) 在android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 在android.view.View.performClick(View.java:5198) 在android.view.View $ PerformClick.run(View.java:21147) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 引起:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'android.text.Editable android.widget.EditText.getText()' 在com.breakthrough.renungan.renungan.OnPost(renungan.java:254) at java.lang.reflect.Method.invoke(Native Method) 在android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 在android.view.View.performClick(View.java:5198) 在android.view.View $ PerformClick.run(View.java:21147) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:0)
尝试改变这个: BackgroundWorker backgroundWorker = new BackgroundWorker(renungan.this);