2014-04-24 36 views
0

我已经发布了以下两个类和微调的XML代码。我也发布了logcat错误。当我点击微调项目,它不幸已经停止。所说的纺丝器的物品装入它们从MySQL数据库中检索不幸的是,当点击微调项目时已停止

MyMainScreen.java

public class MyMainScreen extends Activity implements OnClickListener{ 

// Widget GUI 
Button btnCalendar, btnCalendar1, btnTime, btnTime1; 
private Spinner spinner1, spinner2; 
private ArrayList<Category> categoriesList; 
ProgressDialog pDialog; 

// API urls 
// Url to get all categories 
private String URL_CATEGORIES = "http://10.0.2.2:8888/jobs/get_categories.php"; 


// Variable for storing current date and time 

private int mYear, mMonth, mDay, mHour, mMinute; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    addListenerOnSpinnerItemSelection(); 

    btnCalendar = (Button) findViewById(R.id.btnCalendar); 
    btnTime = (Button) findViewById(R.id.btnTime); 
    btnCalendar1 = (Button) findViewById(R.id.btnCalendar1); 
    btnTime1 = (Button) findViewById(R.id.btnTime1); 

    btnCalendar.setOnClickListener(this); 
    btnTime.setOnClickListener(this); 
    btnCalendar1.setOnClickListener(this); 
    btnTime1.setOnClickListener(this); 
    categoriesList = new ArrayList<Category>(); 

    new GetCategories().execute(); 
} 

/** 
* Async task to get all food categories 
* */ 
private class GetCategories extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(MyMainScreen.this); 
     pDialog.setMessage("Fetching data.."); 
     pDialog.setCancelable(false); 
     pDialog.show(); 

    } 

    @Override 
    protected Void doInBackground(Void... arg0) { 
     ServiceHandler jsonParser = new ServiceHandler(); 
     String json = jsonParser.makeServiceCall(URL_CATEGORIES, ServiceHandler.GET); 

     Log.e("Response: ", "> " + json); 

     if (json != null) { 
      try { 
       JSONObject jsonObj = new JSONObject(json); 
       if (jsonObj != null) { 
        JSONArray categories = jsonObj 
          .getJSONArray("pickup");       

        for (int i = 0; i < categories.length(); i++) { 
         JSONObject catObj = (JSONObject) categories.get(i); 
         Category cat = new Category(catObj.getInt("id"), 
           catObj.getString("name")); 
         categoriesList.add(cat); 
        } 
       } 

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

     } else { 
      Log.e("JSON Data", "Didn't receive any data from server!"); 
     } 

     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     super.onPostExecute(result); 
     if (pDialog.isShowing()) 
      pDialog.dismiss(); 
     populateSpinner(); 
    } 

} 

/** 
* Adding spinner data 
* */ 
private void populateSpinner() { 
    List<String> lables = new ArrayList<String>(); 

    for (int i = 0; i < categoriesList.size(); i++) { 
     lables.add(categoriesList.get(i).getName()); 
    } 

    // Creating adapter for spinner 
    ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_spinner_item, lables); 

    // Drop down layout style - list view with radio button 
    spinnerAdapter 
      .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

    // attaching data adapter to spinner 
    spinner1.setAdapter(spinnerAdapter); 
    spinner2.setAdapter(spinnerAdapter); 
} 

public void addListenerOnSpinnerItemSelection() { 
    spinner1 = (Spinner) findViewById(R.id.spinnerpick); 
    spinner1.setOnItemSelectedListener(new ItemSelectedListener()); 
    spinner2 = (Spinner) findViewById(R.id.spinnerdrop); 
    spinner2.setOnItemSelectedListener(new ItemSelectedListener()); 
    } 



@Override 
public void onClick(View v) { 

    if (v == btnCalendar) { 
    // Process to get Current Date 
    final Calendar c = Calendar.getInstance(); 
    mYear = c.get(Calendar.YEAR); 
    mMonth = c.get(Calendar.MONTH); 
    mDay = c.get(Calendar.DAY_OF_MONTH); 

    // Launch Date Picker Dialog 
    DatePickerDialog dpd = new DatePickerDialog(this, 
      new DatePickerDialog.OnDateSetListener() { 

       @Override 
       public void onDateSet(DatePicker view, int year, 
         int monthOfYear, int dayOfMonth) { 
        // Display Selected date in text box 
        btnCalendar.setText(dayOfMonth + "-" 
          + (monthOfYear + 1) + "-" + year); 

       } 
      }, mYear, mMonth, mDay); 
    dpd.show(); 
} 

    if (v == btnTime) { 

     // Process to get Current Time 
     final Calendar c = Calendar.getInstance(); 
     mHour = c.get(Calendar.HOUR_OF_DAY); 
     mMinute = c.get(Calendar.MINUTE); 

     // Launch Time Picker Dialog 
     TimePickerDialog tpd = new TimePickerDialog(this, 
       new TimePickerDialog.OnTimeSetListener() { 

        @Override 
        public void onTimeSet(TimePicker view, int hourOfDay, 
          int minute) { 
         // Display Selected time in text box 
         btnTime.setText(hourOfDay + ":" + minute); 
        } 
       }, mHour, mMinute, false); 
     tpd.show(); 



} 

    if (v == btnCalendar1) { 
      // Process to get Current Date 
      final Calendar c = Calendar.getInstance(); 
      mYear = c.get(Calendar.YEAR); 
      mMonth = c.get(Calendar.MONTH); 
      mDay = c.get(Calendar.DAY_OF_MONTH); 

      // Launch Date Picker Dialog 
      DatePickerDialog dpd1 = new DatePickerDialog(this, 
        new DatePickerDialog.OnDateSetListener() { 

         @Override 
         public void onDateSet(DatePicker view, int year, 
           int monthOfYear, int dayOfMonth) { 
          // Display Selected date in text box 
          btnCalendar1.setText(dayOfMonth + "-" 
            + (monthOfYear + 1) + "-" + year); 

         } 
        }, mYear, mMonth, mDay); 
      dpd1.show(); 
     } 

      if (v == btnTime1) { 

       // Process to get Current Time 
       final Calendar c = Calendar.getInstance(); 
       mHour = c.get(Calendar.HOUR_OF_DAY); 
       mMinute = c.get(Calendar.MINUTE); 

       // Launch Time Picker Dialog 
       TimePickerDialog tpd1 = new TimePickerDialog(this, 
         new TimePickerDialog.OnTimeSetListener() { 

          @Override 
          public void onTimeSet(TimePicker view, int hourOfDay, 
            int minute) { 
           // Display Selected time in text box 
           btnTime1.setText(hourOfDay + ":" + minute); 
          } 
         }, mHour, mMinute, false); 
       tpd1.show(); 



    } 

    } 

ItemSelectedListener.java

public class ItemSelectedListener extends Activity implements OnItemSelectedListener { 

int check=0; 

public void onClick(View v) { 
    // TODO Auto-generated method stub 

} 


    public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) { 
     check=check+1; 
       if(check>1) 
       { 
    Toast.makeText(parent.getContext(), 
     "You have selected : " + parent.getItemAtPosition(pos).toString(), 
     Toast.LENGTH_SHORT).show(); 
    } 


switch(pos){ 
    case 1: 
      Intent intent = new Intent(ItemSelectedListener.this, Play.class); 
         startActivity(intent); 
         break; 
     case 2: 
      Intent intenti = new Intent(ItemSelectedListener.this, Play.class); 
         startActivity(intenti); 
         break; 
     // and so on 
     // ..... 

       } 


    } 
@Override 
public void onNothingSelected(AdapterView<?> arg0) { 
    // TODO Auto-generated method stub 

} 


} 

activity_main.xml中

JSON基于数据
<LinearLayout 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" 
android:orientation="vertical" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/pick_up" /> 

<LinearLayout 
    android:id="@+id/linearlayout1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <Spinner 
     android:id="@+id/spinnerpick" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:spinnerMode="dropdown" 
     /> 

    <Button 
     android:id="@+id/btnCalendar" 
     style="?android:attr/spinnerStyle" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/Calendar" /> 

    <Button 
     android:id="@+id/btnTime" 
     style="?android:attr/spinnerStyle" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/time_picker" /> 
</LinearLayout> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingTop="10dp" 
    android:text="@string/drop_off" /> 

<LinearLayout 
    android:id="@+id/linearlayout2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

     <Spinner 
     android:id="@+id/spinnerdrop" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:spinnerMode="dropdown" /> 

    <Button 
     android:id="@+id/btnCalendar1" 
     style="?android:attr/spinnerStyle" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/Calendar" /> 

    <Button 
     android:id="@+id/btnTime1" 
     style="?android:attr/spinnerStyle" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/time_picker" /> 

    </LinearLayout> 

<RelativeLayout 
    android:id="@+id/RelativeLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

<Button 
     android:id="@+id/btnSubmit" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:text="@string/button_send" /> 

    </RelativeLayout> 

logcat的

04-24 03:08:10.585: D/AndroidRuntime(1115): Shutting down VM 
    04-24 03:08:10.585: W/dalvikvm(1115): threadid=1: thread exiting with uncaught exception  (group=0x41465700) 
    04-24 03:08:10.635: E/AndroidRuntime(1115): FATAL EXCEPTION: main 
    04-24 03:08:10.635: E/AndroidRuntime(1115): java.lang.NullPointerException 
    04-24 03:08:10.635: E/AndroidRuntime(1115): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:135) 
    04-24 03:08:10.635: E/AndroidRuntime(1115): at android.content.ComponentName.<init>(ComponentName.java:75) 
    04-24 03:08:10.635: E/AndroidRuntime(1115): at android.content.Intent.<init>(Intent.java:3662) 
    04-24 03:08:10.635: E/AndroidRuntime(1115): at com.example.carrental.ItemSelectedListener.onItemSelected(ItemSelectedListener.java:32) 
    04-24 03:08:10.635: E/AndroidRuntime(1115): at android.widget.AdapterView.fireOnSelected(AdapterView.java:892) 
    04-24 03:08:10.635: E/AndroidRuntime(1115): at android.widget.AdapterView.access$200(AdapterView.java:49) 
    04-24 03:08:10.635: E/AndroidRuntime(1115): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:860) 
    04-24 03:08:10.635: E/AndroidRuntime(1115): at android.os.Handler.handleCallback(Handler.java:730) 
    04-24 03:08:10.635: E/AndroidRuntime(1115): at android.os.Handler.dispatchMessage(Handler.java:92) 
    04-24 03:08:10.635: E/AndroidRuntime(1115): at android.os.Looper.loop(Looper.java:137) 
    04-24 03:08:10.635: E/AndroidRuntime(1115): at android.app.ActivityThread.main(ActivityThread.java:5103) 
    04-24 03:08:10.635: E/AndroidRuntime(1115): at java.lang.reflect.Method.invokeNative(Native Method) 
    04-24 03:08:10.635: E/AndroidRuntime(1115): at java.lang.reflect.Method.invoke(Method.java:525) 
    04-24 03:08:10.635: E/AndroidRuntime(1115): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
    04-24 03:08:10.635: E/AndroidRuntime(1115): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
    04-24 03:08:10.635: E/AndroidRuntime(1115): at dalvik.system.NativeStart.main(Native Method) 
+0

有任何错误日志? 打印您的堆栈跟踪 –

+0

摆脱扩展活动公共类ItemSelectedListener扩展活动实现OnItemSelectedListener {' – Raghunandan

+0

@nayoso我发布了上面的错误日志 – user3402438

回答

1

看来你叫getPackageName()断章取义。请调用getPackageName()在Activity.onCreate()

+0

你是什么意思?add getPackage name for what使用? – user3402438

+0

你不能新建Activity或任何子类的实例。活动应该由Android Framework创建。因为您创建的新ItemSelectedListener实例的onCreate方法将不会被调用,这意味着您已经脱离了上下文。 –

0

你应该摆脱的公共类ItemSelectedListener
和您可以使用parent.getContext()开始活动延伸活动startActivity()。

0
04-24 03:08:10.635: E/AndroidRuntime(1115): java.lang.NullPointerException 
04-24 03:08:10.635: E/AndroidRuntime(1115): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:135) 
04-24 03:08:10.635: E/AndroidRuntime(1115): at android.content.ComponentName.<init>(ComponentName.java:75) 
04-24 03:08:10.635: E/AndroidRuntime(1115): at android.content.Intent.<init>(Intent.java:3662) 

Context在初始化Intent为空。要知道为什么读

Can i Create the object of a activity in other class?

你有什么

public class ItemSelectedListener extends Activity implements OnItemSelectedListener { 

spinner1.setOnItemSelectedListener(new ItemSelectedListener()); 

你不能实例化一个Activity类。

您可以使用anonumous内部类。因此更改为

spinner1.setOnItemSelectedListener(new OnItemSelectedListener() 
{ 

    @Override 
    public void onItemSelected(AdapterView<?> parent, View arg1, int pos, 
      long arg3) { 
     Toast.makeText(MainScreen.this, 
        "You have selected : " + parent.getItemAtPosition(pos).toString(), 
        Toast.LENGTH_SHORT).show(); 



      switch(pos){ 
       case 1: 
         Intent intent = new Intent(MyMainScreen.this, Play.class); 
            startActivity(intent); 
       break; 
       case 2: 
         Intent intenti = new Intent(MyMainScreen.this, Play.class); 
            startActivity(intenti); 
       break; 

       } 

    } 

    @Override 
    public void onNothingSelected(AdapterView<?> arg0) { 
     // TODO Auto-generated method stub 

    } 

}); 

同样对于spinner2

你有什么是一个匿名的内部类,它实现了接口OnItemSelectedListener你重写onItemSelected并做那里需要的东西。

+0

@Raghunandan你的意思是把onItemSelected方法放在mainscreen.java里吗?为什么onItemSelected方法不能在单独的类中? – user3402438

+0

@ user3402438'OnItemSelectedListener'是一个接口。无论哪种方式,一个类需要实现接口。在建议的解决方案中,您有一个实现接口的匿名内部类。你可以有一个实现接口的类。但为了意图你需要一个有效的上下文。 ALso startActivity是Activity类的一个方法 – Raghunandan

+0

@ user3402438或者按照我的建议或者黑皮书的建议,两者都可以工作,如果你不知道接口是什么,你需要阅读java – Raghunandan

0

你可以有一个活动的参数为监听器,如果你不希望内部类

public class ItemSelectedListener implements OnItemSelectedListener { 

int check=0; 
Activity mActivity; 
public ItemSelectedListener(Activity activity) { 
    mActivity = activity; 
} 

public void onClick(View v) { 
} 

public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) { 
     check=check+1; 
     if(check>1) { 
     Toast.makeText(mActivity, 
     "You have selected : " + parent.getItemAtPosition(pos).toString(), 
     Toast.LENGTH_SHORT).show(); 
     } 

    switch(pos){ 
     case 1: 
      Intent intent = new Intent(mActivity, Play.class); 
      mActivity.startActivity(intent); 
      break; 
     case 2: 
      Intent intenti = new Intent(mActivity, Play.class); 
      mActivity.startActivity(intenti); 
      break; 
     // and so on 
     // ..... 

     } 
    } 
    @Override 
    public void onNothingSelected(AdapterView<?> arg0) { 
    }  
} 
+0

但我用你的方法...在MainScreen类中,这部分是有一个arror公共无效addListenerOnSpinnerItemSelection(){ \t spinner1 =(微调)findViewById(R.id。 spinnerpick); \t spinner1.setOnItemSelectedListener(新ItemSelectedListener()); \t spinner2 =(微调)findViewById(R.id.spinnerdrop); \t spinner2.setOnItemSelectedListener(新ItemSelectedListener());} – user3402438

+0

变得'新项目SelectedListener(this))' – Blackbelt

+0

@blackbelt你知道如何解决它,因为你的代码由两个ItemSelectedListener组成,这两个ItemSelectedListener与这部分发生冲突:spinner1.setOnItemSelectedListener(new ItemSelectedListener()); – user3402438